Long-Term Statistics for Binary Sensors – Preserve State Changes Without Aggregation

Summary

Home Assistant’s current long-term statistics mechanism does not support binary sensors (binary_sensor.* ). This leads to data loss for discrete sensor state changes (0 → 1 , 1 → 0 ) over time, as they are not stored in the compressed long-term statistics format. Given that binary sensor data takes up minimal storage, it should be preserved in its original form without aggregation.

Current Issue

  • Binary sensors are excluded from long-term statistics: The current implementation only supports numeric sensors. Binary state changes are stored in short-term history (recorder ), but are deleted based on purge_keep_days settings.
  • Loss of historical binary sensor transitions: Since binary sensor data is only kept in recorder , transitions (0 → 1 , 1 → 0 ) are lost once history is purged.
  • Inefficiency of aggregating binary data: Unlike numeric values, which benefit from downsampling, binary sensor states should not be aggregated because their transitions are the most important data. Without them, valuable event history is lost.
  • Workarounds are inefficient:
    • Increasing purge_keep_days bloats the database.
    • Storing binary states as numeric (0/1 ) in long-term statistics results in transition loss due to time-based downsampling.
    • External solutions like InfluxDB or logging to a file do not integrate well with Home Assistant’s built-in history and UI components.

Proposed Solution

  1. Extend Long-Term Statistics to Support Binary Sensors
  • Implement a dedicated storage format for binary sensor transitions that preserves 0 → 1 and 1 → 0 events without aggregation.
  • Ensure that binary sensors are stored in statistics in their original state instead of being subject to numeric downsampling rules.
  1. Introduce a New Table in the Database (or Extend statistics Table)
  • Store timestamped transitions for binary_sensor entities.
  • This should work similarly to how recorder stores history, but without purging based on purge_keep_days .
  1. Ensure Compatibility with Existing UI Components
  • Allow binary sensor transitions to appear in history , logbook , and statistics graph card .
  • Ensure that transitions remain queryable over long periods, just like long-term statistics for numeric sensors.

Benefits

  • Preserves binary sensor data indefinitely without excessive database growth.
  • Eliminates the need for inefficient workarounds (e.g., converting binary sensors to numeric values, relying on external databases).
  • Enhances analysis and historical tracking of discrete events (e.g., motion detection, door openings, switch toggles).
  • Brings binary sensor data storage in line with best practices in industrial SCADA systems, where all state changes are preserved.

Conclusion

This feature would ensure that Home Assistant provides robust and reliable long-term tracking for discrete events, making it more suitable for automation, security monitoring, and industrial use cases. We request that developers consider implementing this enhancement in a future release.

Here is also WTH topic regarding this: WTH are there no long term statistics for binary sensors?