Thanks for this! I used this as a skeleton to bring in my electric and gas consumption from [1]. I adapted this for use with influxdb2 and flux. It pulls the last value in the most recent hour.
The value_template
entries are partly cosmetic - handling them as floats means there’s too many decimals places displayed. But my electric meter sends deciwatt-hour readings (instead of watt-hour or kilowatt-hour) even though it’s screen reads kilowatt-hours. Go figure.
sensor:
- platform: influxdb
api_version: 2
host: <influx host>
port: <influx port>
token: <influx token>
organization: <influx organization id>
bucket: rtlamr
queries_flux:
- name: "Electric meter reading"
range_start: "-1h"
unit_of_measurement: kWh
value_template: "{{ (value|float / 100.0)|round(2) }}"
query: >
filter(fn: (r) => r["_measurement"] == "utilities")
|> filter(fn: (r) => r["_field"] == "consumption")
|> filter(fn: (r) => r["endpoint_id"] == <endpoint id>)
|> filter(fn: (r) => r["endpoint_type"] == "7")
|> filter(fn: (r) => r["msg_type"] == "cumulative")
|> filter(fn: (r) => r["protocol"] == "IDM")
|> last(column: "_value")
|> keep(columns: ["_value"])
- name: "Gas meter reading"
range_start: "-1h"
unit_of_measurement: ft³
value_template: "{{ value|float|round(2) }}"
query: >
filter(fn: (r) => r["_measurement"] == "utilities")
|> filter(fn: (r) => r["_field"] == "consumption")
|> filter(fn: (r) => r["endpoint_id"] == <endpoint id>)
|> filter(fn: (r) => r["endpoint_type"] == "12")
|> filter(fn: (r) => r["msg_type"] == "cumulative")
|> filter(fn: (r) => r["protocol"] == "SCM")
|> last(column: "_value")
|> keep(columns: ["_value"])
[1] - GitHub - bemasher/rtlamr-collect: Data aggregation for rtlamr.