Long term statistics are very useful, because they allow you to keep your database size small while keeping relevant information for a long time. Currently long term statistics only work on numeric sensors, storing a min, max and avg value per hour.
I would like to see long term statistics on binary sensors, storing for how long a binary sensor was active in a given time frame. A window sensor could for example store a window-open-time and window-closed-time per hour. It would then be possible to add those up to calculate for example how long the window was open the last 4 weeks.
This could then also be extended to other stateful entities, such as persons, allowing me to check for example how much time I spent at the office in the last year.
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
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.
Introduce a New Table in the Database (or ExtendstatisticsTable)
Store timestamped transitions for binary_sensor entities.
This should work similarly to how recorder stores history, but without purging based on purge_keep_days .
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.
If a full-fledged implementation is complex or not a priority, it would still be great to have an option to store binary sensor state changes without associated metadata , as a lightweight alternative.