diff --git a/goodwe_controller/DECISIONS/EV-05-solar-mode.md b/goodwe_controller/DECISIONS/EV-05-solar-mode.md index 34f0a69..e89adcf 100644 --- a/goodwe_controller/DECISIONS/EV-05-solar-mode.md +++ b/goodwe_controller/DECISIONS/EV-05-solar-mode.md @@ -85,6 +85,16 @@ inputs (`car_sun_inputs`) and runs the writer in the same background task. were already at full output, and the period landed over the aim (review repro: house 25 900 and 25 950 W, test_ev05.py R4/N4 against a car-Off twin). +## Round 5 (review of 8818768) + +- **Headroom counts only the batteries that can give** (`ctl_car._car_giving`): + above their cut-out, not refusing to give (SAFETY-73), not under a safety + stop, switched on. Their output is compared with their own ratings (and + `guard.able_w` when the guard names it). Before, the site rating still + counted a refusing unit, so a battery that gave its full rating looked like + it had headroom and the cut waited (review repro: battery A refuses, house to + 20 950 W, period 3436 W against the twin's 3363 W). + ## Solar kWh Per cycle in Solar: `min(car power, max(0, spare - room))` Batteries first, diff --git a/goodwe_controller/DOCS.md b/goodwe_controller/DOCS.md index 2cc187d..3726aae 100644 --- a/goodwe_controller/DOCS.md +++ b/goodwe_controller/DOCS.md @@ -3327,14 +3327,14 @@ checks within 10 seconds that the charger took it. current again only if the charger shows a different one. - Off writes nothing, ever. -**Solar mode.** Choose *Solar* on Home (or `Solar` on `select.goodwe_car_mode`) -and where spare sun goes first: **Batteries** or **Car** (`select.goodwe_car_sun_first`). -Both apply at once, with no restart. +**Solar mode.** Choose *Solar* on Home (or `Solar` on +`select.goodwe_car_mode`) and where spare sun goes first: **Batteries** or +**Car** (`select.goodwe_car_sun_first`). Both apply at once, with no restart. - *Spare sun* is solar production less the house, measured without a solar sensor: the car's power, plus what the batteries charge, less what the grid - gives. A grid charge of the batteries, or batteries giving more than the house - uses, is never spare sun. + gives. A grid charge of the batteries, or batteries giving more than the + house uses, is never spare sun. - *Batteries first*: the car gets what is left after the full rating of every battery that may still charge (switched on, not refusing, not full), less *Sun kept back*. So the car gets sun only when the batteries charge at full @@ -3354,18 +3354,18 @@ Both apply at once, with no restart. taken off the car first, down to 0 A if needed. While the batteries can still raise their output, the add-on first waits 15 seconds, so a cloud the batteries catch does not pause the car. It cuts at once when they - cannot: already at full power, at the peak reserve or at their cut-out - level. It also cuts at once in the last 2 minutes of the 15-minute period. A Solar - charge never sets the month's peak. + cannot: already at full power, at the peak reserve, at their cut-out + level or refusing to give. It also cuts at once in the last 2 minutes of + the 15-minute period. A Solar charge never sets the month's peak. - **Every way out of Solar leaves the charger at 0 A**: 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 error or a stuck reading. If the charger does not take the 0 A, the add-on tries again a minute later, three times, then shows *Charger did not take it* and a warning. Once taken, nothing more is written. After the charger - or Home Assistant was away, the - add-on first sets 0 A, then starts again by the rules. A meter that stops - gives a hold for *Pause after*, then 0 A. Choosing Manual leaves it to Manual. + or Home Assistant was away, the add-on first sets 0 A, then starts again + by the rules. A meter that stops gives a hold for *Pause after*, then 0 A. + Choosing Manual leaves it to Manual. - Another writer stops the add-on's writes after 30 seconds, with an amber row and a warning, until you press the mode again or the charger shows the add-on's value again. @@ -3373,24 +3373,23 @@ Both apply at once, with no restart. `sensor.goodwe_car_today_solar_kwh`. Only the sun the car could take counts, not the battery or grid share of a hold, and not Manual. -**The car is never slowed for the peak limit in Manual.** A Manual charge goes on whatever -the 15-minute peak does. With the default `ev_batt_cover` on, the batteries help -cover it down to the peak reserve; a 3-phase 16 A charge (11 kW) can empty two -batteries to the reserve in about 1.5 hours, and the month's peak then rises by -what the car drew. When the car alone is why the batteries cannot hold the -period, the add-on does **not** raise *Battery stopped limiting the peak* or -*Load too high for battery*. It shows a grey note *Car over the peak +**The car is never slowed for the peak limit in Manual.** A Manual charge goes +on whatever the 15-minute peak does. With the default `ev_batt_cover` on, the +batteries help cover it down to the peak reserve; a 3-phase 16 A charge (11 kW) +can empty two batteries to the reserve in about 1.5 hours, and the month's peak +then rises by what the car drew. When the car alone is why the batteries cannot +hold the period, the add-on does **not** raise *Battery stopped limiting the +peak* or *Load too high for battery*. It shows a grey note *Car over the peak limit* once per charging session, a grey row, and says so in the car's *Why*. -This holds only while the car's reading is fresh and the charger's energy -meter confirms it (the meter must have gained at least half of what the power -adds up to over the last minute). A stale, stuck or frozen reading, or a -charger with no energy entity, never hides a real overload of the house: the -normal alarms stand. With `ev_batt_cover` off, the peak -guard protects the house without the car, so it does not spend the batteries on -it. Normal zero-grid control can still discharge them for the car, and a grid -charge of the batteries can bring the period to the peak limit plus the car. -The period can go over by the car alone. When the month's peak was set by a car -charge, Home's peak line says so. +This holds only while the car's reading is fresh and the charger's energy meter +confirms it (the meter must have gained at least half of what the power adds up +to over the last minute). A stale, stuck or frozen reading, or a charger with +no energy entity, never hides a real overload of the house: the normal alarms +stand. With `ev_batt_cover` off, the peak guard protects the house without the +car, so it does not spend the batteries on it. Normal zero-grid control can +still discharge them for the car, and a grid charge of the batteries can bring +the period to the peak limit plus the car. The period can go over by the car +alone. When the month's peak was set by a car charge, Home's peak line says so. **When the charger does not answer.** After 30 seconds the card says *Charger not reachable* and one amber row appears. After 5 minutes one notification @@ -3432,8 +3431,9 @@ automation on. **Home Assistant entities.** With MQTT the add-on adds the sensors `goodwe_car_power`, `goodwe_car_state`, `goodwe_car_today_kwh` and -`goodwe_car_today_solar_kwh`, and the four controls named above. A saved ("retained") command is never applied, so a -restart of Home Assistant cannot change the charger. +`goodwe_car_today_solar_kwh`, and the four controls named above. A saved +("retained") command is never applied, so a restart of Home Assistant cannot +change the charger. ### Site diff --git a/goodwe_controller/app/ctl_car.py b/goodwe_controller/app/ctl_car.py index 0d93d5a..bae7451 100644 --- a/goodwe_controller/app/ctl_car.py +++ b/goodwe_controller/app/ctl_car.py @@ -448,31 +448,44 @@ 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.""" + def _car_giving(self) -> list: + """EV-05: the batteries that can give power now: above their own + cut-out level, not refusing to give (SAFETY-73), not under a safety + stop, and switched on (battery modes).""" + out = [] 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 self.modes_on and not self.pack_mode(inv).use: + continue if float(soc) > float(floor): - return True - return False + out.append(inv) + return out + + def _car_batt_able(self) -> bool: + """EV-05: can a battery still give power at all?""" + return bool(self._car_giving()) def _car_batt_headroom(self, guard) -> bool: - """EV-05 round 4: can the batteries still RAISE their output? A - battery above its cut-out (`_car_batt_able`), the guard neither - saturated nor at the peak reserve, and the batteries' output more - than `HEADROOM_W` under what they can give.""" - if not self._car_batt_able() or guard is None \ - or guard.saturated or guard.floor_limited: + """EV-05 rounds 4 and 5: can the batteries still RAISE their output? + The guard is neither saturated nor at the peak reserve, and the units + that can give (`_car_giving`) give more than `HEADROOM_W` under their + own ratings (and under `guard.able_w` when the guard names it). A unit + that refuses or is stopped counts neither in what is given nor in what + could be.""" + giving = self._car_giving() + if not giving or guard is None or guard.saturated \ + or guard.floor_limited: return False - batt = self.site_batt_w - top = guard.able_w if guard.able_w is not None else self.site_rating_w - if batt is None or top is None: + if any(inv.batt is None for inv in giving): return False - return float(batt) < float(top) - HEADROOM_W + batt = sum(float(inv.batt) for inv in giving) + top = sum(float(inv.rating_w()) for inv in giving) + if guard.able_w is not None: + top = min(top, float(guard.able_w)) + return batt < top - HEADROOM_W def _car_solar_pause(self, mono) -> bool: """EV-05: Solar's own 0 A (a pause, the peak cut, the phase change) diff --git a/goodwe_controller/test_ev05.py b/goodwe_controller/test_ev05.py index b9138f5..e981c5f 100644 --- a/goodwe_controller/test_ev05.py +++ b/goodwe_controller/test_ev05.py @@ -1714,6 +1714,100 @@ for H in (25900.0, 25950.0): cut4 is not None and since4 is not None and cut4["t"] - since4 <= 1 and 900 in q4 and 900 in q4o and q4[900] <= 3400.0 and q4o[900] <= 3400.0) +# =========================================================================== # +print("\nROUND 5. HEADROOM COUNTS ONLY BATTERIES THAT CAN GIVE; EACH CLAUSE DECIDES") +# =========================================================================== # + + +def headroom_at(store): + def f(p, c, t): + g = c.peak_guard + store[t] = (c._car_batt_headroom(g), c.site_batt_w, + None if g is None else (g.saturated, g.floor_limited)) + return f + + +def since_cut(r, t0): + since = next((x["since"] for x in r["trace"] if x["since"] is not None + and x["t"] >= t0), None) + cut = first_cut(r, t0) + return since, (None if cut is None else cut["t"]) + + +# R5a (review round 4 repro): A refuses to give from 850 (SAFETY-73), B gives +# its full 4600 W; the house steps to 8000 W at 905 and 20 950 W at 908. Only +# the full-rating clause, counted over the units that can give, decides: the +# guard is neither saturated (its rating still counts A) nor at the reserve. +def _refuse_a(p, c): + if not getattr(p, "_a_held", False): + p._a_held = True + orig = p.batt_of + p.batt_of = lambda u, at, _o=orig: (min(_o(u, at), 0.0) if u == "a" + else _o(u, at)) + [i for i in c.inverters if i.name.endswith("a")][0].refused = 1 + + +hh5 = steps((0, 500.0), (905, 8000.0), (908, 20950.0)) +EV5 = {t: _refuse_a for t in range(850, 1810)} +hr5 = {} +r5a = S.drive(house=hh5, sun=const(13000.0), first="car", socs=ABLE, + dur=1810, opts={"slew_w": 0}, events=dict(EV5), + on_cycle=headroom_at(hr5)) +r5ao = S.drive(house=hh5, sun=const(13000.0), first="car", socs=ABLE, + dur=1810, opts={"slew_w": 0}, events=dict(EV5), mode="off") +q5 = {q: g for q, g, _c in quarters(r5a, 3400.0)} +q5o = {q: g for q, g, _c in quarters(r5ao, 3400.0)} +s5, c5 = since_cut(r5a, 905) +# B can still raise its output for a few seconds after the first step; the +# moment it gives its full rating there is no headroom, and the cut must come +# then, not 15 s after the guard first wanted power. +nohead5 = next((t for t in sorted(hr5) if t >= 905 and not hr5[t][0]), None) +sat5 = None if nohead5 is None else hr5[nohead5][2] +check(f"R5a/N4 battery A refuses to give, B at its full 4600 W: headroom ends " + f"at {nohead5} (guard (saturated, floor-limited) {sat5}), the cut comes " + f"then, not 15 s after the guard first wanted power (wants from {s5}, " + f"cut at {c5}), and the period lands under the aim like the car-Off " + f"twin ({round(q5.get(900, -1))} W vs {round(q5o.get(900, -1))} W)", + s5 is not None and c5 is not None and nohead5 is not None + and c5 <= nohead5 + 1 and c5 - s5 < 15 and sat5 == (False, False) + and q5.get(900, 1e9) <= 3400.0 and q5o.get(900, 1e9) <= 3400.0) + + +# R5b: the wait itself. The batteries have headroom but answer slowly (20 s), +# so the guard wants power for more than 15 s: the cut comes at 15 s, not +# earlier and not never. +def _slow(p, c): + p.response_s = 20.0 + + +hr5b = {} +r5b = S.drive(house=steps((0, 500.0), (910, 6000.0)), sun=const(2900.0), + first="car", socs=ABLE, dur=960, events={900: _slow}, + on_cycle=headroom_at(hr5b)) +s5b, c5b = since_cut(r5b, 905) +check(f"R5b: the batteries keep their headroom while the guard wants power " + f"(wants from {s5b}, headroom {[hr5b[t][0] for t in (912, 920, 925) if t in hr5b]}" + f"): the cut comes 15 s later, not sooner and not never (cut at {c5b})", + s5b is not None and c5b is not None and 15 <= c5b - s5b <= 16 + and all(hr5b[t][0] for t in range(int(s5b), int(c5b)) if t in hr5b)) +# R5c: the peak reserve alone decides: both batteries at 19 % (above their +# 10 % cut-out, under the 20 % peak reserve), the car at 6 A, the house to +# 9 kW. The guard is floor-limited, not saturated, and the batteries' output +# is still under their rating: the cut comes at once. +hr5c = {} +r5c = S.drive(house=steps((0, 500.0), (300, 9000.0)), + sun=steps((0, 2300.0), (180, 2000.0)), first="car", + socs={"a": 19.0, "b": 19.0}, dur=420, + on_cycle=headroom_at(hr5c)) +s5c, c5c = since_cut(r5c, 290) +st5c = hr5c.get(c5c) if c5c is not None else None +check(f"R5c: floor-limited alone decides (guard (saturated, floor-limited) " + f"{None if st5c is None else st5c[2]}, batteries giving " + f"{None if st5c is None or st5c[1] is None else round(st5c[1])} W): the " + f"cut comes at once (wants from {s5c}, cut at {c5c})", + s5c is not None and c5c is not None and c5c - s5c <= 1 + and st5c is not None and st5c[2] == (False, True) + and st5c[1] is not None and st5c[1] < 9600.0 - 100.0) print("\n(T39, T44, T45, T50 and T51, the long runs, are in test_ev05_long.py)") print() diff --git a/goodwe_controller/tools/ev05_sun.py b/goodwe_controller/tools/ev05_sun.py index 1a013c1..10b26d3 100644 --- a/goodwe_controller/tools/ev05_sun.py +++ b/goodwe_controller/tools/ev05_sun.py @@ -60,6 +60,7 @@ class SunPlant: self.batt_nan = set() # units whose battery figure reads None self.utc_off = 0.0 # the wall clock jumps, the model clock not self.ha_down = False # Home Assistant answers nothing (SAFETY-112) + self.response_s = RESPONSE_S # a slow battery takes longer def batt_of(self, u, at): w = 0.0 @@ -131,7 +132,7 @@ class SunPlant: if e == SET: return self._logged("current", float(v), self.ch.request_current) if e.startswith("number.sp_"): - self.hists[e[-1]].append((self.t + RESPONSE_S, float(v))) + self.hists[e[-1]].append((self.t + self.response_s, float(v))) return True