I had the same requirements as the OP and went for a database solution since I already had Influxdb installed. Using the influxdb sensor, you can specify a query to run against the database and have the database engine calculate min/max/averages/percentiles/etc rather than HASS . It was a bit of a pain to find out all necessary fields for the query but once that was done it was quite simple to transfer the query to the sensor.
That sounds good! I have influxdb + grafana as well! I didnt know about influxdb sensor. It seem as the right thing! Any recomendations/pit falls?
Thank you.
I used the CLI of Influx to figure out the query, Influx is a bit different from the SQL databases I have used before.
Start the Influx CLI by typing influx in a terminal window, then select the correct database: use show databases followed by use database_name (that’s use homeassistant if you haven’t specified your own database name in the influx component declaration). show measurements to see available measurement series, in my case “°C”, and then show series to see available entities (in my case temperature_outdoor).
All this combines to a query, in my case: SELECT median(values) FROM "°C" WHERE entity_id='temperature_outdoor' AND time > now()-24h
This query calculates a median value of all the temperature measurements from my outdoor thermometer recorded the last 24h and returns a single float value.
NB! The single quotes around temperature_outdoor in the query are very important, without them there will be no answer! You’ll have to escape them in the sensor declaration using more quotes, check the docs.
- platform: influxdb
host: localhost
queries:
- name: "Outdoor median"
unit_of_measurement: °C
value_template: '{{ value | round(1) }}'
field: value #select
group_function: median
measurement: '"°C"' #from
where: '"entity_id" = ''temperature_outdoor'' and time > now() - 12h' #where
I have also discovered that by replacing entity_id with friendly_name_str, one can use a sensor’s friendly name instead: "friendly_name_str" = ''outdoor_temperature_sensor_friendly_name''