{"id":25903892,"url":"https://github.com/brunob45/ha-db-cmds","last_synced_at":"2026-05-04T22:37:51.842Z","repository":{"id":279107528,"uuid":"937740532","full_name":"brunob45/ha-db-cmds","owner":"brunob45","description":"Useful commands to handle the Home Assistant Database","archived":false,"fork":false,"pushed_at":"2025-02-23T19:49:59.000Z","size":0,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-23T20:29:43.469Z","etag":null,"topics":["database","home-assistant","self-hosted","sqlite","sqlite3"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/brunob45.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-23T19:33:20.000Z","updated_at":"2025-02-23T19:50:02.000Z","dependencies_parsed_at":"2025-02-23T20:29:48.348Z","dependency_job_id":"509263a3-b434-45b9-91d4-e258bfe0d6b2","html_url":"https://github.com/brunob45/ha-db-cmds","commit_stats":null,"previous_names":["brunob45/ha-db-cmds"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brunob45%2Fha-db-cmds","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brunob45%2Fha-db-cmds/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brunob45%2Fha-db-cmds/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brunob45%2Fha-db-cmds/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brunob45","download_url":"https://codeload.github.com/brunob45/ha-db-cmds/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241605820,"owners_count":19989612,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","home-assistant","self-hosted","sqlite","sqlite3"],"created_at":"2025-03-03T04:16:13.938Z","updated_at":"2026-05-04T22:37:51.814Z","avatar_url":"https://github.com/brunob45.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# ha-db-cmds\nUseful commands to handle the Home Assistant Database\n\n\u003e [!IMPORTANT]\n\u003e Always shutdown Home Assistant before editing the database.\n\n## Valdiate statistics values\n\n```sql\nSELECT start_ts, state, sum, round(SUM(state) OVER (order by start_ts), 2) AS true_sum\nFROM \"statistics\"\nWHERE metadata_id = 132\nORDER BY start_ts DESC\n```\n\n## Fix statistics values\n\n```sql\nUPDATE statistics\nSET sum =  round(SUM(state) OVER (order by start_ts), 2)\nWHERE metadata_id = 132 AND start_ts \u003e= 1723604400.0 \n```\n\n## List all unused state_attributes\n\nI had an issue with Tuya Zigbee devices connected via zigbee2mqtt, where the state_attributes would change on every update, increasing the size of the database dramatically. I used this command to find all unused attributes.\n\n```sql\nSELECT attributes_id\nFROM state_attributes\nWHERE attributes_id NOT IN (\n    SELECT attributes_id\n    FROM states\n)\n```\n\nThen, I used this command to delete all the unused attributes.\n\n```sql\nDELETE\nFROM state_attributes\nWHERE attributes_id NOT IN (\n    SELECT attributes_id\n    FROM states\n)\n```\n\n## Find the most used attributes for a given state\n\nI don't remember why this was useful\n\n```sql\nSELECT COUNT(*) as count, states.attributes_id, shared_attrs\nFROM states\nJOIN state_attributes ON states.attributes_id=state_attributes.attributes_id\nWHERE states.metadata_id=165\nGROUP BY states.attributes_id\nORDER BY count DESC\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrunob45%2Fha-db-cmds","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrunob45%2Fha-db-cmds","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrunob45%2Fha-db-cmds/lists"}