Plugwise Circle Stick on Linux
(Updated: )
Getting Plugwise Cirlce+ working with Plugwise Stick on Ubuntu Linux.
Method 1 - use command-line python-plugwise (failed) ¶
This works but the data syntax returned is extremely cumbersome to track total usage with, i.e. energy consumption per hour in separate buffers.
- Create network using official Plugwise utilities
pip3 install plugwise
–> couldn’t find module documentation- Get https://github.com/cyberjunky/python-plugwise (github.com)
- Find unique USB URI of Plugwise Stick. Use
/dev/serial/by-id
for all scripts to prevent renaming/reordering/other nonsense of usb devices after reboot. - Read out stick
python3 ./plugwise_util -d /dev/serial/by-id/usb-FTDI_FT232R_USB_UART_AM123456-if00-port0 -m 000D6F000212345 -C
- Plugwise Circle stores energy consumption in a confusing ring buffer. Since I only want one counter (I can better do accounting of what energy was used when offline instead of on the stick), some summing is required. I gave up here.
- Current counter: gives energy of current hour
- Current buffer: gives energy of last hours
- Simple algorithm:
- if counter > previous counter: add delta value to internal counter and send to database
- else: add full value to internal counter and send to database
- Gap: misses data when clock moves from one hour to the next. Error is roughly polling interval / 1 hour. E.g. for 5min polling, error is 8%. Not
- Better algorithm
- if counter > previous counter:
- add delta value to internal counter and send to database
- else:
- get last buffer containing 4 hours of data
- If buffer contains previous hour: add delta value to internal counter
- Else: get last-1 buffer
- get last buffer containing 4 hours of data
- add full value of counter to internal counter and send to database
- if counter > previous counter:
Method 2 - use Home Assistant (failed) ¶
This works but has a problem that not all entities are populated (e.g. power consumption), see this (github.com) and this (github.com) issue.
- Understand Home Assistant installation methods / nomenclature (2021)
- Home Assistant Operating System: OS + Supervisor + Home Assistant Core + add-ons
- Home Assistant Container: container-based installation of Home Assistant Core
- Home Assistant Supervised: Manual installation of the Supervisor, advanced version of
- Home Assistant Core: Manual installation using Python virtual environment
- Install Home Assistant Core via virtual env (home-assistant.io)
- Tweak Home Assistant setup
- Disable infinite recording (TODO)
- Ensure auto-start via systemd (home-assistant.io)
- Configure add-ins
- Install plugwise-beta (github.com) (for USB stick access)
- Install HACS (hacs.xyz)
wget -q -O - https://install.hacs.xyz | bash -
- Install HACS (hacs.xyz)
- Add customer repository
https://github.com/plugwise/plugwise-beta
- Restart Home Assistant
- Add integration Plugwise Beta
- Use fixed USB path for Stick
- Wait until Circles are discovered
- Install plugwise-beta (github.com) (for USB stick access)
- Query entity state over Home Assistant REST (home-assistant.io) API to publish to influxdb
- Get long-lived token (via profile):
HATOKEN=<long string of ~183 chars>
- Get data via API
- Entity ID, device, and human-readable label:
ENTID=sensor.power_consumption_today_889c7;DEVID=000D6F00025889c7;LABEL="oven"
- Get entity state:
curl -X GET \ -H "Authorization: Bearer ${HATOKEN}" \ -H "Content-Type: application/json" \ "http://proteus.lan:8123/api/states/${ENTID}"
- Use
jq
to form into Influxdb line protocol likeenergyv3,quantity=electricity,uniqueid=${DEVID},type=consumption,source=${LABEL} value={energy} {timestamp}
:
Need:curl -X GET \ -H "Authorization: Bearer ${HATOKEN}" \ -H "Content-Type: application/json" \ "http://proteus.lan:8123/api/states/${ENTID}" | \ jq
.state
&.last_changed
- Entity ID, device, and human-readable label:
- Get long-lived token (via profile):
Method 3 - use plugwise-2-py (success) ¶
This method works and can broadcast data to Home Assistant (etc.) via MQTT.
- Create network using official Plugwise utilities (‘Source’ for Windows)
- Get Plugwise-2-py source (github.com)
- Find unique USB URI of Plugwise Stick. Use
/dev/serial/by-id
for all scripts to prevent renaming/reordering/other nonsense of usb devices after reboot. E.g./dev/serial/by-id/usb-FTDI_FT232R_USB_UART_AM123456-if00-port0
. - Setup MQTT broker (digitalocean.com), e.g. mosquitto (mosquitto.org)
- Configure setup
- See README (github.com) for instructions
- Configure
pw-hostconfig.json
:
{ "permanent_path": "/var/lib/plugwise-2-py", "tmp_path": "/tmp", "log_path": "/var/log/pwlog", "serial": "/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_AM123456-if00-port0", "log_format": "epoch", "mqtt_ip": "127.0.0.1", "mqtt_port": "1883", "mqtt_user": "username", "mqtt_password": "password" }
- Configure
pw-conf.json
. N.B. setloginterval
to 1 to get energy consumption updates live as well.
{ "static": [ { "mac": "000D6F0002512345", "category": "misc", "name": "circle+", "loginterval": "1", "always_on": "False", "production": "False", "location": "kitchen" } ] }
- Configure
pw-control.json
:
{ "dynamic": [ { "mac": "000D6F0002512345", "switch_state": "on", "name": "circle+", "schedule_state": "off", "schedule": "", "savelog": "yes", "monitor": "yes" } ], "log_level": "info", "log_comm": "no" }
- Run daemon
python3 Plugwise-2.py
- Check for errors
- In log file:
tail -f /var/log/pwlog/pw-logger.log
- In mosquitto:
mosquitto.sub -h 127.0.0.1 -u plugwise-2-py -P password -t "plugwise2py/#"
- In log file:
- Integrate in Home Assistant
- Add MQTT integration (home-assistant.io)
- Add MQTT sensor (home-assistant.io) one by one to
configuration.yaml
sensor: - platform: mqtt name: 'plugwise_12345_power' unique_id: 'plugwise_12345_power' state_topic: 'plugwise2py/state/energy/000D6F000212345' unit_of_measurement: 'W' value_template: '{{ value_json.power }}' device_class: power - platform: mqtt name: 'plugwise_12345_cum_energy' unique_id: 'plugwise_12345_cum_energy' state_topic: 'plugwise2py/state/energy/000D6F000212345' unit_of_measurement: 'kWh' value_template: '{{ value_json.cum_energy | float * 0.001 | round(2) }}' device_class: energy
- Ideally, one would use Home Assistant MQTT discovery (home-assistant.io), but the state topic formatting is not compatible here
- Profit!
Automate in Home Assistant ¶
- Build automations to check when devices are running
- Add input_boolean (home-assistant.io) to configuration.yaml:
# Example configuration.yaml entry input_boolean: dishwasher_active: name: Whether dishwasher is active icon: mdi:dishwasher
- Automation to check when device starts
alias: Dishwasher running trigger: - platform: numeric_state entity_id: sensor.power_usage_81600 above: '1000' condition: - condition: state entity_id: input_boolean.dishwasher_active state: 'off' action: - service: homeassistant.turn_on entity_id: input_boolean.dishwasher_active initial_state: true mode: single
- Automation to notify when device is ready
alias: Dishwasher ready notification trigger: - platform: numeric_state entity_id: sensor.power_usage_81600 below: '5' for: minutes: 1 condition: - condition: state entity_id: input_boolean.dishwasher_active state: 'on' action: - service: notify.notify data: message: Dishwasher is ready. - service: homeassistant.turn_off entity_id: input_boolean.dishwasher_active initial_state: true mode: single