I have a simple need, and not sure which of the many history/statistics features to use to meet it.
I’d like to calculate some statistics on my grid outage (binary sensor).
- Total amount of outage minutes since the beginning of time
- Count of outages since the beginning of time
- Percentage outage since the beginning of time
- Total time, count, and percentage outage summarised per day, week, month, year
My first attempt at this kind of works, but requires storing a very long history in the recorder (multiple years) and this breaks because you can’t set the purge duration separately for different entities (so every state change is recorded over a long period)
I used the history_stats integration to calculate a rolling total like this:
sensor:
- platform: history_stats
name: Daily Grid Outage
entity_id: binary_sensor.assumed_grid_status
state: "off"
type: time
end: "{{ now() }}"
duration: "24:00:00"
This has two issues - it is also stored as state (not in the long term statistics), and I get lots of values, not one per day, week, month, etc.
It looks like the statistics integration does a similar thing (create a rolling statistic, rather than discrete summaries)
The long-term statistics feature seems to be the right endpoint, but maybe I need to turn the binary input into an increasing time value (kind like an energy meter)?
So what’s the best way to go from a binary sensor, to a long-term summarised set of statistics like this?
Bonus points if I can bacfkill the data using the existing binary state data.