Sharing here how I have set up some statistics and what I learnt along the way to confirm my understanding from last few days.
My aim is to store hourly activity information (of parents) over the long term.
Overall it seems to be a 2-step process for each metric (templated forever counts + utility meter OR history stats + templated long term stat)
Is one way better than the other, or one more suited to counts and the other ‘on-time’?
TIA
Long Term Statistics are stored in a different table than short term stats described here Long- and short-term statistics | Home Assistant Data Science Portal. My understanding is that
- Activity - uses
statestable - purged, config viarecorder.yaml - History - uses
statistics_short_term- purged, config viarecorder.yaml. These metrics are defined inconfiguration.yaml - Statistics - uses
statisticstable (hourly summaries) - not purged … don’t know how to cleanup yet.
Config for a binary sensor binary_sensor.hall_pir
To create a count of triggers in templates.yaml these are long term as state_class is defined. This never resets so a Utility meter helper is needed to see it hourly.
- trigger:
- platform: state
entity_id: binary_sensor.hall_pir
to: 'on'
sensor:
- name: "Hall PIR Trigger Count"
unique_id: hall_pir_trigger_count
state: "{{ (states('sensor.hall_pir_trigger_count') | int(0)) + 1 }}"
state_class: total_increasing
unit_of_measurement: "count"
To create daily history of the time the PIR was active, in config.yaml (no state_class)
- platform: history_stats
name: "Hall Motion Time Today"
unique_id: hall_motion_time_today
entity_id: binary_sensor.hall_pir
state: "on"
type: time
start: "{{ today_at('00:00') }}"
end: "{{ now() }}"
To get these on-times into long term statistics, back to templates.yaml, again state_class sends them to long term stats.
- sensor:
- name: "Total Hall Motion Time"
#unique_id: hall_motion_time_longterm
# This pulls from your history_stats sensor
state: "{{ states('sensor.hall_motion_time_today') | float(0) }}"
unit_of_measurement: "h"
# This is for long-term storage
state_class: measurement
device_class: duration