I have a sensor that updates irregularly. Sometimes once per minute, sometimes it can take several hours for a new value to arrive. I have a statistics sensor that tracks this value for 24 hours.
Given this (simplified version of an actual) situation:
The statistics value today at 08:00 reports 10. This is according to the documentation, it’s the difference between the first reading and the last reading within the range.
However, common sense says that the sensor value 24 hours ago is 30 so a more intuitive outcome of the statistics value would be 20. Hence this feature request: use the last reading before the time range instead of the first reading within the time range as the start value for the statistic.
I just want to know how much rain fell in the last 24 hours and in the last couple of days (e.g. 7 days).
If a lot of rain falls between 21:00 and 23:00 then the next day the garden is still wet, despite the daily total of zero. So I think a 24 hour period makes sense.
It is quite easy to do with an SQL sensor (PostgreSQL):
sensor:
- platform: sql
queries:
- name: Bresser51 DB Rain 24H
query: >-
SELECT * FROM states
WHERE entity_id = 'sensor.bresser51_rain'
AND last_updated < CURRENT_TIMESTAMP - INTERVAL '24 hour'
ORDER BY last_updated DESC
LIMIT 1;
column: state
unit_of_measurement: mm
scan_interval: 120
Unfortunately defining SQL sensors in YAML is now deprecated and removed and the definitions are imported into the UI. This has several disadvantages, e.g. not being able to change the poll frequency anymore.