From d17f15b33ca6bdc516c087bbbfc780e5e8b757ab Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 19:01:36 +0100 Subject: [PATCH] EV-20 SPIKE-06: stuck start cap backs off instead of holding Under one stuck verdict Solar still makes at most MAX_TRIES starts and gives one CHARGER_REFUSED; then one more start after 15 min and one every 30 min (each by the start rule and the write clock), so a fault that clears is found again. The episode ends when the reading follows a start, on a press of Solar, Off, Manual, shadow or a stop. Rows: cap, backoff times, recovery, press, episode end; T28 (b) no flapping. DOCS and DECISIONS. Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01VSY9UpTaUYnjJF5oVtbM1w --- .../DECISIONS/EV-20-solar-mode.md | 5 +- goodwe_controller/DOCS.md | 5 +- goodwe_controller/app/carmode.py | 48 +++++-- goodwe_controller/test_ev20_exits.py | 6 + goodwe_controller/test_ev20_rig.py | 118 +++++++++++++----- 5 files changed, 135 insertions(+), 47 deletions(-) diff --git a/goodwe_controller/DECISIONS/EV-20-solar-mode.md b/goodwe_controller/DECISIONS/EV-20-solar-mode.md index 3407b49..a844e3b 100644 --- a/goodwe_controller/DECISIONS/EV-20-solar-mode.md +++ b/goodwe_controller/DECISIONS/EV-20-solar-mode.md @@ -130,7 +130,10 @@ one small set of facts and derives every path from them: own may stand (its own 0 A stands, or Solar was just chosen), a stuck verdict no longer holds Solar: it writes its 0 A first over a current that is not its own, then the start rule runs; at most MAX_TRIES starts per - verdict, then CHARGER_REFUSED until Solar is pressed. Its write moves the reading (the verdict clears) or is + verdict, then one CHARGER_REFUSED, then one more start after 15 min and + one every 30 min (PM decision: no hold until a press). The episode ends + when the reading follows a start, on a press of Solar, Off, Manual, shadow + or a stop (`_stuck_end`). Its write moves the reading (the verdict clears) or is refused (EV-04's retries, then CHARGER_REFUSED). - **No amp ping-pong:** after a step down, a step back up needs `UP_HYST_W` (100 W) past the amp boundary. diff --git a/goodwe_controller/DOCS.md b/goodwe_controller/DOCS.md index 0990a0e..18f080d 100644 --- a/goodwe_controller/DOCS.md +++ b/goodwe_controller/DOCS.md @@ -3361,7 +3361,10 @@ changes at most once per 30 seconds and only by 1 A or more. stopping the add-on each set the charger to 0 A once. A 0 A the charger does not take is tried again after a minute, three times, then an amber row says so. After a restart, or when the charger or Home Assistant answers again, - Solar sets 0 A first and then starts by its own rules. At a stop of the + Solar sets 0 A first and then starts by its own rules. After a stuck + charger reading, Solar tries to start again by its start rule; after three + tries an amber row says so, and it tries once more after 15 minutes, then + every 30 minutes, until the reading follows. At a stop of the add-on the charger's 0 A goes out before the batteries go to 0 W, for at most 15 seconds; if the charger does not take it, the notice *Car may still be charging* follows. diff --git a/goodwe_controller/app/carmode.py b/goodwe_controller/app/carmode.py index 9f2302b..7555685 100644 --- a/goodwe_controller/app/carmode.py +++ b/goodwe_controller/app/carmode.py @@ -64,6 +64,8 @@ PHASE_GAP_S = 600.0 # at most one phase switch per this WRITER_S = 30.0 # a differing read-back this long is another writer RETRY_S = 60.0 # a write the charger did not take, tried again after MAX_TRIES = 3 # ... this many times, then it waits for the user +STUCK_BACKOFF_FIRST_S = 900.0 # Solar under a stuck verdict, its starts used +STUCK_BACKOFF_S = 1800.0 # up: one more after 15 min, then every 30 min TOL_A = CH.TOOK_TOL_A @@ -717,6 +719,8 @@ class SolarWriter(ManualWriter): self.other_at = None # C6: Manual's latest non-zero write self.on_owed = None # C1: told when an own 0 A is out / back self.stuck_starts = 0 # SPIKE-06 C12: starts under one stuck verdict + self.stuck_wait = None # ... used up: the next start not before + self.stuck_warned = False # ... and its one CHARGER_REFUSED was given def owe_start(self) -> None: """C1: the add-on starts in Off while an exit 0 A of its own was still @@ -724,6 +728,15 @@ class SolarWriter(ManualWriter): so the 0 A is owed (once it may write), retried by the exit rule.""" self.start_owed = self.engaged = True + def _stuck_end(self) -> None: + """SPIKE-06: a stuck episode ends (the reading followed a start, a + press of Solar, Off, Manual or shadow): starts counted afresh, and + its CHARGER_REFUSED is cleared.""" + if self.stuck_warned: + self.gave_up = False + self.stuck_starts, self.stuck_wait = 0, None + self.stuck_warned = False + def _owed(self, pending: bool) -> None: if self.on_owed is not None: self.on_owed(pending) @@ -758,7 +771,7 @@ class SolarWriter(ManualWriter): held = self.hold or self.mismatch_since is not None super().user_changed() self.gave_up, self.fails, self.fail_at = False, 0, None - self.stuck_starts = 0 + self._stuck_end() if held: self.last_a = None self.sent_a, self.taken = None, True @@ -772,6 +785,8 @@ class SolarWriter(ManualWriter): zero = float(a) <= TOL_A if not zero and tracker is not None and tracker.stuck(now): self.stuck_starts += 1 # C12: a start under a stuck verdict + if self.stuck_wait is not None: + self.stuck_wait = now + STUCK_BACKOFF_S # a backoff try if zero: self.zero_at = self.zero_sent_at = now self._owed(True) # C1: kept with the mode until back @@ -1007,6 +1022,7 @@ class SolarWriter(ManualWriter): self._adopt(cur) # round 9: a late read-back self._mark(r, now) if mode != SOLAR: + self._stuck_end() self.would = None if mode == OFF and self.start_owed: if not writing: @@ -1026,6 +1042,7 @@ class SolarWriter(ManualWriter): else self.plan.n n_now = 1 if self.single else n_real if not writing: + self._stuck_end() # Shadow, Stop, a safety stop, stopping: the way-out 0 A while # owed, then nothing. The watch goes on (another writer is held # for every reader) and so does the plan, for the card. @@ -1041,24 +1058,31 @@ class SolarWriter(ManualWriter): fault = ("error" if (r.error or r.state in ("E", "F")) else "stuck" if stuck else None) if not stuck: - if self.stuck_starts >= MAX_TRIES and self.gave_up: - self.gave_up = False # the reading moved: start afresh - self.stuck_starts = 0 + self._stuck_end() # the reading moved: start afresh if fault == "stuck" and not self.live: # SPIKE-06 C12/C14 (option 2): no current of Solar's own may stand # (its own 0 A stands, or Solar was just chosen), so a stuck # verdict no longer holds the car: Solar's normal step writes its # 0 A first over a current that is not its own, then the start - # rule. A start moves the reading (the verdict clears) or is not - # taken: then the 0 A again, and after MAX_TRIES such starts under - # one verdict, CHARGER_REFUSED and the 0 A stands (EV-04's rule). + # rule. A start moves the reading (the episode ends) or is not + # taken: then the 0 A again. After MAX_TRIES such starts, one + # CHARGER_REFUSED, then one more start after 15 min and one every + # 30 min (PM decision): a fault that clears is found again. if self.stuck_starts < MAX_TRIES: fault = None - elif not self.gave_up: - self.gave_up = True - _LOG.warning("The charger did not take the current, so the " - "add-on stops asking until you press Solar: " - "CHARGER_REFUSED.") + else: + if self.stuck_wait is None: + self.stuck_wait = now + STUCK_BACKOFF_FIRST_S + if not self.stuck_warned: + self.stuck_warned = True + _LOG.warning("The charger did not take the current, so " + "the add-on tries again in 15 min, then " + "every 30 min: CHARGER_REFUSED.") + if now >= self.stuck_wait: + self.gave_up = False + fault = None + else: + self.gave_up = True if fault is not None: # 0 A while owed, then no write until it clears; then the start # rule. A stuck reading cannot confirm the 0 A. diff --git a/goodwe_controller/test_ev20_exits.py b/goodwe_controller/test_ev20_exits.py index 5eb8d40..c351c70 100644 --- a/goodwe_controller/test_ev20_exits.py +++ b/goodwe_controller/test_ev20_exits.py @@ -240,6 +240,12 @@ check(f"T28 (b): frozen at 0 A, then Solar starts (the write unconfirmed): " bool(st28) and [x for x in w28b if x[0] >= st28[0]][:1] and [x for x in w28b if x[0] >= st28[0]][0][1] == 0.0 and r28b["sim"].setpoint_a == 0.0) +# SPIKE-06 (PM): under the verdict at most MAX_TRIES starts, then the backoff +# (15 min, then 30 min; test_ev20_rig.py): no 72 s flapping. +s28b = [t for t, a, _ in w28b if a > 0.0 and st28 and t >= st28[0]] +check(f"T28 (b): at most {carmode.MAX_TRIES} starts under the verdict in the " + f"run, then none before the 15 min backoff ({s28b})", + len(s28b) <= carmode.MAX_TRIES) # T29: the charger unavailable 300-420 s. T29 = {"i": lambda p, c: None, diff --git a/goodwe_controller/test_ev20_rig.py b/goodwe_controller/test_ev20_rig.py index d1ce379..e81a551 100644 --- a/goodwe_controller/test_ev20_rig.py +++ b/goodwe_controller/test_ev20_rig.py @@ -344,39 +344,91 @@ for race in (False, True): and r["trace"][-1]["fault"] is None) nevers(r, f"C12 [race={race}]") -# C12's bound: a charger whose reading is really frozen. Solar's own 0 A -# stands (the frozen read-back shows 0 A), so the start rule runs; each start -# write is not taken and the 0 A follows. At most MAX_TRIES starts under one -# verdict, then ONE CHARGER_REFUSED warning and the 0 A stands (EV-04: until -# Solar is pressed). Here the reading comes back at 1200 s with the very -# values it froze with (0 W, 0 A), so nothing moves; pressing Solar at 1250 s -# starts afresh. -n0 = LOG.mark() -r = S.drive(const(600.0), lambda t: 600.0 + (1000.0 if 200 <= t < 330 - else 2300.0 if t < 200 - else 6000.0), - dur=1500, first="car", socs={"a": 100.0, "b": 100.0}, - opts={"ev_pause_min": 1, "ev_start_min": 1}, - events={300: lambda p, c: p.ch.freeze(), - 1200: lambda p, c: p.ch.freeze(False), - 1250: act("car_mode", "solar")}) -r["_log"] = n0 -w = cur_writes(r) -st = [x["t"] for x in r["trace"] if x["fault"] == "stuck"] -t0 = nth(st, 0) if st else 0.0 -starts = [t for t, a, _ in w if a > 0.0 and t0 <= t < 1200] -warn = [m for m in LOG.since(n0, "WARNING") if "CHARGER_REFUSED" in m] -after = [(t, a) for t, a, _ in w if t >= 1200] -quiet = [(t, a) for t, a in after if t < 1250] -check(f"C12 bound: a frozen charger after Solar's own 0 A: at most 3 starts " - f"under the verdict ({starts}), one CHARGER_REFUSED ({len(warn)}), the " - f"charger at 0 A, nothing written until Solar is pressed, then a start " - f"within the start rule ({after[:2]})", - bool(st) and 1 <= len(starts) <= carmode.MAX_TRIES and len(warn) == 1 - and not quiet and present(nth(after, 0)) and nth(after, 0)[1] > 0.0 - and nth(after, 0)[0] <= 1250 + 75 - and [a for t, a, _ in w if t0 <= t < 1200][-1:] == [0.0]) -nevers(r, "C12 bound") +# C12's cap and backoff (PM decision): a charger whose reading is really +# frozen (at 300 s, with the values it will show when it comes back). Solar's +# own 0 A stands, so the start rule runs; each start is not taken and the 0 A +# follows. At most MAX_TRIES starts under one verdict, then ONE +# CHARGER_REFUSED; then one more start 15 min later, then one every 30 min +# (each by the start rule), never the 72 s flapping. +def frozen_run(dur, events=None): + n0 = LOG.mark() + ev = {300: lambda p, c: p.ch.freeze()} + ev.update(events or {}) + r = S.drive(const(600.0), lambda t: 600.0 + (1000.0 if 200 <= t < 330 + else 2300.0 if t < 200 + else 6000.0), + dur=dur, first="car", socs={"a": 100.0, "b": 100.0}, + opts={"ev_pause_min": 1, "ev_start_min": 1}, events=ev) + r["_log"] = n0 + st = [x["t"] for x in r["trace"] if x["fault"] == "stuck"] + t0 = nth(st, 0) if st else 1e9 + w = cur_writes(r) + starts = [t for t, a, _ in w if a > 0.0 and t >= t0] + warn = [m for m in LOG.since(n0, "WARNING") if "CHARGER_REFUSED" in m] + return r, w, starts, warn + + +r, w, starts, warn = frozen_run(4000) +first3, later = starts[:3], starts[3:] +t3 = nth(first3, 2) +gaps = [b - a for a, b in zip(later, later[1:])] +check(f"cap: a frozen charger: 3 starts under the verdict ({first3}), then " + f"one CHARGER_REFUSED ({len(warn)})", + len(first3) == carmode.MAX_TRIES and len(warn) == 1 + and all(b - a < 120 for a, b in zip(first3, first3[1:]))) +check(f"backoff: one more start 15 min after the 3rd (+ the start rule), " + f"then one every 30 min (+ the start rule): {later} (gaps {gaps})", + present(t3) and len(later) == 2 + and 900 <= nth(later, 0) - t3 <= 900 + 80 + and all(1800 <= g <= 1800 + 80 for g in gaps)) +nevers(r, "cap and backoff") + +# Recovery: the reading comes back at 1200 s with the values it froze with, +# so nothing moves by itself; the next backoff start is taken, the reading +# follows it, the episode ends (warning cleared) and the car charges. +r, w, starts, warn = frozen_run(2400, {1200: lambda p, c: p.ch.freeze(False)}) +t3 = nth(starts, 2) +rec = [t for t in starts if t > 1200] +check(f"recovery: the fault clears at 1200 s; the next backoff start " + f"({rec[:1]}, 15 min after the 3rd at {t3}) is taken and the car " + f"charges to the end, warning cleared, not stuck", + present(nth(rec, 0)) and present(t3) + and 900 <= nth(rec, 0) - t3 <= 980 + and r["sim"].power_w > 1000.0 and not r["c"].car.stuck_now + and not r["c"].car_solar.gave_up) +nevers(r, "recovery") + +# A press of Solar during the backoff starts the episode afresh: a start by +# the start rule after the press, not 15 min later. +r, w, starts, warn = frozen_run(1400, {1000: act("car_mode", "solar")}) +after = [t for t in starts if t >= 1000] +check(f"press: Solar pressed at 1000 s during the backoff: a start by the " + f"start rule after the press ({after[:1]})", + present(nth(after, 0)) and nth(after, 0) <= 1000 + 75) + +# The episode ends on Off, Manual and shadow: counter, wait and warning +# cleared (read 5 s after the switch). +for name, fn in (("Off", act("car_mode", "off")), + ("Manual", act("car_mode", "manual")), + ("shadow", lambda p, c: c.o.__setitem__("shadow_mode", + "on"))): + seen = {} + + def look(p, c, seen=seen): + sw = c.car_solar + seen.update(starts=sw.stuck_starts, gave_up=sw.gave_up, + wait=getattr(sw, "stuck_wait", None), + warned=getattr(sw, "stuck_warned", None)) + before = {} + + def look0(p, c, before=before): + before.update(starts=c.car_solar.stuck_starts, + gave_up=c.car_solar.gave_up) + r, w, starts, warn = frozen_run(900, {790: look0, 800: fn, 805: look}) + check(f"episode end on {name}: before {before}, after {seen}", + before.get("starts") == carmode.MAX_TRIES and before.get("gave_up") + and seen.get("starts") == 0 and seen.get("gave_up") is False + and seen.get("wait") is None and seen.get("warned") is False) # C14: Manual's refused writes leave a stuck verdict on a paused, live car, # then the user presses Solar. No current of Solar's own may stand, so the