Home Assistant Power Monitoring

I’ve operated a relatively simple Home Assistant installation for the past 3 or so years, for the longest time it comprised of a few Hue bulbs, a hub, and a very slow Optiplex FX160 running Home Assistant and a few other services. Much like any small pet project the scope creeped into its current form:

  • 20 Hue devices
  • 9 IKEA Tradfri devices.
  • 3 Generic Zigbee sensors.
  • 6 ESPHome Smart Plugs.
  • 1 ESPHome “busylight”.
  • 2 ESPHome hacked IKEA air quality sensors.
  • 3 Apple Home Pods (recently ousting some Echo devices).

My home is quite automated. While people may think its a “Internet of Shit” for bored techies, it actually keeps us from doing dumb stuff like leaving the 3KW heater in the living room on all night. This simple automation actually pushed me to investigate what other power savings could be had using Home Assistant and a few extra items.

In August 2021 the Home Assistant team added the Energy Dashboard, a tool to give a single dashboard view of energy consumption and generation. The dashboard is primarily designed for those people who have home generation setups, such as a PV installation, but it is useful for tracking energy usage of the average home. After my experience with taming the living room heater I decided to try and get as many items in the house into this dashboard, and then cry at the true energy usage of the household.

The update and Energy dashboard makes use of sensors to track power usage, the key one is the energy device class, and the unit of kwh. If your device or integration produces a sensor with those two attributes then it will be available on the energy dashboard. Unfortunately, very few devices provide this and some adjustments and additional tweaks to Home Assistant are required to make them visible.

ESPHome Devices

I currently use two types of smart plugs in the home, but both types are ESP8266 based Sonoff clones. They include a hlw8012 chipset that allows for power sensing and provide amps, watts, and voltage to ESPHome without much configuration. The ESPHome framework has a software sensor called “Total Daily Energy” that can use these values and output a sum total kWh sensor that Home Assistant can use for the dashboard.

sensor:
  ...
  - platform: total_daily_energy
    name: "${friendly_name} kWh"
    power_id: power_consumption_watt
    filters:
      - multiply: 0.001
    unit_of_measurement: kWh

In this case, power_consumption_watt is a common name I use for a sensor that outputs the current usage in watts. For the plugs it uses the hlw8012, but for other devices that don’t have power sensing I make use of a static sensor:

interval:
  - interval: 1min
    then:
      - sensor.template.publish:
          id: power_consumption_watt
          state: $power_consumption

sensor:
  - platform: template
    name: "${friendly_name} Power Usage"
    id: power_consumption_watt
    device_class: power
    state_class: measurement
    unit_of_measurement: W
    accuracy_decimals: 4
    filters:
      - heartbeat: 60s

Every minute the interval timer publishes the value defined as power_consumption to the ID power consumption_watt, and that ID is defined as a Template sensor providing watts. The value of power_consumption is defined in the per device configuration as a substitution and is based on monitoring the device with a external power monitor (or another smart plug).

substitutions:
  devicename: bedroom_air_quality
  friendly_name: Bedroom Air Quality
  device_description: Bedroom AQ Monitor
  power_consumption: "0.5696"

IPMI Servers

For my sins I run a two cluster vSphere homelab and a TrueNAS storage server, this small stack likes to guzzle power and getting it visible within Home Assistant was a small challenge. I use Prometheus as my metrics tracking and I currently harvest IPMI sensor information into the database already, as part of the sensors it includes a current watt usage of the system. The issue I had is that Home Assistant doesn’t have any in-built component to pull this data from Prometheus.

Thankfully, the community provided one. A user on Github, lfasci, created homeassistant-prometheus-query which gives the ability to run simple PromQL against your Prometheus instance and import it into Home Assistant. The component has a few issues and is very much developed for the “happy path”, but after some trial and error I was able to create a sensor in Home Assistant with the following:

- platform: prometheus_query
  name: Anshar Power Usage
  unique_id: anshar_power_usage
  prometheus_url: http://prometheus-server.monitoring
  prometheus_query: ipmi_sensor_value{name="pwr_consumption",server="anshar-idrac.int.doofnet.uk"}
  unit_of_measurement: W
  state_class: measurement
  device_class: power

- platform: integration
  source: sensor.anshar_power_usage
  name: Anshar Power Usage kWh
  unit_prefix: k
  unit_time: h
  round: 2

The second part of the puzzle is the “integration” platform. This will take the tracking Watt value and covert it to kWh. This works by calculating the total value when source sensor changes, so if your sensor is very stable and doesn’t change too much you could have very spikey results from the platform.

One issue to point out, if your experimenting with your sensor query and it returns null or invalid data, you may have issues using the integration plugin later. Once the data is stored in the recorder integration it’ll base its data off what is available in there, so if for example your unit of measurement is wrong then the resulting sensor will also be wrong.

Hue & IKEA Tradfri

At the time of writing (2022-01-05), the configuration I have (using zigbee2mqtt) doesn’t support providing any sort of power usage stats. I don’t expect this to change in anyway in the near future, but it may be possible to add fake sensors, much like the hacked air quality sensors, to Home Assistant to produce the required kWh output. You could potentially measure a bulb’s wattage using an external tool, then publishing watt values depending if the bulb is on or off in Home Assistant. This feels like it would be best done by a Z2M Extension, or even something that uses MQTT to pull the state and publish the wattage values.

For the moment, that gives some per device statistics and integrates it nicely into the Home Assistant energy panel.

Power Usage

My next project is to look into monitoring the meter itself to get a total household usage into Home Assistant, but that will be another post.