Tim's blah blah blah

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.

  1. Create network using official Plugwise utilities
  2. pip3 install plugwise –> couldn’t find module documentation
  3. Get https://github.com/cyberjunky/python-plugwise (github.com)
  4. 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.
  5. Read out stick
    python3 ./plugwise_util -d /dev/serial/by-id/usb-FTDI_FT232R_USB_UART_AM123456-if00-port0 -m 000D6F000212345 -C
    
  6. 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.
    1. Current counter: gives energy of current hour
    2. Current buffer: gives energy of last hours
    3. Simple algorithm:
      1. if counter > previous counter: add delta value to internal counter and send to database
      2. else: add full value to internal counter and send to database
      3. 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
    4. Better algorithm
      1. if counter > previous counter:
        1. add delta value to internal counter and send to database
      2. else:
        1. get last buffer containing 4 hours of data
          1. If buffer contains previous hour: add delta value to internal counter
          2. Else: get last-1 buffer
      3. add full value of counter to internal counter and send to database

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.

  1. Understand Home Assistant installation methods / nomenclature (2021)
    1. Home Assistant Operating System: OS + Supervisor + Home Assistant Core + add-ons
    2. Home Assistant Container: container-based installation of Home Assistant Core
    3. Home Assistant Supervised: Manual installation of the Supervisor, advanced version of
    4. Home Assistant Core: Manual installation using Python virtual environment
  2. Install Home Assistant Core via virtual env (home-assistant.io)
  3. Tweak Home Assistant setup
    1. Disable infinite recording (TODO)
    2. Ensure auto-start via systemd (home-assistant.io)
  4. Configure add-ins
    1. Install plugwise-beta (github.com) (for USB stick access)
      1. Install HACS (hacs.xyz)
        1. wget -q -O - https://install.hacs.xyz | bash -
    2. Add customer repository https://github.com/plugwise/plugwise-beta
    3. Restart Home Assistant
    4. Add integration Plugwise Beta
    5. Use fixed USB path for Stick
    6. Wait until Circles are discovered
  5. Query entity state over Home Assistant REST (home-assistant.io) API to publish to influxdb
    1. Get long-lived token (via profile): HATOKEN=<long string of ~183 chars>
    2. Get data via API
      1. Entity ID, device, and human-readable label: ENTID=sensor.power_consumption_today_889c7;DEVID=000D6F00025889c7;LABEL="oven"
      1. Get entity state:
      curl -X GET \
      -H "Authorization: Bearer ${HATOKEN}" \
      -H "Content-Type: application/json" \
      "http://proteus.lan:8123/api/states/${ENTID}"
      
      1. Use jq to form into Influxdb line protocol like energyv3,quantity=electricity,uniqueid=${DEVID},type=consumption,source=${LABEL} value={energy} {timestamp}:
      curl -X GET \
      -H "Authorization: Bearer ${HATOKEN}" \
      -H "Content-Type: application/json" \
      "http://proteus.lan:8123/api/states/${ENTID}" | \
      jq 
      
      Need: .state & .last_changed

Method 3 - use plugwise-2-py (success)

This method works and can broadcast data to Home Assistant (etc.) via MQTT.

  1. Create network using official Plugwise utilities (‘Source’ for Windows)
  2. Get Plugwise-2-py source (github.com)
  3. 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.
  4. Setup MQTT broker (digitalocean.com), e.g. mosquitto (mosquitto.org)
  5. Configure setup
    1. See README (github.com) for instructions
    2. 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"
    }
    
    1. Configure pw-conf.json. N.B. set loginterval 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"
        }
      ]
    }
    
    1. 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"
    }
    
  6. Run daemon python3 Plugwise-2.py
  7. Check for errors
    1. In log file: tail -f /var/log/pwlog/pw-logger.log
    2. In mosquitto: mosquitto.sub -h 127.0.0.1 -u plugwise-2-py -P password -t "plugwise2py/#"
  8. Integrate in Home Assistant
    1. Add MQTT integration (home-assistant.io)
    2. 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
    
    1. Ideally, one would use Home Assistant MQTT discovery (home-assistant.io), but the state topic formatting is not compatible here
  9. Profit!

Automate in Home Assistant

  1. Build automations to check when devices are running
    1. 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
    
    1. 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
    
    1. 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
    

Sources

  1. https://roheve.wordpress.com/tag/plugwise/ (wordpress.com)
  2. https://maartendamen.com/tag/plugwise/ (maartendamen.com)

#Server #Unix #Smarthome