Setting up Influx DB sensors for complex queries

I have a Tesla and I’ve been using HA to track my battery use, the charge/discharge rate, etc. I’ve been visualizing them in Grafana but I’d like to get some of the stats back into HA as new sensors to use as automations. I’m trying to convert an InfluxDB query into a sensor in HA but I’m having a little trouble converting between the two syntaxes.

I have a visualization for an influx query of:

SELECT difference(mean("value")) FROM "value" WHERE ("entity_id" = 'tessa_battery') AND $timeFilter GROUP BY time(1h) fill(linear)

I’d like to get the last value of that query as a sensor in HA but I don’t see how it maps to the fields in the docs.

   -  name: Min for last hour
      unit_of_measurement: '%'
      value_template: '{{ value | round(1) }}'
      group_function: min
      where: '"entity_id" = ''salon'' and time > now() - 1h'
      measurement: '"%"'
      field: tmp
      database: db2

I see how I’d adjust the where clause. But I don’t see how the field, measurement, and group_function map to the SELECT, FROM, and GROUP BY fields.

Can anyone help me understand what I’m missing. I found a lot of info about getting data into Influx for visualization but not as much about using the InfluxDB stats to get data back out.

1 Like