EV-05 round 2: the peak cut waits 15 s while a battery can give
checks / unit (pull_request) Successful in 23m32s
infra-pin / fresh (pull_request) Successful in 1s

Owner 2026-11-18 (dev finding 1, brief v4 Expected 8): while a battery is
above its cut-out, the car's peak cut waits until the guard has wanted power
for 15 s in a row (CUT_WAIT_S), so a cloud edge the batteries catch does not
pause the car. It cuts at once with no battery able, or with 120 s or less of
the 15-minute period left (CUT_NEAR_END_S: waiting 15 s then costs at most
8/7 of the cut, taken off the car at once).

Tests: T21/T22 cut at 15 s and not sooner, the period lands under the aim;
T21b a house step and a cloud edge the batteries catch: no cut, no pause;
T24 (c) late in the period: the cut at once, under the aim; T12's cut row
with the batteries at their cut-out. The harness's battery level model was
1000x too fast (round 1); fixed. The 10-hour day: 0 cut pauses in both
priorities.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
ems-agent
2026-11-18 10:13:02 +01:00
co-authored by Claude
parent 8ba3e1c337
commit 17a7b299d2
6 changed files with 152 additions and 39 deletions
@@ -25,6 +25,17 @@ inputs (`car_sun_inputs`) and runs the writer in the same background task.
(`wanted_w > 0`), and by `min(excess, wanted)`. Batteries that only stop
charging in a cloud's first seconds are not "asked", so a cloud does not cut
the car. A partial cut caps the car for the rest of that 15-minute period.
- **The cut waits 15 s while a battery can still give** (owner 2026-11-18,
round 2): `ctl_car.CUT_WAIT_S`, counted from the first cycle in a row in
which the guard wants power. A battery "can give" when it is above its own
cut-out level, not refusing to give and not under a safety stop
(`_car_batt_able`). It cuts at once with nothing able, or with 120 s or less
of the period left (`CUT_NEAR_END_S`): waiting 15 s with R s left raises what
must come off afterwards by R / (R - 15), at most 8/7 at 120 s, and the cut
then takes it off the car at once. test_ev05.py T21 (a step beyond the
batteries, cut at 15 s) and T24 (c) (a late step, cut at once) show the period
landing under the aim; the 10-hour day of test_ev05_long.py has no cut
pause at the shipped `slew_w` 0.
- **The 4.0 to 4.14 kW band on 3 phases.** 3 phases need 4.14 kW at 6 A, the
switch back to 1 phase is under 4.0 kW. Inside that band the car holds 6 A on
3 phases with no pause count (the hysteresis the design chose). Under 4.0 kW
+5 -2
View File
@@ -3351,8 +3351,11 @@ Both apply at once, with no restart.
that draws one phase while three are allowed is counted on one phase.
- **The car is cut first for the peak limit.** When the 15-minute period heads
over the limit and the peak guard would ask the batteries for power, that is
taken off the car first, down to 0 A at once. A Solar charge never sets the
month's peak.
taken off the car first, down to 0 A if needed. While a battery can still
give power, the add-on first waits 15 seconds, so a cloud the batteries
catch does not pause the car. With every battery at its cut-out level, or
in the last 2 minutes of the 15-minute period, it cuts at once. A Solar
charge never sets the month's peak.
- **Every way out of Solar leaves the charger at 0 A**, written once: a stop or
restart of the add-on (before the inverters go to 0 W), stopping control, a
safety stop, shadow mode turned on, choosing Off, an unplugged car, a charger
+34 -1
View File
@@ -36,6 +36,16 @@ OWN_PAUSE_MAX_S = (carmode.ZERO_WAIT_S + carmode.SETTLE_S
+ carmode.MAX_TRIES * 15.0 + 10.0)
OWN_PAUSE_TAIL_S = chargers.CORR_WINDOW_S
CAR_SUN_KEY = "car_sun" # EV-05: {day, kwh} charged from the sun today
# EV-05 round 2 (owner 2026-11-18): while a battery can still give power the
# peak cut waits until the guard has wanted power this long in a row, so a
# cloud edge the batteries catch does not pause the car. It cuts at once when
# no battery can give (all at their cut-out) or with this little of the
# 15-minute period left: waiting 15 s with R s left raises what must come off
# afterwards by R / (R - 15), at most 8/7 at R = 120 s, and the cut takes it
# off the car at once then (test_ev05.py T21, T21b, T24c: the period lands
# under the aim).
CUT_WAIT_S = 15.0
CUT_NEAR_END_S = 120.0
# EV-05: at shutdown the charger's 0 A waits at most this long, then the
# inverters are released whatever the charger did.
RELEASE_WAIT_S = 15.0
@@ -67,6 +77,7 @@ class CarMixin:
self.car_cut_w = 0.0
self._car_key = None
self._car_late_s = None
self._car_want_since = None # EV-05: the guard wants power since
self._car_sun_last = None
self._car_error_notified = False
sun = data.get(CAR_SUN_KEY)
@@ -433,6 +444,18 @@ class CarMixin:
<= OWN_PAUSE_MAX_S
return 0.0 <= mono - wr.phase_done_at <= OWN_PAUSE_TAIL_S
def _car_batt_able(self) -> bool:
"""EV-05: can a battery still give power? One that is above its own
cut-out level, not refusing to give and not under a safety stop."""
for inv in self.inverters:
soc, floor = inv.soc, inv.bms_soc_floor
if soc is None or floor is None or inv.sign_wd.tripped \
or (inv.refused or 0) > 0:
continue
if float(soc) > float(floor):
return True
return False
def _car_solar_pause(self, mono) -> bool:
"""EV-05: Solar's own 0 A (a pause, the peak cut, the phase change)
gets EV-15's grace, bounded the same way: from the moment it is sent,
@@ -548,6 +571,7 @@ class CarMixin:
# What the guard would ASK the batteries to give (`wanted_w`, its floor
# before the rating) is taken off the car instead, up to the excess:
# batteries that only stop charging are not asked for anything.
cut = 0.0
if self.car_solar_mode() and real_proj is not None \
and target_w is not None and real_proj.block_start is not None \
and guard is not None and guard.engaged and guard.blind is None:
@@ -555,7 +579,16 @@ class CarMixin:
want = guard.wanted_w
if ex is not None and want is not None and math.isfinite(ex) \
and math.isfinite(want) and ex > 0.0 and want > 0.0:
self.car_cut_w = float(min(ex, want))
cut = float(min(ex, want))
if cut > 0.0:
if self._car_want_since is None:
self._car_want_since = mono
left = QUARTER_S - float(real_proj.elapsed_s)
if not self._car_batt_able() or left <= CUT_NEAR_END_S \
or mono - self._car_want_since >= CUT_WAIT_S:
self.car_cut_w = cut
else:
self._car_want_since = None
self._car_sun_tick(mono)
w = self.car_alarm_w()
# EV-15: the figure for grading only. Our own phase change holds the
+89 -32
View File
@@ -577,41 +577,99 @@ ABLE = {"a": 60.0, "b": 60.0}
CUTOUT = {"a": 10.0, "b": 10.0} # the batteries' own cut-out: no giving
def quarters(r, aim):
"""(start, import average, car average) of each whole quarter."""
out = []
tr = r["trace"]
for q0 in range(0, int(tr[-1]["t"]) + 1 - 900, 900):
xs = [x for x in tr if q0 <= x["t"] < q0 + 900]
if len(xs) < 890:
continue
g = sum(max(0.0, x["grid"]) for x in xs) / len(xs)
car = sum(x["car"] for x in xs) / len(xs)
out.append((q0, g, car))
return out
def first_cut(r, after):
return next((x for x in r["trace"] if x["t"] >= after and x["cut"] > 0),
None)
# Car first, house 500 W, sun 2900 W: 2300 W = 10 A on 1 phase.
# The step: 500 -> 6000 W early in the period, about 800 W over the aim.
r21 = S.drive(house=steps((0, 500.0), (200, 6000.0)), sun=const(2900.0),
first="car", socs=ABLE, dur=260)
# Round 2 (owner 2026-11-18): while a battery can still give, the cut waits
# until the guard has wanted power for 15 s in a row (CUT_WAIT_S).
# T21b: a step the batteries catch within 15 s (500 -> 6000 W): no cut.
r21b = S.drive(house=steps((0, 500.0), (200, 6000.0)), sun=const(2900.0),
first="car", socs=ABLE, dur=400)
want21b = [x["t"] for x in r21b["trace"] if x["since"] is not None]
# The step also takes the sun from the car, so the normal hold (6 A) follows.
check(f"T21b: a house step the batteries catch within 15 s: the guard wanted "
f"power for {len(want21b)} s, no cut, no pause; only the hold's 6 A "
f"({cur(r21b)})",
0 < len(want21b) < 15 and not first_cut(r21b, 0)
and [v for _t, v in cur(r21b)] == [10.0, 6.0])
# A cloud edge with the car at high power on 3 phases (sun 13 kW -> 3 kW for
# 120 s, house 500 W), 10 s into a period so the 10 kW import does head over
# the aim: the guard wants power until the batteries ramp; at the shipped
# `slew_w` 0 they catch it within 15 s, so the car is not cut and not paused
# (owner 2026-11-18).
r21c = S.drive(house=const(500.0), sun=steps((0, 13000.0), (910, 3000.0),
(1030, 13000.0)),
first="car", socs=ABLE, dur=1150, opts={"slew_w": 0})
w21c = [(t, v) for t, v in cur(r21c) if 900 <= t < 1150]
want21c = [x["t"] for x in r21c["trace"] if x["since"] is not None
and 905 <= x["t"] <= 960]
check(f"T21b: a cloud edge the batteries catch within 15 s gives no cut and "
f"no pause: the guard wanted power for {len(want21c)} s (writes {w21c}, "
f"phases {ph(r21c)})",
0 < len(want21c) < 15
and not first_cut(r21c, 900) and 0.0 not in [v for _t, v in w21c]
and nonzero(r21c) and nonzero(r21c)[-1][1] >= 6.0)
# T21: a step the batteries cannot catch (500 -> 15500 W for 90 s): the cut
# comes 15 s after the guard first wants power, not sooner.
r21 = S.drive(house=steps((0, 500.0), (200, 15500.0), (290, 500.0)),
sun=const(2900.0), first="car", socs=ABLE, dur=920)
x21 = first_cut(r21, 200)
w21 = [(t, v) for t, v in cur(r21) if t >= 200]
since21 = next((x["since"] for x in r21["trace"] if x["since"] is not None),
None)
w21 = [] if x21 is None else [(t, v) for t, v in cur(r21) if t >= x21["t"]]
lim21 = None if x21 is None else math.floor((2300.0 - x21["cut"]) / 230.0)
check(f"T21: a house step takes the period over the aim with the batteries "
f"able: the car is cut within 1 cycle, to at most floor((2300 - cut) / "
f"230) A (cut {None if x21 is None else round(x21['cut'])} W at "
check(f"T21: a house step beyond the batteries: no cut in the first 15 s the "
f"guard wants power, then the car is cut within 1 cycle, to at most "
f"floor((2300 - cut) / 230) A (wants from {since21}, cut "
f"{None if x21 is None else round(x21['cut'])} W at "
f"{None if x21 is None else x21['t']}, writes {w21})",
x21 is not None and x21["t"] <= 202 and w21
and w21[0][0] <= x21["t"] + 1 and w21[0][1] <= max(lim21, 0))
x21 is not None and since21 is not None
and 15 <= x21["t"] - since21 <= 16 and w21
and w21[0][0] <= x21["t"] + 2 and w21[0][1] <= max(lim21, 0)
and not [t for t, v in cur(r21) if 200 <= t < x21["t"] and v == 0.0])
q21 = [g for q, g, _c in quarters(r21, 3400.0) if q == 0]
check(f"T21: the period still lands under the aim ({[round(g) for g in q21]} "
f"W against 3400 W)", q21 and q21[0] <= 3410.0)
_after = [x for x in r21["trace"] if x21 and x["t"] == x21["t"] + 5]
check(f"T21: 5 s later, what must still come off the meter is at most the "
f"excess less the cut ({[round(x['excess'] or 0) for x in _after]})",
_after and (_after[0]["excess"] or 0.0) <= max(
0.0, (x21["excess"] or 0.0) - x21["cut"]) + 60.0)
r22 = S.drive(house=steps((0, 500.0), (200, 7500.0), (260, 500.0)),
sun=const(2900.0), first="car", socs=ABLE, dur=600)
r22 = S.drive(house=steps((0, 500.0), (200, 17500.0), (290, 500.0)),
sun=const(2900.0), first="car", socs=ABLE, dur=700)
# (a hold's 6 A may come first: the step also takes the car's sun)
x22 = first_cut(r22, 200)
w22 = [(t, v) for t, v in cur(r22) if t >= 200]
since22 = next((x["since"] for x in r22["trace"] if x["since"] is not None),
None)
w22 = [] if x22 is None else [(t, v) for t, v in cur(r22) if t >= x22["t"]]
last_cut22 = max((x["t"] for x in r22["trace"] if x["cut"] > 0), default=None)
check(f"T22: an excess of 920 W or more: 0 A at once, no 5-min hold "
f"({w22}, cut {None if x22 is None else round(x22['cut'])})",
x22 is not None and w22 and w22[0] == (w22[0][0], 0.0)
and w22[0][0] <= x22["t"] + 1)
check(f"T22: an excess of 920 W or more: 0 A, no 5-min hold, after the 15 s "
f"wait and not sooner ({w22}, wants from {since22}, cut "
f"{None if x22 is None else round(x22['cut'])})",
x22 is not None and since22 is not None
and 15 <= x22["t"] - since22 <= 16 and w22
and w22[0][1] == 0.0 and w22[0][0] <= x22["t"] + 2
and not [t for t, v in cur(r22) if 200 <= t < x22["t"] and v == 0.0])
check(f"T22: no restart before the period is under the aim (last cut "
f"{last_cut22}) and the 120 s start rule has run",
last_cut22 is not None and len(w22) >= 2
last_cut22 is not None and len(w22) >= 2 and w22[1][1] > 0
and w22[1][0] - last_cut22 >= 120)
# T23: the house alone over the aim, the batteries at their cut-out, the car
# at 6 A (started at 1700 W, then sun 2000, house 500, margin 100: 1400 W).
@@ -645,26 +703,24 @@ check(f"T24 (a): cut at :13:4x ({cuts24[:3]}), no further cut from :15:00, "
r24b = S.drive(house=steps((0, 500.0), (900, 6500.0)), sun=const(2900.0),
first="car", socs=CUTOUT, dur=960)
c24b = first_cut(r24b, 899)
# T24 (c): batteries able, 85 s before the period's end a step whose import,
# held, takes the period over the aim: inside CUT_NEAR_END_S (120 s) the cut
# comes at once, and the period lands under the aim.
r24c = S.drive(house=steps((0, 500.0), (815, 49000.0), (830, 500.0)),
sun=const(2900.0), first="car", socs=ABLE, dur=920)
c24c = first_cut(r24c, 815)
q24c = [g for q, g, _c in quarters(r24c, 3400.0) if q == 0]
check(f"T24 (c): late in the period with the batteries able the cut does not "
f"wait ({None if c24c is None else c24c['t']}, {cur(r24c)[-2:]}), and "
f"the period lands under the aim ({[round(g) for g in q24c]} W)",
c24c is not None and c24c["t"] <= 817 and q24c and q24c[0] <= 3410.0
and ctl_car.CUT_NEAR_END_S >= 120.0)
check(f"T24 (b): the new period heads over from its first second (N=2, "
f"next_quarter): the cut comes in its first cycles "
f"({None if c24b is None else c24b['t']}, {cur(r24b)[-1:]})",
c24b is not None and c24b["t"] <= 902 and cur(r24b)[-1][1] == 0.0)
def quarters(r, aim):
"""(start, import average, car average) of each whole quarter."""
out = []
tr = r["trace"]
for q0 in range(0, int(tr[-1]["t"]) + 1 - 900, 900):
xs = [x for x in tr if q0 <= x["t"] < q0 + 900]
if len(xs) < 890:
continue
g = sum(max(0.0, x["grid"]) for x in xs) / len(xs)
car = sum(x["car"] for x in xs) / len(xs)
out.append((q0, g, car))
return out
def day25(t):
"""Clouds and house steps, 2 hours."""
clouds = [(400, 520), (1500, 1530), (2600, 3100), (4000, 4060),
@@ -1101,8 +1157,9 @@ for name, trig in (("Off", lambda p, c: c.handle_action("car_mode",
check(f"T12 {name} 2 s after a normal write: the 0 A goes out within 1 "
f"cycle, the 30 s rule does not delay it ({cur(r12)})",
"t2" in s12 and z12 and s12["t2"] <= z12[0] <= s12["t2"] + 1)
# Batteries at their cut-out: nothing can give, so the cut does not wait.
r12c = S.drive(house=steps((0, 500.0), (154, 7500.0)), sun=steps(
(0, 2900.0), (150, 3600.0)), first="car", socs=ABLE, dur=260)
(0, 2900.0), (150, 3600.0)), first="car", socs=CUTOUT, dur=260)
_nz12 = [t for t, v in cur(r12c) if v > 0]
_z12 = [t for t, v in cur(r12c) if v == 0.0]
check(f"T12 the peak cut 2 s after a normal write: 0 A within 1 cycle "
+8 -3
View File
@@ -179,7 +179,11 @@ for first, race in (("batteries", False), ("car", True)):
# so with Batteries first the car gets sun once they are full.
r44 = S.drive(house=house44, sun=sun44, first=first, socs={"a": 40.0,
"b": 60.0},
caps={"a": 15.36, "b": 20.48}, dur=36000, race=race)
caps={"a": 15.36, "b": 20.48}, dur=36000, race=race,
opts={"slew_w": 0})
# `slew_w` 0 is the shipped default. With the harness's usual 400 W a
# cycle (the SAFETY-81 rig) this day also has no cut pause (dev report,
# round 2).
# On the harness clock, as the trace (the model's own clock runs ahead).
w44 = [(x[0], x[2]) for x in r44["plant"].wlog if x[1] == "current"]
p44 = [(x[0], x[2]) for x in r44["plant"].wlog if x[1] == "phases"]
@@ -241,7 +245,8 @@ for first, race in (("batteries", False), ("car", True)):
if g > 3410.0 and g - car <= 3410.0]
gaps = [b[0] - a[0] for a, b in zip(p44, p44[1:])]
check(f"T44 {first} first ({'race' if race else 'step'}): starts {starts} "
f"(<= 1 + {long44} long clouds + {len(cut_t)} peak cuts, Car first)"
f"(<= 1 + {long44} long clouds, Car first); peak-cut pauses "
f"{len(cut_t)} (round 2: none, the batteries catch the cloud edges)"
f"; pauses {pauses}; N5 dwells {n5[:3]}; selects {len(p44)} "
f"(gaps >= 600 s: {gaps[:4]}); no pause from a short cloud "
f"{short_hit[:3]}; the car's grid energy outside holds {grid_wh:.1f} "
@@ -249,7 +254,7 @@ for first, race in (("batteries", False), ("car", True)):
f"{len(steps44)} steps and edges); quarters over the aim by the car "
f"{over44}",
starts >= 1 and not n5 and all(g >= 600 for g in gaps)
and (first != "car" or starts <= 1 + long44 + len(cut_t))
and not cut_t and (first != "car" or starts <= 1 + long44)
and not seq_ok(r44) and not short_hit and grid_wh <= 10.0
and not over44)
+5 -1
View File
@@ -244,6 +244,9 @@ def drive(house, sun, *, dur=600.0, ratings=None, socs=None, cap=3500.0,
or g.saturated)),
floor=(None if g is None else g.floor_w),
excess=(None if g is None else g.excess_w),
want=(None if g is None else g.wanted_w),
since=c._car_want_since,
able=c._car_batt_able(), socs=dict(plant.socs),
proj=(None if g is None else g.projected_w),
target=(None if g is None else g.target_w),
would=w.would, single=w.single,
@@ -254,7 +257,8 @@ def drive(house, sun, *, dur=600.0, ratings=None, socs=None, cap=3500.0,
if callable(on_cycle):
on_cycle(plant, c, t)
for u, kwh in (caps or {}).items():
lv = plant.socs[u] - plant.batt_of(u, t) / 36.0 / kwh
# 1 s of W, as a percent of kWh: W / 3600 / (kWh x 1000) x 100
lv = plant.socs[u] - plant.batt_of(u, t) / 36000.0 / kwh
plant.socs[u] = max(0.0, min(100.0, lv))
t += 1.0
await c.car_settle()