Optimizing Home Assistant's Recorder Database for Performance and Longevity

Represent Optimizing Home Assistant's Recorder Database for Performance and Longevity article
3m read

Introduction: The Foundation of Home Assistant Performance

Home Assistant records nearly every state change and event, enabling history graphs, the logbook, and long-term statistics. This crucial task is handled by the recorder component. However, an unmanaged database can significantly degrade performance, leading to slow dashboards, sluggish automations, and increased resource usage, especially on modest hardware like Raspberry Pis.

Optimizing your Home Assistant database is essential for a reliable and responsive smart home. It's not just an advanced tweak; it's a fundamental step for ensuring long-term system health.

Understanding the recorder Component

By default, Home Assistant uses an SQLite database file (home-assistant_v2.db) in your configuration directory. The recorder writes all entity state changes and event data here. Understanding its configuration is key to efficient data management.

Key Configuration Options for Database Optimization

All recorder configurations are placed under the recorder: key in your configuration.yaml. Here are the most critical parameters:

1. Data Retention: purge_keep_days

This controls how many days of history Home Assistant retains (default often 10 days). Reducing this value is the simplest way to manage database size.

recorder:
  purge_keep_days: 7 # Keep 7 days of history

For most users, 7-14 days balances insights with performance. Resource-limited systems might even consider 3 days.

2. Selective Recording: include and exclude

This is the most powerful optimization. Not every entity's state changes are valuable for history. Many update too frequently (e.g., power sensors, network traffic) or provide transient data (e.g., button presses) that quickly bloats the database.

An exclude strategy is generally recommended: ignore noisy or irrelevant entities, recording everything else. An include strategy records only specified entities, useful for highly constrained systems.

Excluding Entities (Recommended)

Exclude domains, specific entities, or entities matching a glob pattern:

recorder:
  purge_keep_days: 7
  exclude:
    domains:
      - automation
      - script
      - updater
      - sun
      - weblink
    entities:
      - sensor.last_boot
      - sensor.processor_use
      - sensor.memory_free
      - sensor.disk_use
      - sensor.network_in
      - sensor.network_out
      - binary_sensor.motion_sensor_front_last_motion # Example: chatty sensor
    entity_globs:
      - sensor.pyscript_* # Exclude all Pyscript sensors

Tip: Focus on sensors with very frequent updates or many changing attributes. Consider excluding entities whose history you only check in the Logbook.

3. Automatic Purging: auto_purge

auto_purge (default: true) ensures Home Assistant automatically purges old data. It's generally best to leave this enabled.

recorder:
  auto_purge: true

commit_interval can be tweaked for advanced users, but default is fine.

4. External Databases (For Scale)

For very large installations, moving to an external database (MariaDB/PostgreSQL) via db_url can improve performance by offloading processing. However, the optimization principles of purge_keep_days and exclude remain crucial for data management within the external database itself.

recorder:
  db_url: mysql://ha_user:password@core-mariadb/homeassistant?charset=utf8mb4

Best Practices for a Reliable Smart Home Ecosystem

  1. Start Lean: When adding devices, consider if every sensor needs history recorded.
  2. Monitor Database Size: Track your database file size using an SQL sensor:
    sensor:
      - platform: sql
        db_url: "sqlite:////config/home-assistant_v2.db" # Or your external DB URL if configured
        queries:
          - name: Home Assistant DB Size
            query: "SELECT printf('%.2f', (page_count * page_size) / 1024.0 / 1024.0) FROM pragma_page_count(), pragma_page_size();"
            column: "printf('%.2f', (page_count * page_size) / 1024.0 / 1024.0)"
            unit_of_measurement: "MB"
            value_template: "{{ value | float }}"
            unique_id: home_assistant_db_size
            scan_interval: 3600
            
  3. Identify Noisy Entities: Use the "States" developer tool to spot frequently changing entities.
  4. Regular Purges: Home Assistant auto-purges, but a manual purge (recorder.purge service call or restart) can be beneficial after configuration changes.
  5. Backup Strategy: Always maintain a robust backup strategy for your entire Home Assistant configuration.
  6. Long-Term Statistics vs. History: Optimizing recorder generally doesn't affect long-term statistics (aggregated data), but avoid excluding entities vital for dashboards like Energy.

Troubleshooting Common Issues

  • Slow Performance: If performance is still an issue after optimization, check logs for database errors. Ensure your storage (SD card, SSD) is healthy.
  • Database Corruption: Though rare, SQLite databases can become corrupt. If errors occur, try deleting home-assistant_v2.db (after backing up your configuration!). Home Assistant will recreate it, but history will be lost.
  • Missing History: Verify your include/exclude and purge_keep_days settings.

Conclusion: A Smoother, More Efficient Smart Home

Optimizing Home Assistant's recorder database is a proactive step that enhances system performance and stability. By intelligently managing data retention and selectively recording information, you transform a potential bottleneck into an efficient component of your smart home. Invest time in these configurations for a snappier, more reliable Home Assistant experience.

Avatar picture of NGC 224
Written by:

NGC 224

Author bio: DIY Smart Home Creator

There are no comments yet
loading...