cantabile_edge, cantabile_modbus, cantabile_eebus 0.1.650: full source

This commit is contained in:
Setti Solutions S.R.L committed 2026-11-04 03:22:07 +02:00
1 parent 85edd56ec8
commit 3edd015039
20 files changed
+2021 -35

No files matched your search

+62 -12
View File
@@ -25,7 +25,7 @@ log = logging.getLogger(__name__)
STEP_S = 900
STEPS = 192 # 48 h
HISTORY_S = 3 * 86400
HISTORY_S = 8 * 86400 # a day back (the fallback) and a week back (the box's models' lag)
def number(v) -> float | None:
@@ -83,8 +83,9 @@ class Collector:
class History:
"""Quarter-hour means of load and PV (W) for the last 3 days, kept on
disk, for "the same quarter hour yesterday"."""
"""Quarter-hour means of load and PV (W) for the last 8 days, kept on
disk, for "the same quarter hour yesterday" and the box's models' "the
same slot a week before" (localml.py)."""
def __init__(self, path: str):
self.path = path
@@ -162,14 +163,56 @@ def base_load(col: Collector, inputs) -> float | None:
return max(0.0, sum(vals) - ctl)
def _series(name, start, bundles, history, tz, now_value, default, model_names=None):
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;
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."""
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
vals = [v for v, _ in parts]
info = {"series": m, "cloud": sum(src == "series" for _, src in parts), "box": 0, "hour_of_week": 0}
own = None
if box is not None and info["cloud"] < STEPS:
own = bundles.box_forecast(m, start, STEPS, history, box[0], box[1])
for k, (v, src) in enumerate(parts):
if src == "series":
continue
b = own[0][k] if own is not None else None
if b is not None:
vals[k] = b
info["box"] += 1
else:
info["hour_of_week"] += 1
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"]
return vals, info
def _series(name, start, bundles, history, tz, now_value, default, model_names=None, box=None, info=None):
"""A series for the horizon: the model bundle's (the first of
model_names it has, default name), else the same quarter hour 1 or 2
days back, else the value now, else the default."""
model_names it has, default name: _model_series), else the same quarter
hour 1 or 2 days back, else the value now, else the default. info (a
dict): filled with the model's steps by source."""
for m in (model_names if model_names is not None else (name,)):
s = bundles.forecast(m, start, STEP_S, STEPS, tz) if bundles else None
if s is not None:
return s, "model" if m == name else f"model ({m})"
got = _model_series(m, start, bundles, history, tz, box) if bundles else None
if got is not None:
s, n = got
src = "model" if m == name else f"model ({m})"
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'})")
if info is not None:
info.update(n)
return s, src
out, have = [], 0
for k in range(STEPS):
t = start + k * STEP_S
@@ -273,12 +316,16 @@ def model_state(now: float, controls, inputs, col: Collector, bundles, history,
# (load.power as metered) is too only where no planned device has a meter
# inside it, else the history of the base load stands in.
ctl = getattr(inputs, "controlled_power", None) or []
# the box's own models where the cloud's forecast no longer covers a step
# (localml.py), on HA's weather forecast where the mapping has one
box = (weather_forecast or [], getattr(inputs, "weather", "") or "")
fc_src: dict = {"load": {}, "pv": {}}
load, load_src = _series("load_w", start, bundles, history, tz, base_load(col, inputs), 500.0,
("base_load_w",) if ctl else ("base_load_w", "load_w"))
("base_load_w",) if ctl else ("base_load_w", "load_w"), box, fc_src["load"])
if ctl and not load_src.startswith("model") and bundles and \
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)
pv, pv_src = _series("pv_w", start, bundles, history, tz, None, 0.0, None, box, fc_src["pv"])
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
@@ -342,7 +389,10 @@ def model_state(now: float, controls, inputs, col: Collector, bundles, history,
"ghi_w_m2": ghi, "location": location or {},
"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},
"ev_phases": phases_src,
# 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}},
"devices": devices, "building": building or {}, "grid_order": grid_order or {},
# the market's contracts in force as sources and sinks (shadow, the market's step 2)
"market": bundles.market_for_model(start, STEP_S, STEPS, now) if hasattr(bundles, "market_for_model") else []}