EV-51: Solar decides every meter-age rule on the writer step's own age; DEBUG line per step
checks / unit (pull_request) Successful in 41m26s
infra-pin / fresh (pull_request) Successful in 2s

The writer step took the meter age from the snapshot of the previous cycle
(silent / unknown_s / hold), so in production order the silent-meter pause
came one cycle late (C21) and a start or raise could still be written at an own
age of 31-39 s (C22, N8). The step now judges its own age: the pause timing and
held_pause, the non-zero write block (nz_block), and the late-readings hold with
its stale rule. Meter values stay from the snapshot; car.held / car.reading and
the arrival sources' spare-unknown check are unchanged.

Also: one DEBUG line per Solar writer step (time, meter age, decision, write);
tools/ev20_sun.drive takes cyc / phase / jitter / cset (default unchanged);
test_ev51.py (T1-T43, N1-N12); DECISIONS states the rule once.

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VSY9UpTaUYnjJF5oVtbM1w
This commit is contained in:
ems-agent
2026-11-24 17:26:03 +01:00
co-authored by Claude
parent 5bff894cca
commit 68856bc3a0
6 changed files with 1012 additions and 11 deletions
+45 -2
View File
@@ -137,11 +137,41 @@ class SunPlant:
return True
def _cycle_seconds(t0, dur, cyc, phase, jitter, cset):
"""EV-51: the seconds a cycle runs, or None for every second."""
if cset is not None:
due = {int(x) for x in cset if t0 <= x < t0 + dur}
if not due:
raise ValueError("cset has no second inside the run")
return due
if jitter is not None:
gaps = [int(g) for g in jitter]
if not gaps or min(gaps) <= 0:
raise ValueError("jitter needs gaps above 0")
due, t, i = set(), int(t0), 0
while t < t0 + dur:
due.add(t)
t += gaps[i % len(gaps)]
i += 1
return due
if cyc != int(cyc) or cyc <= 0:
raise ValueError("cyc must be a whole number of seconds above 0")
if not 0 <= phase < cyc:
raise ValueError("phase must be in [0, cyc)")
if cyc == 1:
return None
due = {int(t0) + k for k in range(int(phase), int(dur), int(cyc))}
if not due:
raise ValueError("no cycle inside the run")
return due
def drive(house, sun, *, dur=600.0, ratings=None, socs=None, cap=3500.0,
margin=100, opts=None, plug=True, setpoint=0, phases=1, mode="solar",
first="batteries", events=None, sim=None, store=None, race=False,
on_cycle=None, car_phases=3, t0=0.0, caps=None, finale=None,
rows=False, plant_cls=None, beside=False, meter_change=False):
rows=False, plant_cls=None, beside=False, meter_change=False,
cyc=1, phase=0, jitter=None, cset=None):
"""Run it. Returns a dict: plant, controller, charger, notifications with
their second, and a per-second trace. `caps` ({unit: kWh}) lets each
battery's level move with what it charges or gives; without it the
@@ -152,7 +182,14 @@ def drive(house, sun, *, dur=600.0, ratings=None, socs=None, cap=3500.0,
round 1): a write that waits for its read-back is still waiting when the
next cycle reads the charger; it resumes after that read.
`meter_change=True` publishes the meter as Home Assistant does on
`ha_signed`: only when the whole-watt value changes."""
`ha_signed`: only when the whole-watt value changes.
EV-51, the cycle's cadence (the meter and the charger model still tick
every 1 s; only the control cycle is thinned): `cyc` seconds between
cycles and `phase` (the first cycle at t0 + phase), or `jitter`, a
tuple of gaps used in turn (the first cycle at t0), or `cset`, the
cycle seconds. The default (1 s) runs every second as before. A cadence
that would run no cycle is a ValueError, never a vacuous pass."""
due = _cycle_seconds(t0, dur, cyc, phase, jitter, cset)
ratings = ratings or {"a": 5000.0, "b": 4600.0}
socs = socs or {u: 50.0 for u in ratings}
sim = sim or SimCharger(setpoint_a=setpoint)
@@ -252,6 +289,12 @@ def drive(house, sun, *, dur=600.0, ratings=None, socs=None, cap=3500.0,
"ha_signed", imp, exp, phases=1,
ingest_ts=rig.BASE + timedelta(seconds=ts),
ingest_mono=ts))
if due is not None and int(t) not in due:
for u, kwh in (caps or {}).items():
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
continue
await c.read_inputs(now_mono=t)
if race:
await c.car_settle()