v1.4.0 - Fix daily re-overwriting bug
fix: Resolve daily re-overwriting bug and add automatic migration to v1.4.0
This release fixes a critical bug that caused anomalous cumulative value jumps
at month boundaries and implements automatic migration to correct historical data.
Root Cause
The integration runs daily and receives data from "first of last month" to "today"
from Enel API. The old implementation re-saved ALL days (including old ones) every
time with progressively increasing offsets, causing values to compound incorrectly.
Example: September 1st value increased by ~20kWh every day during October,
resulting in jumps of 15,000-200,000 kWh at month boundaries.
Changes
sensor.py - Core Fix
- Add timestamp filtering to save only new data points
- New method
get_last_statistic()returns (timestamp, cumulative_kwh) tuple - Filter data by comparing
point["timestamp"] > last_timestamp - Skip save operation entirely if no new data exists
- Prevent re-overwriting of existing statistics (UPSERT protection)
init.py - Automatic Migration
- Implement config entry migration from version 1 to version 2
- Detect anomalous jumps (>1000 kWh) between months in historical data
- Recalculate continuous cumulative values preserving daily deltas
- Create automatic JSON backup before migration at
/config/.storage/enelgrid_backup_<pod>_v1.json - Apply corrections to both consumption (kWh) and cost (EUR) statistics
- Migration runs once automatically on first load after upgrade
Supporting Files
- config_flow.py: Bump VERSION from 1 to 2 to trigger migration
- const.py: Fix CONF_POD and CONF_USER_NUMBER definitions
- manifest.json: Update version to 1.4.0
Documentation & Tools
- UPGRADE_1.4.0.md: Complete upgrade guide with verification queries
- verify_migration.sh: Automated verification script with auto-detection
- restore_backup.py: Backup validation and restore utility
Testing
Validated in Docker HA environment:
- First run (DB empty): Saves all historical data correctly
- Subsequent runs: Detects existing data, skips re-save ("No new data to save")
- Cumulative values remain stable across multiple daily executions
- No anomalous jumps observed
Fixes #9