Collecting Summarised Statistics

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 states table - purged, config via recorder.yaml
  • History - uses statistics_short_term - purged, config via recorder.yaml. These metrics are defined in configuration.yaml
  • Statistics - uses statistics table (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

To clean those up, delete the database and let it start over.

Do you mean the table … or is it a completely separate database?
I was thinking dropping info more than 18 months old, but I guess that’s a problem for future me!

I just delete my database file every year or so. It will rebuild. All the data is gone, but the graphs repopulate with the new data and I don’t care about any of the rest of it.

gotcha. I do want to keep some stuff, so I will look into selective clean-up in the future.

The LTS doesn’t take very much room, but it’s there until you delete the database. AFAIK.

1 Like