Skip to content

Fix soc_max_calculated 7-day history being wiped on every Home Assistant restart#4259

Merged
springfall2008 merged 3 commits into
mainfrom
copilot/fix-battery-scaling-history-issue
Jul 17, 2026
Merged

Fix soc_max_calculated 7-day history being wiped on every Home Assistant restart#4259
springfall2008 merged 3 commits into
mainfrom
copilot/fix-battery-scaling-history-issue

Conversation

Copilot AI commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

sensor.predbat_soc_max_calculated stores a 7-day rolling history whose trimmed mean feeds battery_scaling_auto/soc_max. That history lived only in the live entity's history attribute, which is ephemeral and not restored after a Home Assistant restart. Both read sites used the live-state-only accessor, so after any HA restart the history was silently reset to a single day, defeating the smoothing and causing soc_max/degradation readings to jump on noise alone.

Core fix

  • Inverter.battery_size_tracking() and Inverter.update_soc_max_calculated_sensor() (inverter.py) now read the history attribute via load_previous_value_from_ha() instead of get_state_wrapper(), so the recorder is used as a fallback when the live entity is missing:
existing_history = self.base.load_previous_value_from_ha(soc_max_sensor_name, attribute="history") or {}
...
history = self.base.load_previous_value_from_ha(sensor_name, attribute="history") or {}

This mirrors the pattern already used for savings_total_predbat and inverter_register_writes.

Test infrastructure

  • TestHAInterface.get_state hardcoded "" for missing entities when an attribute was requested, ignoring the default argument. This diverged from the real HAInterface.get_state (which correctly returns default) and made the "entity gone after restart, recorder still has it" scenario impossible to simulate, masking this exact bug class. Corrected to honor default.
  • Added test_battery_scaling_auto_history_survives_ha_restart in test_find_battery_size.py, which removes the live entity, mocks recorder history via get_history, and asserts the trimmed mean/soc_max are computed as if the history had survived the restart.

Copilot AI changed the title [WIP] Fix battery scaling auto history wipe on HA restart Fix soc_max_calculated 7-day history being wiped on every Home Assistant restart Jul 16, 2026
Copilot AI requested a review from springfall2008 July 16, 2026 18:58
@springfall2008
springfall2008 requested a review from Copilot July 17, 2026 08:15
@springfall2008
springfall2008 marked this pull request as ready for review July 17, 2026 08:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a Home Assistant restart edge case where sensor.*_soc_max_calculated’s 7‑day rolling history attribute was effectively treated as empty after restart, causing the trimmed-mean smoothing (and thus battery_scaling_auto / soc_max) to jump based on a single day.

Changes:

  • Switch Inverter.battery_size_tracking() and Inverter.update_soc_max_calculated_sensor() to read the history attribute via load_previous_value_from_ha() so recorder history is used when the live entity is missing.
  • Fix TestHAInterface.get_state() to honor default for missing entities even when an attribute is requested.
  • Add a regression test simulating “entity missing after restart but recorder history present” to ensure trimmed-mean behavior is preserved.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
apps/predbat/inverter.py Uses recorder-backed reads for soc_max_calculated history to preserve the rolling window across HA restarts.
apps/predbat/tests/test_infra.py Aligns test HA interface behavior with real HAInterface for missing-entity + attribute reads.
apps/predbat/tests/test_find_battery_size.py Adds regression coverage for history recovery after simulated HA restart.

Comment thread apps/predbat/inverter.py
@springfall2008
springfall2008 merged commit 2120998 into main Jul 17, 2026
2 checks passed
@springfall2008
springfall2008 deleted the copilot/fix-battery-scaling-history-issue branch July 17, 2026 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

battery_scaling_auto: the 7-day soc_max_calculated history is wiped by every Home Assistant restart, defeating the trimmed mean

3 participants