cantabile_edge, cantabile_modbus, cantabile_eebus 0.1.676: full source

This commit is contained in:
Setti Solutions S.R.L committed 2026-11-04 10:33:14 +02:00
1 parent 3edd015039
commit d48b9aff87
32 files changed
+4633 -133

No files matched your search

+94 -12
View File
@@ -163,37 +163,74 @@ def base_load(col: Collector, inputs) -> float | None:
return max(0.0, sum(vals) - ctl)
FC_SOURCES = {"own": "the box's own model", "cloud": "the cloud's series", "box": "the cloud's model on the box",
"hour_of_week": "the hour-of-week profile"}
def _model_series(m, start, bundles, history, tz, box) -> tuple[list[float], dict] | None:
"""The model bundle's series m for the horizon: the cloud's forecast
where its series covers a step, else the box's own model (localml.py;
"""A model series m for the horizon, per step the best fresh source
(methods.md B10): the box's own model where it is in force and has a
value (boxtrain.py: bundles.own), else the cloud's forecast where its
series covers the step, else the cloud's model on the box (localml.py;
box: HA's weather forecast and its entity) where it has a value, else
the hour-of-week profile; and how many steps each gave."""
the hour-of-week profile; how many steps each gave, the source that
gave most ("source"), and every source's values ("_cands": the live
error per source, taken out before the plan)."""
trainer = getattr(bundles, "own", None) if bundles is not None else None
mine = None
if trainer is not None and box is not None:
try:
mine = trainer.forecast(m, start, STEPS, box[0], box[1])
except Exception as e: # never stops a plan
log.warning("the box's own %s model: %s", m, e)
if not hasattr(bundles, "forecast_parts"):
s = bundles.forecast(m, start, STEP_S, STEPS, tz)
return None if s is None else (s, {})
parts = bundles.forecast_parts(m, start, STEP_S, STEPS, tz)
if parts is None:
return None
# no cloud model for it: the box's own alone, where it covers the horizon
if mine is None or any(v is None for v in mine[0]):
return None
vals = [float(v) for v in mine[0]]
return vals, {"series": m, "own": STEPS, "cloud": 0, "box": 0, "hour_of_week": 0, "own_model": mine[1].get("kind"),
"source": "own", "_cands": {"own": vals}}
vals = [v for v, _ in parts]
info = {"series": m, "cloud": sum(src == "series" for _, src in parts), "box": 0, "hour_of_week": 0}
info = {"series": m, "own": 0, "cloud": 0, "box": 0, "hour_of_week": 0}
cloud_series = [v if src == "series" else None for v, src in parts]
own = None
if box is not None and info["cloud"] < STEPS:
if box is not None and (trainer is not None or not all(v is not None for v in cloud_series)):
own = bundles.box_forecast(m, start, STEPS, history, box[0], box[1])
for k, (v, src) in enumerate(parts):
if src == "series":
continue
o = mine[0][k] if mine is not None else None
b = own[0][k] if own is not None else None
if b is not None:
if o is not None:
vals[k] = o
info["own"] += 1
elif src == "series":
info["cloud"] += 1
elif b is not None:
vals[k] = b
info["box"] += 1
else:
info["hour_of_week"] += 1
if mine is not None:
info["own_model"] = mine[1].get("kind")
if mine[1].get("weather"):
info["own_weather"] = mine[1]["weather"]
if own is not None:
info["box_model"] = own[1].get("kind")
if own[1].get("weather"):
info["box_weather"] = own[1]["weather"]
if own[1].get("error"):
info["box_error"] = own[1]["error"]
info["source"] = max(("own", "cloud", "box", "hour_of_week"), key=lambda k: info[k])
if trainer is not None:
info["_cands"] = {"own": list(mine[0]) if mine is not None else None, "cloud": cloud_series,
"cloud_model": list(own[0]) if own is not None else None,
"hour_of_week": (bundles.profile(m, start, STEP_S, STEPS, tz) if hasattr(bundles, "profile")
else [v if src == "profile" else None for v, src in parts])}
if not info["own"]:
info.pop("own")
return vals, info
@@ -207,6 +244,9 @@ def _series(name, start, bundles, history, tz, now_value, default, model_names=N
if got is not None:
s, n = got
src = "model" if m == name else f"model ({m})"
if n.get("own"):
src += (f"; the box's own {n.get('own_model')} model (trained on the box) for {n['own']} of {STEPS} steps"
f" (weather: {n.get('own_weather') or 'none'})")
if n.get("box"):
src += (f"; the box's own {n.get('box_model')} model for {n['box']} of {STEPS} steps"
f" (weather: {n.get('box_weather') or 'none'})")
@@ -326,6 +366,39 @@ def model_state(now: float, controls, inputs, col: Collector, bundles, history,
bundles.forecast("load_w", start, STEP_S, 1, tz) is not None:
load_src += " (the model's load_w includes the planned devices: not used)"
pv, pv_src = _series("pv_w", start, bundles, history, tz, None, 0.0, None, box, fc_src["pv"])
# the forecasts per source for the live error (boxtrain.py), not for the plan
cands = {k: v.pop("_cands") for k, v in fc_src.items() if "_cands" in v}
net_load = None
intraday = {}
trainer = getattr(bundles, "own", None)
if trainer is not None:
# the intraday correction (fcmodels.intraday, methods.md B10): the last
# 3 hours' error of what was planned on against the meter, carried into
# the next hours and fading (half-life 2 h); load in W, PV as a ratio
for key, series in (("load", "plan"), ("pv", "pv")):
base = load if key == "load" else pv
c = cands.setdefault(key, {})
c["used"] = list(base)
try:
new, e = trainer.correct(series, start, base)
except Exception as ex: # never stops a plan
log.warning("intraday correction %s: %s", key, ex)
continue
c["corrected"] = list(new)
intraday[key] = round(float(e), 3)
if key == "load":
load = new
else:
pv = new
if trainer is not None:
# the house as metered (devices in), for the member's net draw the box
# publishes upward where it does not act itself (controller.forecasts_up)
if ctl:
nl: dict = {}
got = _series("load_w", start, bundles, history, tz, None, 500.0, ("load_w",), box, nl)
net_load = got[0] if got[1].startswith("model") else None
else:
net_load = load
t_out = _hourly(weather_forecast or [], "temperature", start, STEPS)
t_src = "weather forecast"
measured = col.value(inputs.outside_temp) if inputs.outside_temp else None
@@ -390,6 +463,7 @@ def model_state(now: float, controls, inputs, col: Collector, bundles, history,
"sources": {"load": load_src, "pv": pv_src, "t_out": t_src, "model": bundles.model_id(),
"sky": "weather forecast" if clouds else "clear", "rooms": rooms_src, "ghi": ghi_src,
"ev_phases": phases_src,
**({"intraday": intraday} if intraday else {}),
# per series the steps from the cloud's forecast, the box's own model
# (its kind and weather) and the hour-of-week profile (methods.md B9)
"forecast": {k: v for k, v in fc_src.items() if v}},
@@ -398,6 +472,9 @@ def model_state(now: float, controls, inputs, col: Collector, bundles, history,
"market": bundles.market_for_model(start, STEP_S, STEPS, now) if hasattr(bundles, "market_for_model") else []}
if grid_max_w:
out["grid_max_w"] = float(grid_max_w)
if cands or net_load is not None:
out["_cands"] = cands # taken out by the controller before the plan
out["_net_load"] = net_load
# a shade's moves by rules on the sun and its room (shades.py); the
# optimiser takes them as they are
from . import shades
@@ -437,8 +514,13 @@ def _room_rc(c, params: dict, bundles, only_one: bool, now: float) -> str:
heat is cop * P). Into params; where they came from."""
if "r_k_per_kw" in c.params or "c_kwh_per_k" in c.params:
return "mapping"
get = getattr(bundles, "room_rc", None)
rc = get(c.id, str(c.params.get("area") or ""), only_one, now) if get else None
# the box's own fit where it is in force (boxtrain.py), else the cloud's
own = getattr(bundles, "own", None)
rc = own.room_rc(c.id, str(c.params.get("area") or ""), only_one) if own is not None else None
src = "box model" if rc is not None else "model"
if rc is None:
get = getattr(bundles, "room_rc", None)
rc = get(c.id, str(c.params.get("area") or ""), only_one, now) if get else None
if rc is None:
return "defaults"
r, cap = rc["r_k_per_kw"], rc["c_kwh_per_k"]
@@ -456,7 +538,7 @@ def _room_rc(c, params: dict, bundles, only_one: bool, now: float) -> str:
params["internal_gain_w"] = gain
if "solar_m2" not in c.params:
params["solar_m2"] = solar
return "model"
return src
def _quantities(c) -> list[str]: