Battery degradation calculation automatic #3926
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
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_autoconfig handling and default state. - Adds battery size tracking sensors/history and
soc_kwsupport infind_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_maxmakes 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_kwconversion prevents auto degradation detection for users who provide absolute kWh SoC. The derived percentage becomessoc_kw / nominal_capacity, sofind_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(...)oversoc_kw_data[0]converts every history state before any validation, so a common HA history value likeunknown,unavailable, or an empty history list will raise and abort inverter initialization. The later per-statetryblock 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_maxbiases 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 applyingdp0().
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
| # 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) |
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.