Skip to content

fix: restart-inflation & init-race

Choose a tag to compare

@hoizi89 hoizi89 released this 28 Apr 11:11

🐛 Critical Bug Fix

Fixes #8 reported by @Gremly84: after every HACS reload or HA reboot, total values made massive jumps upward across all modules.

Root cause

`_last_pv_production_kwh` / `_last_grid_export_kwh` / `_last_grid_import_kwh` / `_last_consumption_kwh` were not persisted in storage. After restart they defaulted back to 0, and the very first `state_changed` event from the inverter (e.g. 12,000 kWh total) was computed as delta = 12000 - 0 = 12000 and added to the restored totals.

Compounding: `async_start()` unconditionally overwrote the None defaults with 0 when the inverter integration was still unavailable at boot, disabling the defensive None-check in `_process_energy_update()` that would otherwise have re-baselined cleanly on the first valid reading.

Compounding: `_initialize_from_sensors()` also silently treated an unavailable `grid_export_entity` as export=0, leading to `self_consumption = pv_total` (same class of bug).

What changed

  • `get_state_for_storage()`: persists `last_pv_production_kwh`, `last_grid_export_kwh`, `last_grid_import_kwh`, `last_consumption_kwh`.
  • `restore_state()`: reads them back as `None` when never stored, so the defensive guard works on first boot after upgrade.
  • `async_start()`: only sets `last*` baselines from sensors that are actually available at boot, and only when restore left them at None.
  • `_process_energy_update()`: init guard now covers all three `last*` variables (was missing grid_export — would have crashed).
  • `_initialize_from_sensors()`: bails out if `grid_export_entity` is configured but unavailable.

✅ No breaking changes

  • Config flow unchanged
  • Existing storage data preserved
  • Users who already have inflated values from this bug can use the existing Reset functions in the Options to re-initialise

🩺 If your values are already inflated

If you saw the bug before updating: open Developer Tools → Statistics in HA and use Adjust sum on the affected sensors to bring them back to realistic values. Or use the Reset functions under integration Options.

Full Changelog: v4.0.1...v4.0.2