Track battery percentage history; skip poll write when value unchanged

This commit is contained in:
2026-04-13 21:15:19 -05:00
parent 279a1f3f3e
commit d7ba64a2f3
6 changed files with 230 additions and 7 deletions
+37
View File
@@ -1,5 +1,42 @@
# Schema Migrations
## Adding battery_pct_log table
This table was added to track battery percentage change history.
**Always snapshot first:**
```bash
cp batteries.db batteries.db.$(date +%Y-%m-%d).snapshot
```
**SQLite (Python):**
```python
import sqlite3
conn = sqlite3.connect('batteries.db')
conn.execute('''CREATE TABLE IF NOT EXISTS battery_pct_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
battery_id INTEGER NOT NULL REFERENCES battery(id) ON DELETE CASCADE,
percentage INTEGER NOT NULL,
recorded_at VARCHAR(19) NOT NULL,
source VARCHAR(10)
)''')
conn.commit(); conn.close()
```
**MariaDB / MySQL:**
```sql
CREATE TABLE IF NOT EXISTS battery_pct_log (
id INT AUTO_INCREMENT PRIMARY KEY,
battery_id INT NOT NULL,
percentage INT NOT NULL,
recorded_at VARCHAR(19) NOT NULL,
source VARCHAR(10),
CONSTRAINT fk_bpl_battery FOREIGN KEY (battery_id) REFERENCES battery(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
---
## Adding Home Assistant fields (ha_entity_id, battery_percentage)
These columns were added in the Home Assistant integration feature. Existing databases