Skip to content

Battery degradation calculation automatic #3926

Merged
springfall2008 merged 9 commits into
mainfrom
feat/battery_size_auto
May 16, 2026
Merged

Battery degradation calculation automatic #3926
springfall2008 merged 9 commits into
mainfrom
feat/battery_size_auto

Conversation

@springfall2008
Copy link
Copy Markdown
Owner

No description provided.

Copilot AI review requested due to automatic review settings May 16, 2026 09:14
@springfall2008 springfall2008 changed the title Feat/battery size auto Battery degradation calculation automatic May 16, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds automatic battery capacity tracking/scaling based on historical charging data, with documentation, configuration, charting, and unit coverage.

Changes:

  • Adds battery_scaling_auto config handling and default state.
  • Adds battery size tracking sensors/history and soc_kw support in find_battery_size().
  • Adds a BatteryDegradation chart and expands find battery size tests.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
docs/apps-yaml.md Documents the new automatic battery scaling option.
apps/predbat/web.py Adds a BatteryDegradation chart page/menu item.
apps/predbat/tests/test_find_battery_size.py Adds tests for soc_kw and auto scaling behavior.
apps/predbat/predbat.py Initializes the new auto-scaling flag.
apps/predbat/inverter.py Implements battery size tracking and soc_kw history conversion.
apps/predbat/fetch.py Reads battery_scaling_auto from config.
apps/predbat/config.py Adds schema support for battery_scaling_auto.
Comments suppressed due to low confidence (4)

apps/predbat/inverter.py:603

  • Persisting the 8 kWh fallback into soc_max makes the fallback look like a configured nominal capacity on later inverter initializations. If the first run has no usable history, subsequent runs will clamp auto sizing against 8 kWh and can never grow to the real capacity; the previous code only used the fallback in memory, so later history could still establish the real size.
        if not self.soc_max or self.soc_max <= 0:
            self.log("Warn: Unable to determine battery size for inverter {}, setting to 8 kWh default, you must set soc_max in apps.yaml or wait until enough data is collected to estimate battery size".format(self.id))
            self.soc_max = 8.0
            self.base.set_arg("soc_max", self.soc_max, index=self.id)
            self.base.set_arg("soc_max_nominal", 0, index=self.id)
            self.nominal_capacity = self.soc_max

apps/predbat/inverter.py:675

  • Passing a configured nominal capacity into the soc_kw conversion prevents auto degradation detection for users who provide absolute kWh SoC. The derived percentage becomes soc_kw / nominal_capacity, so find_battery_size() will estimate the nominal capacity rather than the measured usable capacity; it should still derive the percentage scale from the observed kWh history when the goal is to measure degradation.
                if nominal_capacity and nominal_capacity > 0:
                    soc_max = nominal_capacity
                else:
                    soc_max = max(float(dp0(float(state["state"]))) for state in soc_kw_data[0]) if soc_kw_data and len(soc_kw_data) > 0 else None

apps/predbat/inverter.py:675

  • The max(...) over soc_kw_data[0] converts every history state before any validation, so a common HA history value like unknown, unavailable, or an empty history list will raise and abort inverter initialization. The later per-state try block only applies after this maximum has already been computed.
                    soc_max = max(float(dp0(float(state["state"]))) for state in soc_kw_data[0]) if soc_kw_data and len(soc_kw_data) > 0 else None

apps/predbat/inverter.py:675

  • Rounding the observed maximum kWh value to a whole number before using it as soc_max biases the capacity estimate, especially for common fractional battery sizes (for example 5.2 kWh is treated as 5 kWh). The auto estimate should preserve the measured kWh precision instead of applying dp0().
                    soc_max = max(float(dp0(float(state["state"]))) for state in soc_kw_data[0]) if soc_kw_data and len(soc_kw_data) > 0 else None

Comment thread apps/predbat/inverter.py Outdated
Comment thread apps/predbat/tests/test_find_battery_size.py
Comment thread apps/predbat/inverter.py Outdated
Comment thread docs/apps-yaml.md Outdated
Comment thread apps/predbat/inverter.py
# Only calculate once per day to save compute resources
found_size = self.find_battery_size(self.nominal_capacity)
if found_size and found_size > 0:
trimmed_mean = self.update_soc_max_calculated_sensor(found_size, self.nominal_capacity)
springfall2008 and others added 4 commits May 16, 2026 14:59
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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.

2 participants