cantabile_edge, cantabile_modbus, cantabile_eebus 0.1.1919: full source

This commit is contained in:
Setti Solutions S.R.L committed 2026-11-12 13:44:54 +00:00
1 parent b24675fe9c
commit c0a6f8a579
21 files changed
+1055 -47

No files matched your search

+56 -15
View File
@@ -151,6 +151,34 @@ def _hourly(forecast: list[dict], key: str, start: int, steps: int) -> list[floa
return out
def outdoor_temperature(start: int, col: Collector, inputs, ha_forecast: list[dict] | None,
fleet_forecast: list[dict] | None) -> tuple[list[float] | None, str]:
"""The outdoor temperature per step and where it is from (the plan's
sources.outdoor, methods.md A1.4): "ha_entity" (the mapping's weather
entity's forecast), else "fleet_forecast" (fleet's DWD forecast for the
site's cell, bundles.weather_forecast), either "+sensor_bias" where an
outdoor sensor reads now (the forecast moved by the difference, fading
over 6 hours); else "sensor_now" or "ha_entity_now" (that value held);
else (None, "none"): no stand-in."""
measured = col.value(inputs.outside_temp) if inputs.outside_temp else None
t_out, src = _hourly(ha_forecast or [], "temperature", start, STEPS), "ha_entity"
if t_out is None:
t_out, src = _hourly(fleet_forecast or [], "temperature", start, STEPS), "fleet_forecast"
if t_out is not None:
if measured is not None:
# a thermometer outside says better than the forecast what it is now
bias = measured - t_out[0]
t_out = [v + bias * max(0.0, 1 - k / 24) for k, v in enumerate(t_out)]
src += "+sensor_bias"
return t_out, src
if measured is not None:
return [measured] * STEPS, "sensor_now"
now_out = number(col.attr(inputs.weather, "temperature")) if inputs.weather else None
if now_out is not None:
return [now_out] * STEPS, "ha_entity_now"
return None, "none"
def watts(col: Collector, eid: str) -> float | None:
"""A power sensor's value in W (HA's kW converted)."""
v = col.value(eid)
@@ -935,20 +963,15 @@ def model_state(now: float, controls, inputs, col: Collector, bundles, history,
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
if t_out is not None and measured is not None:
# a thermometer outside says better than the forecast what it is
# now: the forecast moved by the difference, fading over 6 hours
bias = measured - t_out[0]
t_out = [v + bias * max(0.0, 1 - k / 24) for k, v in enumerate(t_out)]
t_src = "weather forecast, from the outdoor sensor now"
# the outdoor temperature (methods.md A1.4): the mapping's weather entity's forecast, else
# fleet's forecast for the site's own cell (the tariff bundle's "weather", DWD MOSMIX), each
# moved by an outdoor sensor where there is one; else the sensor's (or the entity's) value
# now, held; else none: no number stands in (rooms are not planned, a quiet note)
fwx = bundles.weather_forecast(now) if hasattr(bundles, "weather_forecast") else []
t_out, t_src = outdoor_temperature(start, col, inputs, weather_forecast, fwx)
if t_out is None:
now_out = col.value(inputs.outside_temp)
if now_out is None and inputs.weather:
now_out = number(col.attr(inputs.weather, "temperature"))
t_out, t_src = [now_out if now_out is not None else 10.0] * STEPS, ("now" if now_out is not None else "default")
notes.append("no outdoor temperature (no weather entity, no forecast from the platform for the site's "
"location, no outdoor sensor): rooms are not planned, they follow their schedule")
peak = max(pv) if pv else 0.0
if peak > 100:
sun = [max(0.0, p / peak) for p in pv]
@@ -1018,6 +1041,12 @@ def model_state(now: float, controls, inputs, col: Collector, bundles, history,
fallback.append({"id": c.id, "kind": c.kind, "params": params, "state": st, "bounds": bounds,
"channels": sorted(c.writable())})
continue
if t_out is None:
# no outdoor temperature: the room model has no outside to lose heat to. Not a
# refusal (nothing to enter in the mapping): its fallback schedule, the plan's note
fallback.append({"id": c.id, "kind": c.kind, "params": params, "state": st, "bounds": bounds,
"channels": sorted(c.writable())})
continue
if c.kind == "heating" and hasattr(c, "heat_facts"):
facts, _, fsrc = c.heat_facts()
params.update(facts)
@@ -1054,6 +1083,15 @@ def model_state(now: float, controls, inputs, col: Collector, bundles, history,
if "note" in got:
notes.append(got["note"])
continue
if t_out is None and params.get("cop_curve") and str(params.get("heater")) == "heat_pump":
# its COP by the outside temperature: none known. The device's one COP where it
# has one, else not planned (its own thermostat)
if params.get("cop") is None:
notes.append(f"{c.id}: hot-water tank not planned: its heat pump's COP follows the outdoor "
"temperature and there is none; its own thermostat meanwhile")
continue
params["cop_curve"] = []
got["src"] = dict(got["src"], cop="its one COP (no outdoor temperature for the curve)")
tank_src[c.id] = got["src"]
extra = {"draw_w": got["draw_w"]}
b = c.bounds("temp_c", (extra_limits or {}).get(c.id)) # with a Großanlage's floor (tank_facts)
@@ -1062,6 +1100,9 @@ def model_state(now: float, controls, inputs, col: Collector, bundles, history,
"state": st, "bounds": bounds,
"channels": sorted(c.writable()), **extra})
clouds = _hourly(weather_forecast or [], "cloud_coverage", start, STEPS)
sky_src = "weather forecast"
if clouds is None and fwx:
clouds, sky_src = _hourly(fwx, "cloud_coverage", start, STEPS), "fleet forecast"
sky = [max(0.0, min(1.0, 1 - c / 100.0)) for c in clouds] if clouds else [1.0] * STEPS
ghi, ghi_src = irradiance(start, location, sky, sun if peak > 100 else None)
# the heat outlook (space heat no room plans, hot water), CO2 (methods A2): the forecasts and factors
@@ -1077,8 +1118,8 @@ def model_state(now: float, controls, inputs, col: Collector, bundles, history,
"priced_steps": getattr(prices, "priced_steps", STEPS)},
"load_w": load, "pv_w": pv, "t_out": t_out, "sun": sun, "occupied": occupied, "sky": sky,
"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,
"sources": {"load": load_src, "pv": pv_src, "outdoor": t_src, "model": bundles.model_id(),
"sky": sky_src if clouds else "clear", "rooms": rooms_src, "ghi": ghi_src,
"rooms_cop": cop_src, "rooms_band": band_src,
"ev_phases": phases_src,
# occupied steps per source (presence now, learnt, fixed hours) and why (B16)