Calculate home-assistant_V2.db size without whitelisting entire config dir

Please DON’T USE constructs like these—they might return incorrect results (you’d need the page_count multiplied by page_size) and are horribly slow. Your version runs several minutes on my system (2.3GB DB) and returns ~648MB instead of 2.3GB.

Since sqlite v3.16, we have pragmas that return results and don’t have side effects. So here is my suggestion for improvement:

sensor:
  # HA database size
  - platform: sql
    # Not needed when using recorder, because we already have a connection.
    # Also, socket connections on Linux systems are MUCH faster.
    # Absolute path begins after the 3rd "/" (location for Hassbian).
    #db_url: sqlite:////home/homeassistant/.homeassistant/home-assistant_v2.db
    scan_interval: 300
    queries:
      - name: DB Size
        # This is the fastest solution, available since sqlite 3.16.
        # It’s the same info as returned by ".dbinfo" in the sqlite3 cmdline client.
        query: 'SELECT ROUND(page_count * page_size / 1024 / 1024, 1) as size FROM pragma_page_count(), pragma_page_size();'
        column: 'size'
        unit_of_measurement: 'MiB'

Hey, and thanks for the idea for another nice sensor! :slight_smile:

3 Likes