IoTaWatt / InfluxDB / Home Assistant Energy Panel

With the release of Home Assistant 2021.8 and the new home energy management system, it seemed obvious to integrate it with my IoTaWatt home energy monitor system. While I was able to scrape and innovate the solution together, I thought I would write up an end-to-end guide for how to perform this integration for others.

First of all, my strategy is to take data from IoTaWatt → InfluxDB → Home Assistant. The InfluxDB step is for two key reasons. One, I already had that set up for dashboarding with another system. But more importantly, I think it’s the right architecture here because the IoTaWatt device is a lightweight esp8266 that you don’t want to overwhelm with lots of data access. Sending it all to InfluxDB creates one appropriate time-series DB to read that data from for any other integrating system.

So, step #1; IoTaWatt configuration. I’m still using the InfluxDB 1.0 system, so I have my IoTaWatt → InfluxDB_v1 Uploader configured…

The critical parts of this configuration are:

  • The field-key is set to “$units”; this makes my field in InfluxDB either “Watts” or “Volts”. “Watts” will later be used in the Home Assistant configuration to read this data.
  • Only the raw “Watts” data is sent to InfluxDB.
  • The “Grid_Import”, “Grid_Export”, and “Solar_Total” calculations will be used, so you’ll want those or the equivalent.
  • Grid_Import is the amount of power coming in from the grid, and typically this is the sum of all the mains. If you have a solar system like me, then it’s the sum of the mains, max 0, so that negative values are not included.
  • Grid_Export is the power going out to the grid; it’s the absolute value of the negative mains.
  • Solar_Total is all the solar production.

Even if you don’t have a solar system (yet?), you could configure all of these InfluxDB uploads anyway and set the Solar to 0.

Alright, now you’ve got IoTaWatt → InfluxDB data going… how do you get that into Home Assistant?

Home Assistant (currently) requires the data to be imported into an entity with these properties:

  • The value should be an ever-accumulating amount of energy over time.
  • unit_of_measurement that is Wh or kWh
  • device_class that is energy
  • last_reset that is a UTC date when the accumulation of the value was last reset. In my case I’ll just accumulate forever, so I didn’t bother to set this to anything other than UTC time zero.
  • state_class that is measurement, which in the case of an energy device describes to Home Assistant that this will be an accumulating value.

Here’s the Home Assistant configuration I used to pull my fields into HA from InfluxDB:

sensor:
  - platform: influxdb
    scan_interval: 300
    api_version: 1
    host: # my InfluxDB IP address...
    port: 8086
    database: iotawatt
    queries:
      # For the "energy" ones, we'll be making them state_class=measurement;
      # for "energy" that's expected to be "accumulated energy consumption"
      # (https://developers.home-assistant.io/docs/core/entity/sensor/)
      # so we do an integral of the Watts for the entire iotawatt system time
      # really don't know what this will do if iotawatt starts losing data
      # in the past; might need to populate last_reset if that's the case, or
      # integrate the Watts data wth the hass-based integration (integral)
      # system.
      - name: Solar Energy
        unit_of_measurement: Wh
        where: '"input" = ''Solar_Total'''
        measurement: '"iotawatt"'
        group_function: integral
        field: '"Watts", 1h'
      - name: Grid Import Energy
        unit_of_measurement: Wh
        where: '"input" = ''Grid_Import'''
        measurement: '"iotawatt"'
        group_function: integral
        field: '"Watts", 1h'
      - name: Grid Export Energy
        unit_of_measurement: Wh
        where: '"input" = ''Grid_Export'''
        measurement: '"iotawatt"'
        group_function: integral
        field: '"Watts", 1h'

homeassistant:
  customize_glob:
    sensor.*_energy:
      device_class: energy
      last_reset: '1970-01-01T00:00:00+00:00'
      state_class: measurement

There’s a couple tricks going on here…

The “Watt” value in InfluxDB is being turned into “Wh” (watt-hours) by using the group_function integral, which calculates the area under the values. integral takes two arguments, the field and the time-period to integrate with; the field here gets set to "Watts", 1h which is internally rendered as integral("Watts", 1h). It’s a bit hacky as the field isn’t really a field, it’s two parameters. But it works.

The scan_interval is set to 5 minutes. The query against InfluxDB is going to read the entire history of the Watts field since the beginning of time when data was sent to it, integrate it, and return it. That’s a bit slow (takes 2-3 seconds-ish), so I figured a slower scan interval is appropriate because I don’t want my server being bombarded with this busy work. 5 minute intervals seem fine.

The customize_glob is used to set the device_class, last_reset, and state_class on the sensor entity that is created by the InfluxDB integration.

And… that’s about it. After that, the rest of the energy configuration can be done in the Home Assistant UI.

Hope this helps someone get their config up and running!

7 Likes

This is super useful. I also have my IoTaWatt exporting to Influx, though from memory I have it only measuring the actual measurements (voltage, hz, amperage) and then where-ever I need wattage I just did the math when getting it displayed.
I’m gonna dive back in and see if I can get it working with a combo of your guide and my different measurements.
I didn’t fancy having to get the IoTaWatt exporting through two different methods with its limited heap, and didn’t know where to start to get the info out of influx and into home assistant

1 Like