Skip to content

fix(inverter): treat a 0W live rate limit as unset, so Predbat's own 0W write cannot latch the rate at 0 (#5219) - #5626

Open
springfall2008 wants to merge 5 commits into
mainfrom
fix/zero-rate-limit-curve-crash-5219
Open

springfall2008 wants to merge 5 commits into
mainfrom
fix/zero-rate-limit-curve-crash-5219

Conversation

@springfall2008

@springfall2008 springfall2008 commented Oct 19, 2026 •

Copy link
Copy Markdown
Owner

This is an automated draft PR generated from issue #5219 — a maintainer should review it before merging.

Fixes #5219

Summary

The Sunsynk template points inverter_limit_charge/_discharge at sensors that mirror the charge/discharge current Predbat writes. When a freeze export starts, Predbat writes 0W to charge_rate. On the next cycle refresh_config() reads that 0W back as the limit, so battery_rate_max_charge becomes 0. When this PR was opened, find_charge_curve() then divided by it and raised ZeroDivisionError, aborting update_pred() every cycle.

Update 16 Nov 2026 (Claude, for @chalfontchubby), after merging main: main has since removed find_charge_curve() (#6212); the rate grid learner that replaced it returns early when the maximum rate is 0, and the execute.py division mentioned below is gone too. So main no longer crashes on a 0W reading. The latch remains: with battery_rate_max_charge at 0 the plan cannot charge in that direction, and the 0W Predbat wrote keeps reading back as the limit, so the rate is never restored. This PR now fixes only that, and the find_charge_curve() guard and its test have gone with the function.

The change:

  • Inverter.refresh_config() (inverter.py:692): an inverter_limit_charge/inverter_limit_discharge reading of 0W or less is treated as no reading. The rate falls back to battery_rate_max and a Warn: line is logged. The apps.yaml schema already rejects zero for these keys ("zero": False), so a 0W value only reaches this code from a live sensor. This fixes the cause, and it also covers the second division on the same value that triage found in execute.py (inverter.battery_rate_max_charge / self.battery_rate_max_charge, now line 1213). inverter_limit_charge_dc and inverter_limit_export default from the corrected values, so they inherit the fix.

Testing

  • New test_zero_limit_sensor_falls_back (tests/test_inverter.py) builds an Inverter whose limit sensors read 0W and checks that the charge, discharge and export maximums fall back to battery_rate_max (5000W).
  • The test fails without the fix and passes with it. With inverter.py stashed, tools/triage_test.sh inverter failed with battery_rate_max_charge/_discharge/_export ... got 0W.
  • ./run_pre_commit passes: all hooks pass, and the quick suite it runs reports "All tests passed (4 slow tests skipped)".

Notes

  • Behaviour choice: a genuine BMS-driven 0W limit (for example, a cold battery refusing charge) is now planned at battery_rate_max rather than at 0. That is a real behaviour change, not only a crash fix. The divide-by-zero was in the charge-curve search, which ran only in auto mode or before a curve had been worked out. So on main at the time, an inverter with a fixed or already-computed curve did not crash on a 0W reading: it planned no charging in that direction, which is right while the battery really refuses charge. With this PR that case plans full-rate charging the inverter can't deliver, and logs a Warn: every cycle. In return, the PR fixes the self-latching case, where Predbat's own 0W write keeps battery_rate_max_charge at 0 so the rate is never restored. (Correction added by Claude on behalf of @chalfontchubby, who asked for this PR; the earlier wording said nothing that worked before changes.) If you'd rather such a reading pause planning in that direction, that needs a wider change, because several sites divide by these rates.
  • Blast radius: GitNexus impact() rates refresh_config HIGH. That is because it sits on the per-cycle update_pred/fetch_inverter_data path, not because of how far the change reaches. No signatures change, and behaviour changes only for a ≤ 0W limit reading (see Behaviour choice above for the cases where main did not crash). detect_changes() reports low risk.
  • The warning repeats on each cycle while the sensor reads 0W, for example about six times across a 30-minute freeze export. I left it at Warn: rather than adding once-only state.

🤖 Generated with Claude Code

…5219)

A limit sensor that mirrors the charge/discharge rate Predbat writes (the
Sunsynk template's max-charge-rate sensor) reads back Predbat's own 0W
freeze-export write, which zeroed battery_rate_max_charge and made
find_charge_curve divide by zero on every cycle, so no plan was ever made
to restore the rate. Treat a non-positive inverter_limit_charge/_discharge
reading as no reading, and skip the curve search when the rate is 0W.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@springfall2008 springfall2008 self-assigned this Oct 19, 2026
@springfall2008 springfall2008 added BOT_REVIEW Trigger an autotriage BOT_CLEANUP Trigger: bot should address PR review feedback and CI failures, then commit and push labels Oct 19, 2026

@springfall2008 springfall2008 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Automated comment from the triage bot.

Reviewed head commit 11c5c16 (fix(inverter): don't crash the plan when a live rate limit reads 0W (#5219)): 1 finding posted inline, 0 in the body. Overall the fix addresses the root cause the way the GH#5219 triage recommended (treat a <=0W live limit reading as "no reading" at refresh_config(), plus a <=0 guard in find_charge_curve()), and the PR body's claims check out against the code.

What I verified as correct:

  • One computation site: Inverter.__init__ (inverter.py:436) calls refresh_config(), and battery_rate_max_charge/_discharge/_export are assigned only at inverter.py:709-713, so the guard at lines 697-702 covers the only path that can zero them from a limit read. The fallback (battery_rate_max_raw) is already assigned at that point (lines 666-672).
  • The PR's claim that the second division site is covered: adjust_battery_target_multi divides the per-inverter rate by the fleet sum assigned at execute.py:1433/1454, so a single-inverter install - the reporter's - sums to exactly the zeroed rate and the division is 0/0. The guard removes that.
  • {} from find_charge_curve() is handled by its caller: execute.py:1370-1398 falls back to battery_charge_power_curve_default or sets computed_*_curve in non-auto mode; auto mode keeps retrying (the normal auto-mode signature per the debug journal).
  • The schema claim is right: zero: False on all four limit keys (config.py:2710-2713) means a 0W value can only reach refresh_config() from a live sensor, so the guard never overturns an apps.yaml value.
  • None <= 0 is not reachable: get_arg() with a float default guarantees a float (userinterface.py:326-335), which is also what min() on main received.
  • Both new tests fail without the fix by code reading (no code executed, per review rules): test_zero_limit_sensor_falls_back fails because min(0, 5000)/MINUTE_WATT is 0, not 5000; test_find_charge_curve_zero_rate reaches the pre-fix division via the same fixture (create_test_history_data) the sibling curve tests use to reach it. The MINUTE_WATT roundtrip in the assertions is consistent (const.py:127, 60000), both tests restore my_predbat.args and dummy_items in finally, and both are registered in their run_* functions.
  • Docs need no change: docs/apps-yaml.md describes the four limit keys but makes no zero-handling statement, and this change alters no setting, entity or default.

The disclosed behaviour choice (a genuine 0W limit reading is now planned at battery_rate_max with a per-cycle Warn:) is reasonable: pre-fix that state crashed every cycle, so nothing that planned correctly changes.

Comment thread apps/predbat/inverter.py Resolved
@springfall2008 springfall2008 removed the BOT_REVIEW Trigger an autotriage label Oct 19, 2026
…nd inverter_limit_export too (#5219)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@springfall2008 springfall2008 removed the BOT_CLEANUP Trigger: bot should address PR review feedback and CI failures, then commit and push label Oct 19, 2026
@springfall2008
springfall2008 marked this pull request as ready for review October 19, 2026 19:27
@chalfontchubby

Copy link
Copy Markdown
Collaborator

DECISION_NEEDED

Posted by Claude on behalf of @chalfontchubby.

What should Predbat do with a live charge or discharge limit that reads 0 W? Main crashes on it in the curve search (auto mode / no curve yet), but plans no charging when the curve is fixed. This PR replaces 0 W with battery_rate_max and logs a warning every cycle (see "Behaviour choice" in the description).

  • A: as this PR does: 0 W reads as the maximum, with a warning. Fixes the crash and the case where Predbat's own 0 W write latches; a genuine 0 W (for example a cold battery) is planned at full rate.
  • B: keep 0 W as 0 (plan no charging in that direction), and only guard the divisions so the curve search can't crash.
  • C: B, plus treat 0 W as the maximum only when Predbat wrote that 0 itself.
  • Other

Main (#6212) removed find_charge_curve() and its test file; the rate
grid learner that replaced it already returns early when max_power is
0. The PR's guard in find_charge_curve and its zero-rate test go with
them. The 0W live-limit fallback in update_status stays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chalfontchubby chalfontchubby changed the title fix(inverter): don't crash the plan when a live rate limit reads 0W (#5219) fix(inverter): treat a 0W live rate limit as unset, so Predbat's own 0W write cannot latch the rate at 0 (#5219) Nov 16, 2026
@chalfontchubby

Copy link
Copy Markdown
Collaborator

Posted by Claude on behalf of @chalfontchubby.

Going ahead with option A. The DECISION_NEEDED question from 23 Oct has had no response, so this PR now does A: a live inverter_limit_* reading of 0W or less is treated as unset and falls back to battery_rate_max, with a Warn: each cycle. I've removed the DECISION_NEEDED label. Please overrule in review if you prefer another option.

The options that were on the table:

  • A (taken): 0W means "no reading", so use the maximum. This fixes the latch: with the Sunsynk template, Predbat's own 0W write for a freeze export reads back as the limit and keeps the rate at 0. The cost is that a genuine 0W limit, such as a BMS refusing charge, is planned at full rate.
  • B: keep 0 as "no charging" and only guard the divisions. This does not fix the reported latch, because the rate is never restored.
  • C: B, plus 0 means "maximum" only when Predbat wrote the 0. This needs write-tracking against mirror sensors that lag.

Also changed by merging main (581e59e): main removed find_charge_curve() (#6212), and the rate grid learner that replaced it already returns early on a 0W maximum. So main no longer crashes on a 0W reading. The PR's curve guard and its test went with the function, and the title and description now cover only the latch fix. Pre-commit and the quick suite pass on the merge.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BOT_PR_OPENED Bot PR flow opened a draft PR for this ticket

Projects

None yet

Development

Successfully merging this pull request may close these issues.

find_charge_curve crashes with ZeroDivisionError when charge rate is 0W (e.g. during forced export), blocking all planning

2 participants