https://github.com/brunob45/ha-db-cmds
Useful commands to handle the Home Assistant Database
https://github.com/brunob45/ha-db-cmds
database home-assistant self-hosted sqlite sqlite3
Last synced: about 2 months ago
JSON representation
Useful commands to handle the Home Assistant Database
- Host: GitHub
- URL: https://github.com/brunob45/ha-db-cmds
- Owner: brunob45
- Created: 2025-02-23T19:33:20.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-23T19:49:59.000Z (over 1 year ago)
- Last Synced: 2025-02-23T20:29:43.469Z (over 1 year ago)
- Topics: database, home-assistant, self-hosted, sqlite, sqlite3
- Homepage:
- Size: 0 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ha-db-cmds
Useful commands to handle the Home Assistant Database
> [!IMPORTANT]
> Always shutdown Home Assistant before editing the database.
## Valdiate statistics values
```sql
SELECT start_ts, state, sum, round(SUM(state) OVER (order by start_ts), 2) AS true_sum
FROM "statistics"
WHERE metadata_id = 132
ORDER BY start_ts DESC
```
## Fix statistics values
```sql
UPDATE statistics
SET sum = round(SUM(state) OVER (order by start_ts), 2)
WHERE metadata_id = 132 AND start_ts >= 1723604400.0
```
## List all unused state_attributes
I 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.
```sql
SELECT attributes_id
FROM state_attributes
WHERE attributes_id NOT IN (
SELECT attributes_id
FROM states
)
```
Then, I used this command to delete all the unused attributes.
```sql
DELETE
FROM state_attributes
WHERE attributes_id NOT IN (
SELECT attributes_id
FROM states
)
```
## Find the most used attributes for a given state
I don't remember why this was useful
```sql
SELECT COUNT(*) as count, states.attributes_id, shared_attrs
FROM states
JOIN state_attributes ON states.attributes_id=state_attributes.attributes_id
WHERE states.metadata_id=165
GROUP BY states.attributes_id
ORDER BY count DESC
```