Energy for individual devices: pulling Wh from Influxdb2

Background: I have just installed an IotaWatt and am monitoring the power consumption on many individual circuits. After reading the forums, I followed @mfenniak’s advice and am sending all data from the IotaWatt directly to my InfluxDB2 container. Currently I am sending both the W for each circuit as well as the Wh.

My next step is to import this data into HA so that I can add the monitored circuits as “Individual devices” on the Energy dashboard. I have read through many forum posts and the documentation, but am still unclear as to how best to do this.

I believe I need to create a sensor for each monitored circuit that queries influx for the Wh. As I understand it, this sensor needs to be ever-increasing, i.e. the value is the total Wh consumed since the beginning. Is this correct? It seems to me that this will eventually hit some sort of limit, or continue to get slower as each subsequent query has to pull in more data.

My one thought on this is that the utility meter integration appears to reset daily/weekly/monthly. Could this be a better way of accomplishing this?

Would love any guidance on this.

Not really. See Sensor Entity | Home Assistant Developer Docs for a description of state_class

If the state_class is “total_increasing”, HA will interpret a decrease of the value as the start of a new cycle, without the need of a utility_meter

Thank you for this. I’ve updated my config, but believe I will need to wait >24h to see the result on the energy dashboard.

For completeness, here is my config for an individual influx query:

customize_glob.yaml

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

sensor.yaml

- platform: influxdb
  api_version: 2
  ssl: false
  host: localhost
  port: 8086
  token: !secret influx_token
  organization: MYORG
  bucket: iotawatt
  queries_flux:
    - name: "basement fridge energy"
      unit_of_measurement: kWh
      range_start: "1970-01-01T00:00:00+00:00"
      query: >
        filter(fn: (r) => r["_measurement"] == "IWatt")
        |> filter(fn: (r) => r["_field"] == "Wh")
        |> filter(fn: (r) => r["input"] == "basement_fridge")
        |> sum()
      value_template: "{{ value|float / 1000 }}"

Where you able to get this working? I notice you used “MYORG”, which would be the 16digit character, but otherwise I’m trying very similar to get values from Influx.

However, for me it is not working (the sensor is not even appearing).

While, however, I am able to write HA to custom bucket in Influx.

The thing is I want some of my server stats to show in HA as a sensor value (disk usage of Unraid e.g.).

This is my sensor.yaml

- platform: influxdb
  api_version: 2
  host: 192.168.2.144
  port: 8086
  token: !secret influxdb_token
  organization: 6cc1f888c05bc70e
  bucket: unraid
  queries_flux:
    - name: "UnraidDisk"
      #range_start: "-1h"
      #unit_of_measurement: "%"
      #value_template: "{{ (value|float / 100.0)|round(2) }}"
      query: >
        filter(fn: (r) => r["_measurement"] == "disk")
        |> filter(fn: (r) => r["_field"] == "used_percent")
        |> filter(fn: (r) => r["device"] == "shfs")
        |> filter(fn: (r) => r["fstype"] == "fuse.shfs")
        |> filter(fn: (r) => r["host"] == "unraid")
        |> last(column: "_value")
        |> keep(columns: ["_value"])

Not able to get this even appearing in the frontend. I’m running HA in docker (host mode) BTW.