Compare commits
13
Commits
sync-0.20.1
...
OPT-38
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0bacafadf0 | ||
|
|
c43873b142 | ||
|
|
b291262ee1 | ||
|
|
c70eeb6916 | ||
|
|
03541a1eee | ||
|
|
85433edac6 | ||
|
|
2667cf0dda | ||
|
|
5b6cb0bf08 | ||
|
|
c66aa7f69a | ||
|
|
6d2b9b5335 | ||
|
|
47c89a13d0 | ||
|
|
45535e19d7 | ||
|
|
af2d0d2f61 |
@@ -2,7 +2,10 @@
|
||||
|
||||
### Not in a release yet
|
||||
|
||||
- Home now draws today's charge from the grid for every hour the add-on will really charge, not only the run under way ([[UX-77]]).
|
||||
- When Home Assistant passes readings on late, control no longer swings on them: it waits until the battery shows its last power request, and the peak limit no longer asks for full power on them. The page says so, and if the readings stay too late every inverter goes to 0 W ([[SAFETY-128]]).
|
||||
- With a 5 s meter, readings 30 s late are now noticed within 100 seconds, not 3 minutes ([[SAFETY-129]]).
|
||||
- If one of the tomorrow solar forecast sensors stops updating in the afternoon, tomorrow's forecast now stays on the Forecast tab instead of vanishing until 20:00 ([[OPT-38]]).
|
||||
|
||||
## 0.20.1
|
||||
|
||||
|
||||
@@ -44,3 +44,5 @@ tonight." (`home_words.solar.kept`, EN and NL). The event log says
|
||||
**Not changed.** Control with modes off: it does not use the forecast. The
|
||||
screens still show the line, so it says "kept", not "used for the plan". A
|
||||
forecast that stays fresh. The hold does not look at the weather, only at the clock and the day.
|
||||
|
||||
**OPT-38 (2026-11-07).** A kept TOMORROW reading (at most `KEPT_MAX_AGE_S` old, same `day0`) now stands in at any hour, not only from `BELL_TO_H`: one sensor that stopped at 14:54 had dropped the whole sum (OPT-17) from 17:54 to 20:00. Today readings and the OPT-34 morning rules are unchanged; `test_opt38.py`.
|
||||
|
||||
@@ -31,7 +31,12 @@ inverter limit in its last seconds (OPT-34 rig F-2).
|
||||
- **Meter** (`MeterFit`, round 1's fit): `meter(t) + request(t - d)` is the
|
||||
house alone at the right `d`. Quick, but confounded by the house. Needs
|
||||
16 new values in 60 s, so only a meter of about 3.5 s or faster: at 5 s
|
||||
it gave false delays. Late over 8 s.
|
||||
it gave false delays. Late over 8 s. At a slower meter (a 5 s P1) the
|
||||
same fit runs over 120 s and calls only a delay over 16 s late, on 2 fits
|
||||
in a row, and never ends a verdict (SAFETY-129). Alone it reads 35-90 s on healthy
|
||||
runs, a few times in a row; only the slow-start and battery-fit check
|
||||
below keeps those from a verdict. 10 s late is not found: it cannot be
|
||||
told from a 10 s slow battery, and it does not swing at 5 s.
|
||||
- A meter fit says late only with evidence from the batteries' side too:
|
||||
a request more than 100 W from the battery reading took 3 s or more to
|
||||
move a reading towards it (`SLOW_START_S`; on time they start within
|
||||
@@ -64,9 +69,12 @@ house at 1, 2 and 5 s ticks, so the commands are the same as without the gate.
|
||||
The fits run all the time; the pull request has the counts.
|
||||
|
||||
**What it costs.**
|
||||
- Found 36-68 s after a lag starts at 1-2 s meters, about 3 minutes at a 5 s
|
||||
meter (battery fit only; the meter fit needs a faster meter). Until then control is as before. At 5 s and 30 s
|
||||
late the export is cut to about a third, not removed.
|
||||
- Found 36-68 s after a lag starts at 1-2 s meters; at a 5 s meter 30 s late
|
||||
is found within 100 s after it starts (88 s measured) (the loop first moves 100 W after 60 s,
|
||||
SAFETY-129) with the export above -200 W, and 10 s late is not found. Until
|
||||
then control is as before. In a quiet house the "readings late" row stays
|
||||
about 2 minutes longer after the lag ends (126 s against 32 s on base); no
|
||||
harm measured, and it clears as early as on base when the load changes.
|
||||
- A battery that answers slowly is read as late readings and held: a dead
|
||||
time of 10 s or more (8 s reads 7-8; 10 s reads 10 at 1-2 s meters, 9 at
|
||||
5 s), or a 15-20 s lag (review round 2). The GoodWe measured settles in 6 s
|
||||
|
||||
@@ -97,9 +97,14 @@ is `SolarToday.add()` after the solar read, in a `try`, in memory.
|
||||
- Prices are planned about a day ahead (`priceruns.HORIZON`), so a block of
|
||||
tomorrow later than 24 h from now, or one that needs prices not yet
|
||||
published, appears as the day comes near. The tab says so.
|
||||
- Today's blocks are Home's, and are not extended or shortened: control may
|
||||
plan more once they end, and Home draws it then. The curve stops charging
|
||||
at its level (`charge_to()`) and giving at the floor.
|
||||
- Today's blocks are Home's. UX-77: Home draws today's grid block to where
|
||||
control will stop charging, as the tab does for tomorrow's: the cheap run
|
||||
rolls on at its end while a battery is short (`cheaproll.roll_end()`, the
|
||||
same loop as `dayview._levels()`), the level read at the charge power the
|
||||
peak limit leaves (peak aim less the house, else the rating) and shared by
|
||||
room left to full as control shares it (`packshare.weights()`, re-read as
|
||||
the levels move), and each battery's block ends at its own stop. The curve stops charging at its level
|
||||
(`charge_to()`) and giving at the floor.
|
||||
- Today, a give run in the plan on a battery that is empty now has no block on
|
||||
Home (no energy to give). The tab equals Home, and says so: "The add-on plans
|
||||
to give power. No block shows while a battery has no energy to give." The
|
||||
|
||||
@@ -0,0 +1,201 @@
|
||||
"""UX-77: where the cheap run rolls on. Display only; control is unchanged.
|
||||
|
||||
A cheap run is sized for the batteries charging together, at their full
|
||||
rating, and starts at the first of the cheapest hours. The peak limit leaves
|
||||
less power than that, so a battery is still short when the run ends, and
|
||||
control plans the next run then (`Controller._price_runs()`). On a flat night
|
||||
that goes on until the batteries are at their level or the cheap hours end.
|
||||
|
||||
`roll_end()` asks what control asks at each run end, of the same functions
|
||||
(`modetrade.energy()`, `priceruns.plan()`), until nothing is short or the next
|
||||
run does not follow on. `for_today()` reads the batteries at the level they
|
||||
will have by then: the charge power is the peak limit's room (peak aim minus
|
||||
the house, as `dayplan` counts it), shared as control shares it: by room
|
||||
left to full (`packshare.weights()`), read again as the levels move, so a
|
||||
battery that fills drops out and its share goes to the others. Where that
|
||||
room is not known (no house profile or no aim), the rating is used.
|
||||
DECISIONS/UX-54-forecast-tab.md has the record.
|
||||
"""
|
||||
|
||||
import dataclasses
|
||||
import math
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from . import arbitrage, dayplan, modetrade, packshare, priceruns
|
||||
from .dayplan import EFF
|
||||
from .price import required_spread, uncertainty_of
|
||||
|
||||
SLOT_S = 900 # a price slot
|
||||
STEP = timedelta(minutes=5) # how finely the level is followed
|
||||
LIMIT = 24 # rolls at most (a day of one-hour runs)
|
||||
|
||||
|
||||
def roll_end(run, points, packs_at, *, required, floor_pct, ceiling_pct,
|
||||
targets=None, limit=LIMIT):
|
||||
"""Where the cheap run `run` ends once control has rolled it on.
|
||||
|
||||
`packs_at(end)` gives the `modetrade.Pack`s as they stand when a run
|
||||
ends at `end`. At each end the charge still short is asked of
|
||||
`modetrade.energy()` and the next run of `priceruns.plan()`, as control
|
||||
asks them. It stops when nothing is short or the next run does not follow
|
||||
on, and is `run.end` when control would not roll it."""
|
||||
end_t = run.end
|
||||
for _ in range(limit):
|
||||
e = modetrade.energy(packs_at(end_t), floor_pct, ceiling_pct,
|
||||
targets=targets)
|
||||
if e[0] <= 1e-6:
|
||||
break
|
||||
new = priceruns.plan(points, end_t, required_eur_kwh=required,
|
||||
charge_kwh=e[0], charge_kw=e[1], sell_kwh=0.0,
|
||||
sell_kw=0.0)
|
||||
nxt = new.cheap
|
||||
if nxt is None or not new.charge_useful() or nxt.start > end_t \
|
||||
or nxt.end <= end_t:
|
||||
break
|
||||
end_t = nxt.end
|
||||
return end_t
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Roll:
|
||||
"""`end`: where control stops charging, `stops`: name -> when that battery
|
||||
reaches its level (rounded up to a price slot), None while it still
|
||||
charges at `end`. A battery not in `stops` does not charge in the run."""
|
||||
end: datetime
|
||||
stops: dict
|
||||
|
||||
def end_for(self, name, base_end):
|
||||
"""The end of `name`'s block: its stop, no later than `end`."""
|
||||
if name not in self.stops:
|
||||
return base_end
|
||||
stop = self.stops[name]
|
||||
return self.end if stop is None else min(self.end, stop)
|
||||
|
||||
def last(self, base_end):
|
||||
"""The last end of any battery's block (the chart's cheap bar)."""
|
||||
return max([base_end] + [self.end_for(n, base_end)
|
||||
for n in self.stops])
|
||||
|
||||
|
||||
def _ceil_slot(t):
|
||||
return datetime.fromtimestamp(
|
||||
math.ceil(t.timestamp() / SLOT_S) * SLOT_S, tz=t.tzinfo
|
||||
or timezone.utc)
|
||||
|
||||
|
||||
class _Charge:
|
||||
"""The batteries' levels followed from `t0` while the chargers charge at
|
||||
the room the peak limit leaves: `min(sum of ratings, aim - house)`, shared
|
||||
by room left to full as control does (`_shares()`), each stopping at `modetrade.charge_to()`."""
|
||||
|
||||
def __init__(self, packs, t0, ceiling_pct, targets, aim_w, house):
|
||||
self.packs = {p.name: p for p in packs}
|
||||
self.t = t0
|
||||
self.ceil = ceiling_pct
|
||||
self.targets = targets
|
||||
self.aim = aim_w
|
||||
self.house = house
|
||||
self.stops = {}
|
||||
|
||||
def at(self, end):
|
||||
"""The packs when a run ends at `end`."""
|
||||
self._to(end)
|
||||
return list(self.packs.values())
|
||||
|
||||
def _room_w(self, hour, total):
|
||||
if self.aim is None or self.house is None:
|
||||
return total
|
||||
house_w, base_w = self.house
|
||||
return min(total, max(0.0, float(self.aim)
|
||||
- max(house_w(hour), base_w, 0.0)))
|
||||
|
||||
def _to(self, end):
|
||||
while self.t < end:
|
||||
nxt = min(end, self.t + STEP)
|
||||
for s, e, hour in dayplan._hour_slices(self.t, nxt):
|
||||
self._charge(s, e, hour)
|
||||
self.t = nxt
|
||||
|
||||
def _shares(self, ch, w):
|
||||
"""Watts per charger: `w` shared by `packshare.weights()` over all
|
||||
packs (room left to full, as control passes it), no charger above its
|
||||
rating, the surplus going to the others. By rating where the room left
|
||||
is not known (no capacity)."""
|
||||
wt = packshare.weights(list(self.packs.values()), -1.0)
|
||||
if wt is None:
|
||||
wt = {p.name: abs(p.rating_w) for p in ch}
|
||||
wt = {p.name: wt.get(p.name, 0.0) for p in ch}
|
||||
cap = {p.name: abs(p.rating_w) for p in ch}
|
||||
out, open_, left = {}, set(wt), float(w)
|
||||
while open_:
|
||||
tot = sum(wt[n] for n in open_)
|
||||
if tot <= 0.0:
|
||||
for n in open_:
|
||||
out[n] = 0.0
|
||||
break
|
||||
over = [n for n in open_ if left * wt[n] / tot > cap[n] + 1e-9]
|
||||
if not over:
|
||||
for n in open_:
|
||||
out[n] = left * wt[n] / tot
|
||||
break
|
||||
for n in over:
|
||||
out[n] = cap[n]
|
||||
left -= cap[n]
|
||||
open_.discard(n)
|
||||
return out
|
||||
|
||||
def _charge(self, s, e, hour):
|
||||
ch = modetrade.chargers(list(self.packs.values()), self.ceil,
|
||||
targets=self.targets)
|
||||
total = sum(abs(p.rating_w) for p in ch)
|
||||
if not ch or total <= 0.0:
|
||||
return
|
||||
w = self._room_w(hour, total)
|
||||
hrs = (e - s).total_seconds() / 3600.0
|
||||
share = self._shares(ch, w)
|
||||
for p in ch:
|
||||
self.stops.setdefault(p.name, None)
|
||||
if not p.capacity_kwh:
|
||||
continue
|
||||
to = modetrade.charge_to(p, self.ceil, self.targets)
|
||||
kwh = share[p.name] * hrs / 1000.0 * EFF
|
||||
room = (to - p.soc) / 100.0 * float(p.capacity_kwh)
|
||||
if kwh <= 0.0:
|
||||
continue
|
||||
if kwh >= room - 1e-9:
|
||||
self.stops[p.name] = _ceil_slot(
|
||||
s + (e - s) * (room / kwh))
|
||||
kwh = room
|
||||
self.packs[p.name] = dataclasses.replace(
|
||||
p, soc=p.soc + kwh / float(p.capacity_kwh) * 100.0)
|
||||
|
||||
|
||||
def for_today(c, rp, now):
|
||||
"""The `Roll` of the cheap run `rp.cheap` now under way or to come today,
|
||||
or None when control does not roll it on (no run left, charging not
|
||||
useful, the trade gate shut, nothing short at its end). Display only:
|
||||
nothing here is fed back to control."""
|
||||
if rp is None or not rp.on or rp.cheap is None or rp.cheap.end <= now \
|
||||
or not rp.charge_useful() \
|
||||
or getattr(c, "mode_gate", None) is not None:
|
||||
return None
|
||||
plan = c.fresh_tariff_plan(now)
|
||||
if plan is None:
|
||||
return None
|
||||
floor, ceil = arbitrage.soc_bounds(c.o)
|
||||
hp = getattr(c, "house_profile", None)
|
||||
sim = _Charge(c._mode_packs(), max(now, rp.cheap.start), ceil,
|
||||
c.plan_targets(), c.peak_aim_w(),
|
||||
None if hp is None else hp.estimator())
|
||||
required = required_spread(
|
||||
arbitrage.threshold_eur_kwh(c.o),
|
||||
uncertainty_of(plan.read.product)
|
||||
| arbitrage.weather_uncertainty(True, c.solar_fc,
|
||||
bool(c.weather_held(now))))
|
||||
end = roll_end(rp.cheap, plan.read.points, sim.at, required=required,
|
||||
floor_pct=floor, ceiling_pct=ceil,
|
||||
targets=c.plan_targets())
|
||||
if end <= rp.cheap.end:
|
||||
return None
|
||||
return Roll(end, dict(sim.stops))
|
||||
@@ -19,7 +19,8 @@ empty chart. DECISIONS/UX-54-forecast-tab.md has the record.
|
||||
import dataclasses
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from . import arbitrage, dayplan, homeview as hv, modetrade, priceruns
|
||||
from . import arbitrage, cheaproll, dayplan, homeview as hv, modetrade
|
||||
from . import priceruns
|
||||
from . import solarfc
|
||||
from .daycurve import Batt, curve
|
||||
from .packmode import SOLAR_FIRST
|
||||
@@ -210,25 +211,18 @@ def _levels(c, est, fc, trust, now, end, runs, floor, ceil, notes_b, shadow,
|
||||
arbitrage.threshold_eur_kwh(c.o),
|
||||
uncertainty_of(plan.read.product)
|
||||
| arbitrage.weather_uncertainty(True, c.solar_fc))
|
||||
end_t = cheap.end
|
||||
for _ in range(24):
|
||||
ext = dataclasses.replace(rp, cheap=priceruns.Run(
|
||||
|
||||
def packs_at(end_t):
|
||||
"""The packs at the curve's level when a run ends at `end_t`."""
|
||||
at = dataclasses.replace(rp, cheap=priceruns.Run(
|
||||
cheap.start, end_t, cheap.mean_eur_kwh))
|
||||
rt, lv = settle(ext, lv0)
|
||||
at_end = [dataclasses.replace(packs[n], soc=v)
|
||||
for n, v in ((n, _level_at(lv.get(n, ()), end_t))
|
||||
for n in lv) if v is not None]
|
||||
e = modetrade.energy(at_end, floor, ceil, targets=targets)
|
||||
if e[0] <= 1e-6:
|
||||
break
|
||||
new = priceruns.plan(
|
||||
plan.read.points, end_t, required_eur_kwh=required,
|
||||
charge_kwh=e[0], charge_kw=e[1], sell_kwh=0.0, sell_kw=0.0)
|
||||
nxt = new.cheap
|
||||
if nxt is None or not new.charge_useful() or nxt.start > end_t \
|
||||
or nxt.end <= end_t:
|
||||
break
|
||||
end_t = nxt.end
|
||||
_rt, lv = settle(at, lv0)
|
||||
return [dataclasses.replace(packs[n], soc=v)
|
||||
for n, v in ((n, _level_at(lv.get(n, ()), end_t))
|
||||
for n in lv) if v is not None]
|
||||
end_t = cheaproll.roll_end(
|
||||
cheap, plan.read.points, packs_at, required=required,
|
||||
floor_pct=floor, ceiling_pct=ceil, targets=targets)
|
||||
ext = dataclasses.replace(rp, cheap=priceruns.Run(
|
||||
cheap.start, end_t, cheap.mean_eur_kwh))
|
||||
rt, lv = settle(ext, lv0)
|
||||
|
||||
@@ -19,7 +19,7 @@ section at the bottom and is held to STYLE.md by test_messages.py.
|
||||
import contextlib
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from . import arbitrage, messages, peakguard, uitext
|
||||
from . import arbitrage, cheaproll, messages, peakguard, priceruns, uitext
|
||||
from .dayplan import EFF
|
||||
from .maintenance import CHARGE as M_CHARGE, DRAIN as M_DRAIN, HOLD as M_HOLD
|
||||
from .maintenance import IDLE as M_IDLE, next_slot_start
|
||||
@@ -218,14 +218,20 @@ def price_windows(c, plan, verdict, now=None):
|
||||
"""`[(kind, start, end)]`: where the add-on itself buys ("cheap") and
|
||||
gives ("dear"). With battery modes on that is the kept `priceruns.RunPlan`
|
||||
(`c.mode_runs`): the cheap run when charging in it pays and the trade gate
|
||||
allows it (UX-53), and every give run. With them off it is the arbitrage
|
||||
verdict's legs the add-on will take, the ones `trade_runs()` draws
|
||||
(`trade_legs()`, given `now`). Nothing is classified here."""
|
||||
allows it (UX-53), and every give run. UX-77: the cheap run is drawn to
|
||||
where control stops charging as it rolls on (`cheaproll`). With them off
|
||||
it is the arbitrage verdict's legs the add-on will take, the ones
|
||||
`trade_runs()` draws (`trade_legs()`, given `now`). Nothing is classified
|
||||
here."""
|
||||
if getattr(c, "modes_on", False):
|
||||
rp = getattr(c, "mode_runs", None)
|
||||
if rp is None or not rp.on or not _price_mode_in_use(c):
|
||||
return []
|
||||
out = [("cheap", rp.cheap.start, rp.cheap.end)] \
|
||||
end = rp.cheap.end
|
||||
roll = None if now is None else cheaproll.for_today(c, rp, now)
|
||||
if roll is not None:
|
||||
end = roll.last(end) # UX-77: to where control stops
|
||||
out = [("cheap", rp.cheap.start, end)] \
|
||||
if rp.charge_useful() and getattr(c, "mode_gate", None) is None \
|
||||
else []
|
||||
return out + [("dear", g.start, g.end) for g in rp.give]
|
||||
@@ -422,6 +428,7 @@ def mode_runs(c, day0, day1, now, shadow, ahead=False, rp=None):
|
||||
`ahead=True`) a charge from the floor and a give from the ceiling. The
|
||||
same filter, the same `MIN_KWH`. `rp`: a run plan to read instead of
|
||||
`c.mode_runs`. Home never passes either."""
|
||||
today = rp is None and not ahead
|
||||
rp = rp if rp is not None else getattr(c, "mode_runs", None)
|
||||
if getattr(c, "mode_tick", None) is None:
|
||||
return [], None # no cycle yet: nothing is known
|
||||
@@ -431,6 +438,9 @@ def mode_runs(c, day0, day1, now, shadow, ahead=False, rp=None):
|
||||
return [], "not_worth"
|
||||
floor, ceiling = arbitrage.soc_bounds(c.o)
|
||||
gate = getattr(c, "mode_gate", None)
|
||||
# UX-77: today's grid block runs to where control stops charging (the run
|
||||
# rolls on when it ends), each battery's to its own stop.
|
||||
roll = cheaproll.for_today(c, rp, now) if today else None
|
||||
out = []
|
||||
for i, inv in enumerate(c.inverters):
|
||||
pm = c.pack_mode(inv)
|
||||
@@ -465,7 +475,10 @@ def mode_runs(c, day0, day1, now, shadow, ahead=False, rp=None):
|
||||
if gate is None and rp.charge_useful() and (
|
||||
need > 0.0 if need is not None
|
||||
else pm.mode in GRID_CHARGE_MODES):
|
||||
blocks.append(("grid", rp.cheap))
|
||||
cheap = rp.cheap if roll is None else priceruns.Run(
|
||||
rp.cheap.start, roll.end_for(inv.name, rp.cheap.end),
|
||||
rp.cheap.mean_eur_kwh)
|
||||
blocks.append(("grid", cheap))
|
||||
sell = rp.sell if pm.sell and pm.can_sell else None
|
||||
for g in rp.give:
|
||||
if sell is not None and g.start <= sell.start < g.end:
|
||||
|
||||
@@ -10,7 +10,8 @@ already fixed. The readings show it: they answer each power request late.
|
||||
house does not move that reading, so a repeating load cannot fool it. It
|
||||
needs the request to move FIT_MOVE_W; one reading per 10 s makes it slow.
|
||||
- `MeterFit`: how long after the site request the meter shows it. Quick at a
|
||||
1-3 s meter, none at a slower one, and a house load that repeats can fool it.
|
||||
1-3 s meter, slower and for long lags only at a 5 s one (SAFETY-129), and a
|
||||
house load that repeats can fool it.
|
||||
The meter counts only when an ask was slow to start and no battery fit said
|
||||
"on time"; with hysteresis (ON_S / OFF_S) the result is `late_seen`. Then (`observe()`, each cycle):
|
||||
- a new command more than ANSWER_W from the battery reading is PENDING until
|
||||
@@ -163,13 +164,22 @@ class Delay:
|
||||
|
||||
|
||||
# The meter's delay: fitted over this much history, from this many new
|
||||
# values (a meter of about 3.5 s or faster: a slower one gives too few to
|
||||
# fit without false delays); late over M_LATE_S (the plant's 6 s settle and one 2 s meter tick),
|
||||
# but only when the batteries' own readings have not just shown them on time.
|
||||
# values (a meter of about 3.5 s or faster; a slower one see M_SLOW_FIT_S);
|
||||
# late over M_LATE_S (the plant's 6 s settle and one 2 s meter tick), but only
|
||||
# when the batteries' own readings have not just shown them on time.
|
||||
M_FIT_S = 60.0
|
||||
M_ROWS = 16
|
||||
M_LATE_S = freshness.PLANT_SETTLE_S + 2.0 # 8.0
|
||||
M_SHARE, M_BETTER = 0.4, 0.7
|
||||
# SAFETY-129: a meter slower than that (a 5 s P1) has fewer than M_ROWS values
|
||||
# in M_FIT_S, so it is fitted over M_SLOW_FIT_S instead, from the same M_ROWS
|
||||
# values. It calls a delay late only over M_SLOW_LATE_S and on ON_FITS fits in
|
||||
# a row. Alone it reads 35-90 s on healthy runs, a few times in a row; what
|
||||
# keeps those from a verdict is the slow-start and battery-fit check in
|
||||
# watch(), not the limit. A 10 s lag is not found: it cannot be told from a
|
||||
# slow battery (R2-1), nor does it swing at 5 s.
|
||||
M_SLOW_FIT_S = 120.0
|
||||
M_SLOW_LATE_S = 16.0
|
||||
# A meter fit counts only after an ask that took this long to start moving a
|
||||
# reading: on time they start within 1-2 s (the recorded house, at 1-5 s).
|
||||
SLOW_START_S = 3.0
|
||||
@@ -183,19 +193,30 @@ class MeterFit:
|
||||
up with an earlier request, so it only counts while the batteries' own
|
||||
fit is not on time (`Answer.watch`)."""
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, fit_s=M_FIT_S, late_s=M_LATE_S):
|
||||
self.fit_s, self.late_s = fit_s, late_s
|
||||
self.cmds = deque() # (t, request) when it changed
|
||||
self.grid = deque() # (t, meter) new values only
|
||||
|
||||
def slow(self):
|
||||
"""The meter is too slow for M_ROWS values in `fit_s`: the usual gap
|
||||
between its new values (median, so a pause does not count) is over
|
||||
fit_s / M_ROWS."""
|
||||
ts = [t for t, _g in self.grid]
|
||||
if len(ts) < 5:
|
||||
return False
|
||||
gaps = sorted(b - a for a, b in zip(ts, ts[1:]))
|
||||
return gaps[len(gaps) // 2] * M_ROWS > self.fit_s
|
||||
|
||||
def add(self, now, cmd, grid):
|
||||
if cmd is not None and (not self.cmds or self.cmds[-1][1] != cmd):
|
||||
self.cmds.append((now, float(cmd)))
|
||||
if grid is not None and (not self.grid or self.grid[-1][1] != grid):
|
||||
self.grid.append((now, float(grid)))
|
||||
while self.grid and self.grid[0][0] < now - M_FIT_S:
|
||||
while self.grid and self.grid[0][0] < now - self.fit_s:
|
||||
self.grid.popleft()
|
||||
while (len(self.cmds) > 1
|
||||
and self.cmds[1][0] <= now - M_FIT_S - MAX_DELAY_S):
|
||||
and self.cmds[1][0] <= now - self.fit_s - MAX_DELAY_S):
|
||||
self.cmds.popleft()
|
||||
|
||||
def estimate(self):
|
||||
@@ -214,9 +235,9 @@ class MeterFit:
|
||||
best = min(range(len(pure)), key=lambda d: (pure[d], d))
|
||||
if g_var <= 0 or pure[best] > (1 - M_SHARE) * g_var:
|
||||
return None
|
||||
if best <= M_LATE_S:
|
||||
if best <= self.late_s:
|
||||
return float(best)
|
||||
if pure[best] > M_BETTER * min(pure[:int(M_LATE_S) + 1]):
|
||||
if pure[best] > M_BETTER * min(pure[:int(self.late_s) + 1]):
|
||||
return None
|
||||
return float(best)
|
||||
|
||||
@@ -236,6 +257,8 @@ class Answer:
|
||||
self._was = None # (command, reading, meter) at the last call
|
||||
self.delay = Delay()
|
||||
self.meter = MeterFit()
|
||||
self.meter_slow = MeterFit(M_SLOW_FIT_S, M_SLOW_LATE_S) # SAFETY-129
|
||||
self._ms_run = 0 # late slow-meter fits in a row
|
||||
self._batt_fit = None # (when, d): the batteries' last fit
|
||||
self._b_run = 0 # late battery fits in a row
|
||||
self._start_age = None # (when, how long the last ask took to start)
|
||||
@@ -279,14 +302,18 @@ class Answer:
|
||||
delays. Late on ON_FITS battery fits from ON_S, or on a meter fit
|
||||
over M_LATE_S when an ask was slow to start (SLOW_START_S) and no
|
||||
battery fit within FIT_S said on time: a house load that repeats can
|
||||
fool the meter fit, not these. On time again on a battery fit under
|
||||
OFF_S, or a meter fit within M_LATE_S unless a battery fit said late.
|
||||
fool the meter fit, not these. A meter too slow for M_ROWS values in
|
||||
M_FIT_S (a 5 s P1) is fitted over M_SLOW_FIT_S and counts only over
|
||||
M_SLOW_LATE_S, on ON_FITS fits in a row. On time again on a battery fit
|
||||
under OFF_S, or a meter fit within M_LATE_S unless a battery fit said
|
||||
late.
|
||||
"""
|
||||
if self._start is None:
|
||||
self._start = now
|
||||
for name, cmd, reading in units:
|
||||
self.delay.add(now, name, cmd, reading)
|
||||
self.meter.add(now, standing_w, grid_w)
|
||||
self.meter_slow.add(now, standing_w, grid_w)
|
||||
if self._fit_at is not None and now - self._fit_at < FIT_EVERY_S:
|
||||
return
|
||||
self._fit_at = now
|
||||
@@ -295,6 +322,9 @@ class Answer:
|
||||
and now - self._exempt_at <= FIT_S:
|
||||
b = None # a battery that stopped following: no late fit
|
||||
m = self.meter.estimate()
|
||||
ms = None
|
||||
if self.meter.slow():
|
||||
ms = self.meter_slow.estimate()
|
||||
if b is not None:
|
||||
self._batt_fit = (now, b)
|
||||
self.shown_s = b if b >= ON_S else None
|
||||
@@ -304,7 +334,13 @@ class Answer:
|
||||
b_late = recent and self._batt_fit[1] >= ON_S
|
||||
|
||||
self._b_run = self._b_run + 1 if (b or 0.0) >= ON_S else 0
|
||||
d = b if b is not None else m
|
||||
self._ms_run = self._ms_run + 1 if (ms or 0.0) > M_SLOW_LATE_S else 0
|
||||
# A slow-meter fit only ever adds a late verdict: it counts as a fit
|
||||
# when it is late on ON_FITS in a row, and never ends one, so a slow
|
||||
# battery (R2-1) runs exactly as before.
|
||||
if self._ms_run < ON_FITS:
|
||||
ms = None
|
||||
d = b if b is not None else m if m is not None else ms
|
||||
if d is None:
|
||||
if self._fit_ok is None or now - self._fit_ok > FIT_GONE_S:
|
||||
self.late_seen = False # nothing to tell it by
|
||||
@@ -312,12 +348,13 @@ class Answer:
|
||||
self.fits += 1
|
||||
self.delay_s, self._fit_ok = d, now
|
||||
warm = now - self._start >= MAX_DELAY_S + FIT_S
|
||||
m_late = (m or 0.0) > M_LATE_S
|
||||
m_late = ((m or 0.0) > M_LATE_S and warm) or (
|
||||
self._ms_run >= ON_FITS
|
||||
and now - self._start >= MAX_DELAY_S + M_SLOW_FIT_S)
|
||||
slow = (self._start_age is not None
|
||||
and now - self._start_age[0] <= M_FIT_S
|
||||
and self._start_age[1] >= SLOW_START_S)
|
||||
if self._b_run >= ON_FITS or (
|
||||
m_late and warm and slow and not b_fresh):
|
||||
if self._b_run >= ON_FITS or (m_late and slow and not b_fresh):
|
||||
self.late_seen = True
|
||||
elif (b is not None and b < OFF_S) or (
|
||||
m is not None and m <= M_LATE_S and not b_late):
|
||||
|
||||
@@ -34,7 +34,9 @@ evening and api.forecast.solar is sometimes out for a poll. A kept reading
|
||||
stands in for its sensor (a) for up to `FRESH_S` when the sensor is missing
|
||||
or unavailable, as a sensor that merely stopped updating is already
|
||||
tolerated, and (b) from the end of the bell (`BELL_TO_H`) until local
|
||||
midnight, if it is at most `KEPT_MAX_AGE_S` old when used.
|
||||
midnight, if it is at most `KEPT_MAX_AGE_S` old when used. A TOMORROW
|
||||
reading needs no evening for (b), at any hour (OPT-38: one Forecast.Solar
|
||||
sensor stopped at 14:54 and tomorrow vanished from 17:54 to 20:00).
|
||||
Never beyond that, and never for another day: a today figure is only today's,
|
||||
a tomorrow figure is tomorrow's until midnight and then, until the bell starts
|
||||
(`BELL_FROM_H`, 06:00 local), today's (OPT-34: at most `KEPT_MORNING_MAX_AGE_S`
|
||||
@@ -330,8 +332,9 @@ class Hold:
|
||||
|
||||
The kept reading must be for `day0`, and either (a) at most `FRESH_S`
|
||||
old, or (b) used at night: from the end of the bell to local midnight,
|
||||
at most `KEPT_MAX_AGE_S` old. `kept` is true when (b) was needed,
|
||||
or `as_today` (OPT-34: a tomorrow figure standing in for today, after
|
||||
at most `KEPT_MAX_AGE_S` old; a "tomorrow" reading at any hour
|
||||
(OPT-38; `read_sum()` flags it kept only from the end of the bell).
|
||||
`kept` is true when (b) was needed, or `as_today` (OPT-34: a tomorrow figure standing in for today, after
|
||||
midnight and until the bell starts, `KEPT_MORNING_MAX_AGE_S`)."""
|
||||
rec = self._r.get((kind, entity, day0))
|
||||
if rec is None:
|
||||
@@ -347,8 +350,8 @@ class Hold:
|
||||
return (day, at, True) if ok else None
|
||||
if age > KEPT_MAX_AGE_S:
|
||||
return None
|
||||
# A tomorrow sensor is asked with day0 = the next midnight, which is
|
||||
# in the future all day, so the evening ends at that day's end.
|
||||
if kind == "tomorrow":
|
||||
return day, at, True # OPT-38: no evening needed
|
||||
evening = _local_hour(now) >= BELL_TO_H and now < _bounds(day0)[1]
|
||||
return (day, at, True) if evening else None
|
||||
|
||||
@@ -556,6 +559,9 @@ def read_sum(today, tomorrow, now: datetime, hold: Hold | None = None):
|
||||
if why0 is not None:
|
||||
return None, why0, gaps
|
||||
tday = _sum(tdays) if tomorrow and len(tdays) == len(tomorrow) else None
|
||||
kept = kept or (tday is not None and tkept)
|
||||
# OPT-38: before the bell ends a kept tomorrow reading is no "kept for
|
||||
# tonight": it must not replace today's line or the log.
|
||||
kept = kept or (tday is not None and tkept
|
||||
and _local_hour(now) >= BELL_TO_H)
|
||||
# The oldest member's report is when the sum was last fully known.
|
||||
return Forecast(_sum(days), tday, min(ats), kept), None, gaps
|
||||
|
||||
@@ -144,8 +144,9 @@ rs(st(5.0, at(4, 9, 5)), st(12.0, at(4, 9, 5)), at(4, 9, 6), h9)
|
||||
for label, tm in (("stale", st(12.0, at(4, 9, 5))), ("unavailable", GONE)):
|
||||
fd = rs(st(3.0, at(4, 13, 55)), tm, at(4, 14, 0), h9)[0]
|
||||
check(f"14:00, today fresh, tomorrow {label} since 09:05 (over 3 h): "
|
||||
"no tomorrow, nothing kept",
|
||||
fd is not None and fd.tomorrow is None and not fd.kept)
|
||||
"the 09:05 figure covers it, kept (OPT-38: at any hour)",
|
||||
fd is not None and fd.tomorrow is not None
|
||||
and fd.tomorrow.kwh == 12.0 and not fd.kept)
|
||||
fn = rs(st(0.1, at(4, 20, 55)), tm, at(4, 21, 0), h9)[0]
|
||||
check(f"21:00, tomorrow {label}: the 09:05 figure covers it, kept",
|
||||
fn is not None and fn.tomorrow is not None
|
||||
|
||||
@@ -186,8 +186,9 @@ check("a fresh figure replaces the kept one at once (04:00: 5.0 today, "
|
||||
and fy.today.kwh == 5.0 and fy.tomorrow.kwh == 9.0)
|
||||
fy2 = rs(st(5.0, at(4, 7, 55)), GONE, at(4, 8, 0), hd)[0]
|
||||
check("... and at 08:00, today fresh and tomorrow's sensor down for over 3 h: "
|
||||
"no tomorrow (the 3rd's figure is not the 5th's)",
|
||||
fy2 is not None and fy2.tomorrow is None and fy2.today.kwh == 5.0)
|
||||
"tomorrow is the 03:50 figure for the 5th (9.0, not flagged kept before 20:00: OPT-38), never "
|
||||
"the 3rd's 12.0", fy2 is not None and fy2.today.kwh == 5.0
|
||||
and fy2.tomorrow is not None and fy2.tomorrow.kwh == 9.0 and not fy2.kept)
|
||||
|
||||
# Restart: the saved hold keeps the figure.
|
||||
for label, now in (("01:00", at(4, 1, 0)), ("04:00", at(4, 4, 0))):
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
"""Runnable check for OPT-38. `python3 test_opt38.py`
|
||||
|
||||
Live 0.20.1, 2026-11-06: one of three Forecast.Solar tomorrow sensors last
|
||||
reported 14:54. From 17:54 (over FRESH_S) to 20:00 (BELL_TO_H) nothing stood in
|
||||
for it, the OPT-17 sum was dropped and the Forecast tab had no tomorrow. A kept
|
||||
tomorrow reading now stands in at any hour (`Hold.stand_in()`). Today members,
|
||||
and the OPT-34 morning rules, are as before.
|
||||
|
||||
Mutations (MUTATION.md, purged copies): see the pull request.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(0, HERE)
|
||||
|
||||
from app import homeview, solarfc # noqa: E402
|
||||
|
||||
fails = []
|
||||
|
||||
|
||||
def check(name, cond):
|
||||
print(f" {'ok ' if cond else 'FAIL'} {name}")
|
||||
if not cond:
|
||||
fails.append(name)
|
||||
|
||||
|
||||
L = timezone(timedelta(hours=1)) # Brussels in winter (CET)
|
||||
|
||||
|
||||
def at(day, hh, mm=0):
|
||||
return datetime(2026, 11, day, hh, mm, tzinfo=L).astimezone(timezone.utc)
|
||||
|
||||
|
||||
def st(kwh, when):
|
||||
return {"state": f"{kwh}", "attributes": {"unit_of_measurement": "kWh"},
|
||||
"last_updated": when.isoformat(), "last_reported": when.isoformat()}
|
||||
|
||||
|
||||
TODAY = [("sensor.t", None)]
|
||||
M = ["sensor.m1", "sensor.m2", "sensor.m3"]
|
||||
|
||||
|
||||
def run(hold, readings, now, today_at=None):
|
||||
t = [("sensor.t", st(0.5, today_at or now - timedelta(minutes=5)))]
|
||||
return solarfc.read_sum(
|
||||
t, [(e, st(k, w)) for e, (k, w) in zip(M, readings)], now, hold)
|
||||
|
||||
|
||||
# The live day: m2 last reported 14:54, the others keep reporting.
|
||||
h = solarfc.Hold()
|
||||
run(h, [(4.0, at(6, 14, 0)), (5.0, at(6, 14, 54)), (6.0, at(6, 14, 0))],
|
||||
at(6, 14, 55))
|
||||
fresh = lambda w: [(4.0, w), (5.0, at(6, 14, 54)), (6.0, w)] # noqa: E731
|
||||
f, why, gaps = run(h, fresh(at(6, 18, 20)), at(6, 18, 30))
|
||||
check("18:30, one member last reported 14:54: tomorrow sum is there (not "
|
||||
"flagged kept before 20:00)",
|
||||
f is not None and f.tomorrow is not None and f.tomorrow.kwh == 15.0
|
||||
and not f.kept and gaps == ())
|
||||
f, _w, _g = run(h, fresh(at(6, 17, 40)), at(6, 17, 50))
|
||||
check("17:50 (just under 3 h): fresh, not kept",
|
||||
f is not None and f.tomorrow.kwh == 15.0 and not f.kept)
|
||||
f, _w, _g = run(h, fresh(at(6, 17, 55)), at(6, 18, 0))
|
||||
check("18:00 (stale since 17:54): tomorrow there",
|
||||
f is not None and f.tomorrow is not None and not f.kept)
|
||||
f, _w, _g = run(h, fresh(at(6, 19, 50)), at(6, 19, 55))
|
||||
check("19:55 (before the bell ends): tomorrow there", f.tomorrow is not None)
|
||||
|
||||
# Too old: over KEPT_MAX_AGE_S (12 h) when used.
|
||||
h2 = solarfc.Hold()
|
||||
run(h2, [(4.0, at(6, 5, 0)), (5.0, at(6, 5, 0)), (6.0, at(6, 5, 0))],
|
||||
at(6, 5, 5))
|
||||
w = at(6, 16, 50)
|
||||
f, why, gaps = run(h2, [(4.0, w), (5.0, at(6, 5, 0)), (6.0, w)],
|
||||
at(6, 17, 10))
|
||||
check("member 12 h 10 min old: no tomorrow sum (OPT-17 rule), today stays",
|
||||
f is not None and f.tomorrow is None
|
||||
and [g[0] for g in gaps] == ["tomorrow"])
|
||||
f, _w, _g = run(h2, [(4.0, w), (5.0, at(6, 5, 0)), (6.0, w)],
|
||||
at(6, 16, 50))
|
||||
check("... and 11 h 50 min old is still kept",
|
||||
f is not None and f.tomorrow is not None and not f.kept)
|
||||
|
||||
# Not a partial sum: a member never read has nothing to stand in.
|
||||
f, _w, gaps = run(solarfc.Hold(), [(4.0, at(6, 18, 0)),
|
||||
(5.0, at(6, 14, 54)),
|
||||
(6.0, at(6, 18, 0))], at(6, 18, 30))
|
||||
check("a member with no kept reading: no tomorrow sum, no partial sum",
|
||||
f is not None and f.tomorrow is None and len(gaps) == 1)
|
||||
|
||||
# Today members are as before: a stale today member at 18:30 is not kept.
|
||||
h3 = solarfc.Hold()
|
||||
solarfc.read_sum([("sensor.t", st(1.0, at(6, 14, 54)))],
|
||||
[(M[0], st(9.0, at(6, 14, 54)))], at(6, 14, 55), h3)
|
||||
f, why, _g = solarfc.read_sum([("sensor.t", st(1.0, at(6, 14, 54)))],
|
||||
[(M[0], st(9.0, at(6, 18, 20)))],
|
||||
at(6, 18, 30), h3)
|
||||
check("stale today member at 18:30: not kept, no forecast",
|
||||
f is None and why == solarfc.STALE)
|
||||
f, _w, _g = solarfc.read_sum([("sensor.t", st(1.0, at(6, 14, 54)))],
|
||||
[(M[0], st(9.0, at(6, 20, 0)))],
|
||||
at(6, 20, 30), h3)
|
||||
check("... and at 20:30 it is kept, as before (the bell has ended)",
|
||||
f is not None and f.kept)
|
||||
|
||||
# After midnight: a tomorrow reading from before midnight is never tomorrow's.
|
||||
h4 = solarfc.Hold()
|
||||
run(h4, [(4.0, at(6, 14, 0)), (5.0, at(6, 14, 0)), (6.0, at(6, 14, 0))],
|
||||
at(6, 14, 5))
|
||||
for label, now in (("00:30", at(7, 0, 30)), ("10:00", at(7, 10, 0)),
|
||||
("15:00", at(7, 15, 0))):
|
||||
f, _w, _g = solarfc.read_sum(
|
||||
[("sensor.t", st(2.0, now - timedelta(minutes=5)))],
|
||||
[(e, st(k, at(6, 14, 0))) for e, k in zip(M, (4.0, 5.0, 6.0))],
|
||||
now, h4)
|
||||
check(f"{label} on the 7th: the 6th's tomorrow figure is not the "
|
||||
"7th's tomorrow", f is not None and f.tomorrow is None)
|
||||
|
||||
# Before 20:00 a kept tomorrow reading is not "kept for tonight": today's
|
||||
# line stays the normal one (EN and NL) and the event log (main.py, from
|
||||
# `fc.kept`) has no "kept".
|
||||
h5 = solarfc.Hold()
|
||||
solarfc.read_sum([("t", st(3.0, at(6, 9, 0)))], [("m", st(12.0, at(6, 9, 5)))],
|
||||
at(6, 9, 6), h5)
|
||||
for hh in (14, 18, 19):
|
||||
f, _w, _g = solarfc.read_sum([("t", st(2.5, at(6, hh - 1, 55)))],
|
||||
[("m", st(12.0, at(6, 9, 5)))],
|
||||
at(6, hh, 0), h5)
|
||||
check(f"{hh}:00, today fresh, tomorrow kept from 09:05: tomorrow there, "
|
||||
"not flagged kept", f is not None and f.tomorrow is not None
|
||||
and f.tomorrow.kwh == 12.0 and f.kept is False)
|
||||
f, _w, _g = solarfc.read_sum([("t", st(2.5, at(6, 13, 55)))],
|
||||
[("m", st(12.0, at(6, 9, 5)))], at(6, 14, 0), h5)
|
||||
for lang in ("en", "nl"):
|
||||
with homeview.in_lang(lang):
|
||||
line = homeview.line_solar(f.today.kwh, f.today.kwh * 0.7,
|
||||
homeview.hhmm(f.at), kept=f.kept)
|
||||
kept_line = homeview.line_solar(1, 1, homeview.hhmm(f.at), kept=True)
|
||||
print(f" {lang}: {line!r}")
|
||||
if lang == "en":
|
||||
check("... EN says 'Solar still to come today'",
|
||||
line.startswith("Solar still to come today"))
|
||||
check(f"14:00 Home line ({lang}) is the normal line, not the kept one",
|
||||
line != kept_line and "2,5" in line.replace(".", ",")
|
||||
and "13:55" in line)
|
||||
f, _w, _g = solarfc.read_sum([("t", st(0.2, at(6, 20, 55)))],
|
||||
[("m", st(12.0, at(6, 9, 5)))], at(6, 21, 0), h5)
|
||||
check("21:00 as before: tomorrow kept from 09:05 is flagged kept",
|
||||
f is not None and f.kept and f.tomorrow.kwh == 12.0)
|
||||
|
||||
print()
|
||||
if fails:
|
||||
print(f"{len(fails)} FAILED:")
|
||||
for f in fails:
|
||||
print(" -", f)
|
||||
sys.exit(1)
|
||||
print("all checks passed")
|
||||
@@ -229,9 +229,11 @@ late5 = R.Rig(lag_s=30.0, lag_on=ON, lag_off=ON + 300.0, p1_s=5.0)
|
||||
late5.run(ON + 300.0, lambda rig, c, t: rows5.extend(
|
||||
str(r["text"]) for r in c.checks() if r.get("id") == "late"))
|
||||
print(f" 5 s meter, 30 s late: {sorted(set(rows5))}")
|
||||
check("at a 5 s meter (the batteries' fit) the row names the delay: about "
|
||||
"20-32 s for 30 s late",
|
||||
rows5 and set(rows5) <= _ok_texts and any("about" in x for x in rows5))
|
||||
# SAFETY-129: the 5 s meter's fit finds it first (no figure); the hold then
|
||||
# leaves the batteries' fit nothing to measure, so the row names no delay.
|
||||
check("at a 5 s meter the row says the readings are late, with no figure "
|
||||
"(the meter alone found it)",
|
||||
rows5 and set(rows5) == {str(messages.row_readings_late(None))})
|
||||
check("logged once for the episode, and once when it ends, not per cycle",
|
||||
n_late == 1 and n_back == 1)
|
||||
check("the row goes once a fit shows the readings on time, or 2 minutes "
|
||||
|
||||
@@ -0,0 +1,238 @@
|
||||
"""Runnable check for SAFETY-129: late Home Assistant readings found at a 5 s meter.
|
||||
`python3 test_safety129.py`
|
||||
|
||||
SAFETY-128 fitted the meter's delay only at a meter of about 3.5 s or faster
|
||||
(16 new values in 60 s). The owner's P1 reports every 5 s, so there only the
|
||||
batteries' fit ran: with readings 30 s late the loop swung to -420 W export
|
||||
and was found after about 3 minutes (-1 325 W before the hold).
|
||||
`lateness.MeterFit` now also runs at a slow meter over 120 s, and calls a
|
||||
delay late only over `M_SLOW_LATE_S` on two fits in a row, with the same
|
||||
corroboration as before (an ask slow to start, no battery fit on time).
|
||||
DECISIONS/SAFETY-128-late-readings.md.
|
||||
|
||||
The REAL Controller on TEST-23's plant (tools/safety128_rig.py).
|
||||
|
||||
1. 5 s meter, readings 30 s late (N=2 house 1 800 W, N=1 house 1 500 W): found
|
||||
within 100 s, and the export never goes below -200 W (red on c66aa7f: found
|
||||
after 190 s, export to -420 W).
|
||||
2. 5 s meter, readings 10 s late: no swing, nothing to hold; and the limit that
|
||||
goes with it: a battery that answers in 10 s (R2-1) is still not read as
|
||||
late at a 5 s meter, which a lower slow-meter threshold would break.
|
||||
3. No false holds at a slow meter: the owner's shape at 2, 5 and 10 s, HA
|
||||
restarts and meter pauses at 2 and 5 s, 8-10 s dead times: never
|
||||
late, so the commands are those of the run without the gate.
|
||||
4. The slow-meter fit at its edges.
|
||||
|
||||
Mutations (MUTATION.md, purged copies) are listed in the pull request.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(0, HERE)
|
||||
sys.path.insert(0, os.path.join(HERE, "tools"))
|
||||
logging.disable(logging.WARNING)
|
||||
|
||||
import safety128_rig as R # noqa: E402
|
||||
import app.main as app_main # noqa: E402
|
||||
from app import lateness # noqa: E402
|
||||
|
||||
fails = []
|
||||
|
||||
|
||||
def check(name, cond):
|
||||
print(f" {'ok ' if cond else 'FAIL'} {name}")
|
||||
if not cond:
|
||||
fails.append(name)
|
||||
|
||||
|
||||
def no_gate(fn):
|
||||
"""`fn()` with the answer gate off: the tree before SAFETY-128."""
|
||||
real = app_main.Controller._readings_late
|
||||
app_main.Controller._readings_late = lambda self, standing_w: None
|
||||
try:
|
||||
return fn()
|
||||
finally:
|
||||
app_main.Controller._readings_late = real
|
||||
|
||||
|
||||
def run_late(**kw):
|
||||
"""Run the rig; (rig, seconds the verdict stood, first second it stood)."""
|
||||
dur = kw.pop("dur")
|
||||
seen = []
|
||||
r = R.Rig(**kw)
|
||||
r.run(dur, lambda rig, c, t: seen.append(t) if c.answer.late_seen
|
||||
else None)
|
||||
return r, len(seen), min(seen) if seen else None
|
||||
|
||||
|
||||
def grid_min(rows, t0, t1):
|
||||
return min(r[1] for r in rows if t0 <= r[0] < t1)
|
||||
|
||||
|
||||
AB, A = ("a", "b"), ("a",)
|
||||
ON = 300.0
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
print("1. 5 s meter, readings 30 s late for 300 s")
|
||||
for units, house in ((AB, 1800.0), (A, 1500.0)):
|
||||
n = f"N={len(units)}"
|
||||
r, secs, first = run_late(house=house, lag_s=30.0, lag_on=ON,
|
||||
lag_off=ON + 300.0, units=units, p1_s=5.0,
|
||||
dur=ON + 420.0)
|
||||
low = grid_min(r.rows, ON, ON + 300.0)
|
||||
print(f" {n}: found {None if first is None else first - ON:.0f} s "
|
||||
f"after the start, export to {low:.0f} W")
|
||||
check(f"{n}: found within 100 s of the lag starting",
|
||||
first is not None and first - ON <= 100.0)
|
||||
check(f"{n}: the export never goes below -200 W", low >= -200.0)
|
||||
if len(units) == 2:
|
||||
old = R.Rig(house=house, lag_s=30.0, lag_on=ON, lag_off=ON + 300.0,
|
||||
units=units, p1_s=5.0)
|
||||
no_gate(lambda: old.run(ON + 300.0))
|
||||
check(f"{n}: without the gate the export goes below -1 kW",
|
||||
grid_min(old.rows, ON, ON + 300.0) < -1000.0)
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
print("\n2. 5 s meter, readings 10 s late; a battery that answers in 10 s")
|
||||
r, secs, first = run_late(house=1800.0, lag_s=10.0, lag_on=ON,
|
||||
lag_off=ON + 150.0, units=AB, p1_s=5.0,
|
||||
dur=ON + 200.0)
|
||||
low = grid_min(r.rows, ON, ON + 150.0)
|
||||
print(f" 10 s late: export to {low:.0f} W, verdict stood {secs} s")
|
||||
check("10 s late: no swing, the export never goes below -200 W", low >= -200.0)
|
||||
|
||||
|
||||
def owner(t):
|
||||
w = 400.0
|
||||
if 600 <= t < 780:
|
||||
w += 2200
|
||||
if 1200 <= t < 2400 and ((t - 1200) % 105) < 45:
|
||||
w += 2000
|
||||
return w
|
||||
|
||||
|
||||
for dead in (8.0, 10.0):
|
||||
r, secs, first = run_late(house=owner, noise=80.0, units=AB, p1_s=5.0,
|
||||
dead_s=dead, dur=800.0)
|
||||
print(f" {dead:.0f} s dead time, 5 s meter: late {secs} s")
|
||||
check(f"{dead:.0f} s dead time at a 5 s meter is not read as late "
|
||||
f"(R2-1 does not get worse)", secs == 0)
|
||||
|
||||
# At a 6 s meter an 8 s dead time reads late on the slow fit alone; only the
|
||||
# slow-start and battery-fit check keeps it from a verdict (122 s late
|
||||
# without the check, from 1 084 s).
|
||||
r, secs, first = run_late(house=owner, noise=80.0, units=AB, p1_s=6.0,
|
||||
dead_s=8.0, dur=1500.0)
|
||||
print(f" 8 s dead time, 6 s meter: late {secs} s")
|
||||
check("an 8 s dead time at a 6 s meter is never late (the slow fit needs an "
|
||||
"ask slow to start and no battery fit on time)", secs == 0)
|
||||
|
||||
# A 12 s dead time is already read as late (R2-1, SAFETY-128); the slow-meter
|
||||
# fit must not make that longer. The same run with the slow fit off is the
|
||||
# tree before this card.
|
||||
_slow = lateness.MeterFit.slow
|
||||
secs = {}
|
||||
for label in ("with", "without"): # 12 s dead time: late from about 140 s
|
||||
lateness.MeterFit.slow = _slow if label == "with" else (lambda self: False)
|
||||
try:
|
||||
secs[label] = run_late(house=owner, noise=80.0, units=A, p1_s=5.0,
|
||||
dead_s=12.0, dur=900.0)[1]
|
||||
finally:
|
||||
lateness.MeterFit.slow = _slow
|
||||
print(f" 12 s dead time, N=1, 5 s meter: late {secs['with']} s, with the "
|
||||
f"slow fit off {secs['without']} s")
|
||||
check("a 12 s dead time at a 5 s meter is late no longer than before "
|
||||
"(R2-1 does not get worse)", secs["with"] <= secs["without"])
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
print("\n3. no false holds")
|
||||
_view = R.Plant.view
|
||||
|
||||
|
||||
def frozen(windows, meter_only):
|
||||
def view(self, t, lag_s, lag_on, lag_off):
|
||||
cur = _view(self, t, lag_s, lag_on, lag_off)
|
||||
for a, b in windows:
|
||||
if a <= t < b:
|
||||
for row in reversed(self.hist):
|
||||
if row[0] <= a + 1e-9:
|
||||
old = row[1:]
|
||||
return (old[0], old[1], cur[2], cur[3]) \
|
||||
if meter_only else old
|
||||
return cur
|
||||
return view
|
||||
|
||||
|
||||
for p1 in (2.0, 5.0, 10.0):
|
||||
kw = dict(house=owner, noise=80.0, units=AB, p1_s=p1)
|
||||
r, secs, first = run_late(dur=800.0, **kw)
|
||||
new = r
|
||||
old = R.Rig(**kw)
|
||||
no_gate(lambda: old.run(800.0))
|
||||
same = max(abs(x[2] - y[2]) for x, y in zip(new.rows, old.rows))
|
||||
print(f" owner shape, {p1:.0f} s meter: late {secs} s, command "
|
||||
f"difference {same:.0f} W")
|
||||
check(f"{p1:.0f} s meter, owner shape: never late, same commands",
|
||||
secs == 0 and same == 0)
|
||||
for label, windows, meter_only in (
|
||||
("HA restart", ((650.0, 710.0),), False),
|
||||
("meter pause", ((640.0, 700.0),), True)):
|
||||
for p1 in (2.0, 5.0):
|
||||
R.Plant.view = frozen(windows, meter_only)
|
||||
try:
|
||||
r, secs, first = run_late(house=owner, noise=80.0, units=AB,
|
||||
p1_s=p1, dur=800.0)
|
||||
finally:
|
||||
R.Plant.view = _view
|
||||
check(f"{label} at a {p1:.0f} s meter: never late", secs == 0)
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
print("\n4. the slow-meter fit at its edges")
|
||||
|
||||
|
||||
def meter_with(period, pause_after=None, pause_s=40.0, n=24):
|
||||
f = lateness.MeterFit()
|
||||
f.add(0.0, 100.0, 0.0)
|
||||
for k in range(1, n):
|
||||
gap = pause_s if pause_after is not None and k > pause_after else 0.0
|
||||
f.add(period * k + gap, None, float(k))
|
||||
return f
|
||||
|
||||
|
||||
check("a 5 s and a 10 s meter are slow",
|
||||
meter_with(5.0).slow() and meter_with(10.0).slow())
|
||||
check("a 1 s, a 2 s and a 3 s meter are not slow",
|
||||
not any(meter_with(p, n=60).slow() for p in (1.0, 2.0, 3.0)))
|
||||
check("a pause in a 2 s meter does not make it slow",
|
||||
not meter_with(2.0, pause_after=10, n=40).slow())
|
||||
def fed(values):
|
||||
"""Answer.watch() with the slow fit reading `values`, one per fit."""
|
||||
a = lateness.Answer()
|
||||
a.meter.slow = lambda: True
|
||||
a.meter.estimate = a.delay.estimate = lambda: None
|
||||
it = iter(values)
|
||||
a.meter_slow.estimate = lambda: next(it, None)
|
||||
for k in range(len(values)):
|
||||
a.watch(2.0 * k, [], 0.0, 0.0)
|
||||
return a
|
||||
|
||||
|
||||
check("one late slow-meter fit is no fit; two in a row are one, and "
|
||||
"one on time or in between is none",
|
||||
fed([30.0]).fits == 0 and fed([30.0, 30.0]).fits == 1
|
||||
and fed([30.0, None, 30.0]).fits == 0 and fed([30.0, 12.0]).fits == 0
|
||||
and fed([3.0, 3.0]).fits == 0 and not fed([30.0, 30.0]).late_seen)
|
||||
check("the slow fit asks for a longer window and a later delay than the "
|
||||
"fast one", lateness.M_SLOW_FIT_S > lateness.M_FIT_S
|
||||
and lateness.M_SLOW_LATE_S > lateness.M_LATE_S)
|
||||
check("the slow fit's late limit is above the fast one's and above a "
|
||||
"10 s lag", lateness.M_SLOW_LATE_S >= 16.0)
|
||||
|
||||
print()
|
||||
if fails:
|
||||
print(f"{len(fails)} FAILED: " + ", ".join(fails))
|
||||
sys.exit(1)
|
||||
print("all checks passed")
|
||||
@@ -39,7 +39,8 @@ sys.path.insert(0, os.path.join(HERE, "tools"))
|
||||
logging.disable(logging.WARNING)
|
||||
|
||||
import ux54_scenarios as S # noqa: E402
|
||||
from app import arbitrage, dayplan, dayview, homeview, modetrade # noqa: E402
|
||||
from app import arbitrage, cheaproll, dayplan, dayview, homeview # noqa: E402
|
||||
from app import modetrade # noqa: E402
|
||||
from app import packmode, packshare, peakguard, solarfc, uitext # noqa: E402
|
||||
from app.main import pack_energy_of # noqa: E402
|
||||
from app.price import Uncertainty # noqa: E402
|
||||
@@ -232,11 +233,19 @@ END_S = S.brussels(END).strftime("%H:%M")
|
||||
check(f"the cheap run is 01:00 to {END_S} (02:30 for this house)",
|
||||
c_n.mode_runs.cheap.start == at(1, 0, 0) and END == at(2, 30, 0))
|
||||
nb = tab(c_n, at(0, 18))["days"][0]["batteries"]
|
||||
check("in the night before the cheap run both charge from the grid in it "
|
||||
f"(01:00 to {END_S}), then give at 17:00",
|
||||
all([r["kind"] for r in b["blocks"]] == ["grid", "give"] for b in nb)
|
||||
and all(b["blocks"][0]["from"] == "01:00"
|
||||
and b["blocks"][0]["to"] == END_S for b in nb))
|
||||
# UX-77: today's block runs to where control stops charging: the 1.5 h run is
|
||||
# sized at the full rating, the peak limit leaves 3.1 kW, so control plans the
|
||||
# next run when it ends (`cheaproll`), and each battery's block ends at its stop.
|
||||
RL = cheaproll.for_today(c_n, c_n.mode_runs, at(0, 18))
|
||||
ENDS = [S.brussels(RL.end_for(inv.name, END)).strftime("%H:%M")
|
||||
for inv in c_n.inverters]
|
||||
check("in the night before the cheap run both charge from the grid from "
|
||||
f"01:00 to where control stops ({ENDS}, the run rolls on after "
|
||||
f"{END_S}), then give at 17:00",
|
||||
RL is not None and RL.end > END and ENDS[0] < ENDS[1]
|
||||
and all([r["kind"] for r in b["blocks"]] == ["grid", "give"] for b in nb)
|
||||
and all(b["blocks"][0]["from"] == "01:00" for b in nb)
|
||||
and [b["blocks"][0]["to"] for b in nb] == ENDS)
|
||||
# UX-53: no grid block while the gate refuses.
|
||||
for why in ("peak", "not_worth"):
|
||||
c_n.mode_gate = why
|
||||
@@ -255,12 +264,16 @@ def at_h(b, h):
|
||||
|
||||
|
||||
LVL_A = c_n.plan_targets()[c_n.inverters[0].name]
|
||||
END_HS = [(S.brussels(RL.end_for(inv.name, END)).replace(tzinfo=None)
|
||||
- S.brussels(DAY0).replace(tzinfo=None)).total_seconds() / 3600.0
|
||||
for inv in c_n.inverters]
|
||||
check(f"the curve charges in a grid block: A (planned level {LVL_A:.0f} %) "
|
||||
"and B (Cheap hours, to the end of its run) are well above where they "
|
||||
"were at 01:00, and stay put after the run",
|
||||
all(at_h(b, END_H) - at_h(b, 1.0) > 8.0
|
||||
and abs(at_h(b, END_H + 0.5) - at_h(b, END_H)) < 1e-6
|
||||
for b in nb) and nb[0]["levels"][0][1] == 30.0)
|
||||
"and B (Cheap hours, to the end of its block) are well above where "
|
||||
"they were at 01:00, and stay put after the block",
|
||||
all(at_h(b, eh) - at_h(b, 1.0) > 8.0
|
||||
and all(abs(v - at_h(b, eh)) < 1e-6 for x, v in b["levels"]
|
||||
if eh + 1e-6 < x < eh + 3.0)
|
||||
for b, eh in zip(nb, END_HS)) and nb[0]["levels"][0][1] == 30.0)
|
||||
# Modes off: the verdict's legs.
|
||||
c_f = S.make("off", at(10, 7))
|
||||
fb = tab(c_f, at(10, 7))
|
||||
|
||||
@@ -0,0 +1,358 @@
|
||||
"""UX-77: Home draws today's grid charge for the hours control will charge.
|
||||
|
||||
The real `Controller._mode_roles()` and `homeview.build()`, stepped each
|
||||
minute over a flat night (the walk of the investigation): at five times, every
|
||||
minute control charges battery X in that local day lies inside X's drawn grid
|
||||
block, the block does not run past the hour control stops charging X by more
|
||||
than one slot, and the chart's cheap bars cover every hour with a charge.
|
||||
The peak-bound night is the one that needs the peak limit's room (a mutant that
|
||||
rolls at the full rating fails it, part 3); dynamic prices draw what they drew
|
||||
before (part 4). Mutations (MUTATION.md, purged copies): listed in the pull
|
||||
request. Run: python3 test_ux77.py
|
||||
"""
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(0, HERE)
|
||||
sys.path.insert(0, os.path.join(HERE, "tools"))
|
||||
logging.disable(logging.WARNING)
|
||||
|
||||
import ux54_scenarios as S # noqa: E402
|
||||
from app import dayplan, homeview, modetrade, packmode, packshare # noqa: E402
|
||||
from app.main import Controller # noqa: E402
|
||||
from app.peakguard import PeakDecision # noqa: E402
|
||||
from app.static_tariff import static_plan # noqa: E402
|
||||
from app.store import unit_key # noqa: E402
|
||||
from app.tariff import BILLING_TZ # noqa: E402
|
||||
|
||||
try: # not on the base, where it is red
|
||||
from app import cheaproll
|
||||
except ImportError:
|
||||
cheaproll = None
|
||||
|
||||
UTC = timezone.utc
|
||||
EFF = 0.95
|
||||
AIM = 3450.0
|
||||
SLOT = timedelta(hours=1) # the price slot of the day/night tariff
|
||||
QUARTER = timedelta(minutes=15) # the run's own slot
|
||||
SAMPLES = ("23:30", "00:30", "01:30", "02:30", "04:30")
|
||||
OLD = ("leftover_solar", "cheap_hours") # the owner's old shape
|
||||
NOW = ("leftover_solar", "leftover_solar") # the current one
|
||||
PASS = FAIL = 0
|
||||
|
||||
|
||||
def check(name, ok, detail=""):
|
||||
global PASS, FAIL
|
||||
if ok:
|
||||
PASS += 1
|
||||
else:
|
||||
FAIL += 1
|
||||
print(f"FAIL {name} {detail}")
|
||||
|
||||
|
||||
def no_ties(h):
|
||||
"""Two dips, no two slots at the same price: nothing rolls on."""
|
||||
return (0.05 if 1.0 <= h < 2.0 else 0.08 if 5.0 <= h < 6.0
|
||||
else 0.30) + 0.0001 * h
|
||||
|
||||
|
||||
def shape(night=None):
|
||||
d = {h: 0.973 * (600.0 if h < 6 or h >= 22 else 1200.0 if h < 10
|
||||
else 900.0 if h < 17 else 1800.0) for h in range(24)}
|
||||
if night is not None:
|
||||
for h in list(range(0, 7)) + [22, 23]:
|
||||
d[h] = night
|
||||
return d
|
||||
|
||||
|
||||
def profile(sh, now):
|
||||
d = now.astimezone(BILLING_TZ).date()
|
||||
return dayplan.HouseProfile({(d - timedelta(days=k)).isoformat():
|
||||
{str(h): w for h, w in sh.items()} for k in (1, 2, 3)})
|
||||
|
||||
|
||||
def loc(t):
|
||||
return t.astimezone(BILLING_TZ)
|
||||
|
||||
|
||||
def hm(s, day):
|
||||
h, m = map(int, s.split(":"))
|
||||
return day.replace(hour=h, minute=m)
|
||||
|
||||
|
||||
def split_by_room(w, ch, rating, wt, invs):
|
||||
"""Control's split of `w` over the chargers `ch`: by `packshare.weights()`,
|
||||
no battery over its rating, the surplus to the others."""
|
||||
p, open_, left = [0.0, 0.0], set(ch), w
|
||||
while open_:
|
||||
tot = sum(wt[invs[i].name] for i in open_)
|
||||
over = [i for i in open_
|
||||
if left * wt[invs[i].name] / tot > rating[i] + 1e-9]
|
||||
if not over:
|
||||
for i in open_:
|
||||
p[i] = left * wt[invs[i].name] / tot
|
||||
break
|
||||
for i in over:
|
||||
p[i] = rating[i]
|
||||
left -= rating[i]
|
||||
open_.discard(i)
|
||||
return p
|
||||
|
||||
|
||||
def walk(modes, soc0, house_w, night=None, dynamic=False, samples=SAMPLES,
|
||||
start_h=22, end_h=7, plain=None, by_room=True, fc=(10.2, 10.0)):
|
||||
"""Walk 22:00-07:15. Returns (charging, homes, commands):
|
||||
charging[minute] = set of batteries control charges (power > 0),
|
||||
homes[sample] = Home's `/status` plan at that minute, commands = the
|
||||
(minute, unit, role, size) control gave."""
|
||||
opts = dict(S.BASE)
|
||||
opts.update(battery_modes_enabled=True,
|
||||
pv_forecast_entities=[f"{S.FC1}_{i}" for i in range(3)],
|
||||
pv_forecast_tomorrow_entities=[f"{S.FCT1}_{i}"
|
||||
for i in range(3)],
|
||||
static_profile=json.dumps(S.DN), arbitrage_enabled=False,
|
||||
arbitrage_soc_floor=20, arbitrage_soc_ceiling=95,
|
||||
cycle_cost_threshold_eur=0.04)
|
||||
c = Controller(opts, S.Hass(), S.Store(), S.Pub(), stop=S.asyncio.Event())
|
||||
c.auto = True
|
||||
soc = list(soc0)
|
||||
caps = [inv.capacity_kwh for inv in c.inverters]
|
||||
rating = [inv.max_w for inv in c.inverters]
|
||||
for i, inv in enumerate(c.inverters):
|
||||
inv.batt, inv.last_write = 0.0, 0.0
|
||||
inv.dev_min, inv.dev_max = -5000.0, 5000.0
|
||||
c.store.data[unit_key(inv.name, packmode.MODE_KEY)] = modes[i]
|
||||
c.pv_w = 0.0
|
||||
c.peak_guard = PeakDecision(engaged=False, floor_w=None, ceiling_w=7000.0,
|
||||
ceiling_from="cap", projected_w=800.0)
|
||||
start = datetime(2026, 11, 1, start_h, 0, tzinfo=BILLING_TZ).astimezone(UTC)
|
||||
end = datetime(2026, 11, 2, end_h, 15, tzinfo=BILLING_TZ).astimezone(UTC)
|
||||
c.house_profile = profile(shape(night), start)
|
||||
now, plan_at = start, None
|
||||
charging, homes, cmds = {}, {}, []
|
||||
while now < end:
|
||||
lt = loc(now)
|
||||
today, tomorrow = fc if lt.hour < 12 else fc[::-1]
|
||||
c.solar_fc = S.forecast(now, today, tomorrow, members=3)
|
||||
c.solar_fc_why = None
|
||||
if plan_at is None or (now - plan_at).total_seconds() >= 1800:
|
||||
c.tariff_plan = (S.quarters(now, dynamic) if dynamic else
|
||||
static_plan(opts, now, now + timedelta(hours=48),
|
||||
now=now))
|
||||
plan_at = now
|
||||
c.tariff_at = plan_at
|
||||
for i, inv in enumerate(c.inverters):
|
||||
inv.soc = round(soc[i], 1)
|
||||
room = AIM - house_w
|
||||
c.arb_headroom_w = room
|
||||
c.grid = house_w
|
||||
tick = c._mode_roles(now, None)
|
||||
size = modetrade.size(tick, (-sum(rating), sum(rating)), room) \
|
||||
if tick.action else 0.0
|
||||
ch = [i for i, inv in enumerate(c.inverters)
|
||||
if tick.roles.get(inv.name) == modetrade.CHARGE]
|
||||
p = [0.0, 0.0]
|
||||
if size < 0 and ch:
|
||||
if by_room: # as control: room left to full, rating caps
|
||||
wt = packshare.weights(c.pack_energy, -1.0)
|
||||
p = split_by_room(-size, ch, rating, wt, c.inverters)
|
||||
else:
|
||||
tot = sum(rating[i] for i in ch)
|
||||
for i in ch:
|
||||
p[i] = -size * rating[i] / tot
|
||||
for i in range(2):
|
||||
soc[i] = min(100.0, soc[i] + p[i] / 1000.0 / 60.0 * EFF
|
||||
/ caps[i] * 100.0)
|
||||
charging[lt.replace(second=0, microsecond=0)] = {
|
||||
i for i in range(2) if p[i] > 0.0}
|
||||
cmds.append((lt.strftime("%H:%M"), tick.action,
|
||||
tuple(sorted(tick.roles.items())), round(size)))
|
||||
if lt.strftime("%H:%M") in samples:
|
||||
homes[lt.replace(second=0, microsecond=0)] = homeview.build(
|
||||
c, level="ok", checks=[], now=now, lang="en")
|
||||
if plain is not None: # the same, with no roll
|
||||
real = cheaproll.for_today
|
||||
cheaproll.for_today = lambda *_a: None
|
||||
try:
|
||||
plain[lt.replace(second=0, microsecond=0)] = \
|
||||
homeview.build(c, level="ok", checks=[], now=now,
|
||||
lang="en")
|
||||
finally:
|
||||
cheaproll.for_today = real
|
||||
now += timedelta(minutes=1)
|
||||
return charging, homes, cmds
|
||||
|
||||
|
||||
def blocks_of(st, i):
|
||||
"""Battery i's drawn grid blocks as (start, end) local datetimes."""
|
||||
return [(loc(datetime.fromisoformat(r["start"])),
|
||||
loc(datetime.fromisoformat(r["end"])))
|
||||
for r in st["plan"]["runs"]
|
||||
if r["kind"] == "grid" and r["battery"] == i]
|
||||
|
||||
|
||||
def today_of(st, i):
|
||||
return [(t["from"], t["to"]) for t in st["batteries"][i]["today"]]
|
||||
|
||||
|
||||
def violations(charging, homes, slot=SLOT):
|
||||
"""What is wrong with Home's drawing at each sample."""
|
||||
bad = []
|
||||
for t, st in sorted(homes.items()):
|
||||
day_end = (t.replace(hour=0, minute=0) + timedelta(days=1))
|
||||
mins = {m: who for m, who in charging.items() if t <= m < day_end}
|
||||
for i in range(2):
|
||||
mine = sorted(m for m, who in mins.items() if i in who)
|
||||
bl = blocks_of(st, i)
|
||||
for m in mine:
|
||||
if not any(a <= m < z for a, z in bl):
|
||||
bad.append(f"{t:%H:%M} battery {i}: charges at "
|
||||
f"{m:%H:%M}, not in a block {bl}")
|
||||
break
|
||||
if mine:
|
||||
stop = mine[-1] + timedelta(minutes=1)
|
||||
if slot == QUARTER: # the last charging quarter's end
|
||||
stop += timedelta(minutes=-stop.minute % 15)
|
||||
for a, z in bl:
|
||||
if z > stop + slot:
|
||||
bad.append(f"{t:%H:%M} battery {i}: block to "
|
||||
f"{z:%H:%M}, stops {stop:%H:%M}")
|
||||
# the Today line says the same as the block
|
||||
tl = today_of(st, i)
|
||||
for m in mine:
|
||||
if not any(f <= m.strftime("%H:%M") < (z if z != "24:00"
|
||||
else "99") for f, z in tl):
|
||||
bad.append(f"{t:%H:%M} battery {i}: Today line {tl} "
|
||||
f"has no {m:%H:%M}")
|
||||
break
|
||||
cheap = {b["label"][:2] for b in st["plan"]["prices"]
|
||||
if b["kind"] == "cheap"}
|
||||
for m in mins:
|
||||
if mins[m] and m.strftime("%H") not in cheap:
|
||||
bad.append(f"{t:%H:%M}: charge at {m:%H:%M}, hour not cheap")
|
||||
break
|
||||
return bad
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Run
|
||||
# --------------------------------------------------------------------------- #
|
||||
def show(tag, ch, homes, want_bad, slot=SLOT):
|
||||
bad = violations(ch, homes, slot)
|
||||
if want_bad:
|
||||
check(f"{tag}: red", bool(bad), "no violation found")
|
||||
else:
|
||||
check(f"{tag}: every charging minute is drawn, no block runs on",
|
||||
not bad, "\n " + "\n ".join(bad))
|
||||
return bad
|
||||
|
||||
|
||||
def charged(ch, i, after):
|
||||
return [m for m, who in ch.items() if i in who and m >= after]
|
||||
|
||||
|
||||
def main():
|
||||
global cheaproll
|
||||
if cheaproll is None:
|
||||
print("no app/cheaproll.py: the base; the walk is red there")
|
||||
cheaproll = type("Off", (), {"for_today": staticmethod(lambda *a: None)})
|
||||
|
||||
print("1. the walk: no-bind (0.6 kW house) and peak-bound (2.85 kW house)")
|
||||
CASES = (("old shape, no bind", OLD, (20.0, 20.0), 584.0, None),
|
||||
("current shape, no bind", NOW, (20.0, 20.0), 584.0, None),
|
||||
("old shape, peak-bound", OLD, (40.0, 90.0), 2850.0, 2850.0),
|
||||
("current shape, peak-bound", NOW, (40.0, 90.0), 2850.0, 2850.0))
|
||||
# the rig's checks 2 and 3 (UX-77 round 2): room left and rating disagree
|
||||
UNEQUAL = (("unequal levels, A+B Leftover solar, no bind", NOW,
|
||||
(30.0, 20.0), 584.0, None, (5.0, 5.0)),
|
||||
("unequal levels, A Leftover solar + B Cheap hours", OLD,
|
||||
(40.0, 88.0), 2850.0, 2850.0, (10.2, 10.0)))
|
||||
walked = {}
|
||||
for tag, modes, soc, hw, night in CASES:
|
||||
ch, homes, _cmds = walk(modes, soc, hw, night=night)
|
||||
walked[tag] = (ch, homes)
|
||||
show(tag, ch, homes, False)
|
||||
# the check has something to check: charging goes on past 00:30
|
||||
day = datetime(2026, 11, 2, 0, 30, tzinfo=BILLING_TZ)
|
||||
check(f"{tag}: control charges after 00:30", bool(
|
||||
charged(ch, 0, day) or charged(ch, 1, day)))
|
||||
|
||||
print("2. a battery with nothing to charge gets no block")
|
||||
ch, homes, _c = walk(OLD, (40.0, 94.0), 2850.0, night=2850.0)
|
||||
st = homes[datetime(2026, 11, 2, 2, 30, tzinfo=BILLING_TZ)]
|
||||
check("B is at its level at 02:30: no block", blocks_of(st, 1) == [],
|
||||
str(blocks_of(st, 1)))
|
||||
check("A still has one at 02:30", bool(blocks_of(st, 0)))
|
||||
|
||||
print("3. a roll at the full rating fails the peak-bound night")
|
||||
if hasattr(cheaproll, "_Charge"):
|
||||
real = cheaproll._Charge._room_w
|
||||
cheaproll._Charge._room_w = lambda self, hour, total: total
|
||||
try:
|
||||
for tag, modes, soc, hw, night in CASES[2:]:
|
||||
ch, homes, _c = walk(modes, soc, hw, night=night)
|
||||
show(f"mutant, {tag}", ch, homes, True)
|
||||
finally:
|
||||
cheaproll._Charge._room_w = real
|
||||
|
||||
print("4. dynamic prices: Home is what it was")
|
||||
for tag, modes, soc, hw, night in (CASES[2], CASES[3], CASES[1]):
|
||||
plain = {}
|
||||
ch, homes, _c = walk(modes, soc, hw, night=night, dynamic=no_ties,
|
||||
plain=plain)
|
||||
same = all(json.dumps(homes[t], sort_keys=True, default=str)
|
||||
== json.dumps(plain[t], sort_keys=True, default=str)
|
||||
for t in homes)
|
||||
check(f"dynamic, {tag}: same as with no roll", same)
|
||||
check(f"dynamic, {tag}: and there are grid blocks to compare",
|
||||
any(r["kind"] == "grid" for st in homes.values()
|
||||
for r in st["plan"]["runs"]))
|
||||
|
||||
print("5. unequal levels: the roll shares the room as control does")
|
||||
for tag, modes, soc, hw, night, fc in UNEQUAL:
|
||||
ch, homes, _c = walk(modes, soc, hw, night=night, fc=fc)
|
||||
show(tag, ch, homes, False, QUARTER)
|
||||
day = datetime(2026, 11, 2, 0, 30, tzinfo=BILLING_TZ)
|
||||
check(f"{tag}: control charges after 00:30", bool(
|
||||
charged(ch, 0, day) or charged(ch, 1, day)))
|
||||
if hasattr(cheaproll, "_Charge") \
|
||||
and hasattr(cheaproll._Charge, "_shares"):
|
||||
real = cheaproll._Charge._shares
|
||||
cheaproll._Charge._shares = lambda self, ch, w: {
|
||||
p.name: w * abs(p.rating_w) / sum(abs(q.rating_w) for q in ch)
|
||||
for p in ch}
|
||||
try:
|
||||
for tag, modes, soc, hw, night, fc in UNEQUAL:
|
||||
ch, homes, _c = walk(modes, soc, hw, night=night, fc=fc)
|
||||
show(f"mutant by rating, {tag}", ch, homes, True, QUARTER)
|
||||
finally:
|
||||
cheaproll._Charge._shares = real
|
||||
|
||||
print("6. the pieces")
|
||||
if hasattr(cheaproll, "Roll"):
|
||||
t = datetime(2026, 11, 2, 1, 46, tzinfo=UTC)
|
||||
check("a stop is rounded up to the slot", cheaproll._ceil_slot(t)
|
||||
== datetime(2026, 11, 2, 2, 0, tzinfo=UTC))
|
||||
t = datetime(2026, 11, 2, 1, 45, tzinfo=UTC)
|
||||
check("a stop on a slot stays", cheaproll._ceil_slot(t) == t)
|
||||
e0 = datetime(2026, 11, 2, 0, 0, tzinfo=UTC)
|
||||
e1 = datetime(2026, 11, 2, 6, 0, tzinfo=UTC)
|
||||
r = cheaproll.Roll(e1, {"a": None, "b": datetime(2026, 11, 2, 2, 0,
|
||||
tzinfo=UTC)})
|
||||
check("a battery still charging ends at the roll", r.end_for("a", e0) == e1)
|
||||
check("a battery at its level ends at its stop",
|
||||
r.end_for("b", e0) == datetime(2026, 11, 2, 2, 0, tzinfo=UTC))
|
||||
check("a battery that does not charge keeps the run's end",
|
||||
r.end_for("c", e0) == e0)
|
||||
check("the chart's bar runs to the last block", r.last(e0) == e1)
|
||||
|
||||
print(f"\n{PASS} passed, {FAIL} failed")
|
||||
return FAIL
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(1 if main() else 0)
|
||||
Reference in New Issue
Block a user