cantabile_edge, cantabile_modbus, cantabile_eebus 0.1.1289: full source

This commit is contained in:
Setti Solutions S.R.L committed 2026-11-08 16:35:35 +02:00
1 parent 165dda4479
commit 05802d6f87
57 files changed
+5928 -232

No files matched your search

+35 -6
View File
@@ -49,12 +49,21 @@ log = logging.getLogger(__name__)
SLOT_S = 900
KEEP_DAYS = 400 # slots: this month and last, and a year back for a later view
KEEP_PRICE_DAYS = 3
CARRIERS = ("electricity", "gas", "heat")
CARRIERS = ("electricity", "gas", "heat", "oil")
# the meters the costs are read from: carrier, kind (a part, grid.1.import_energy, counts as its quantity)
ASSETS = {"grid.import_energy": ("electricity", "counter"), "grid.import_power": ("electricity", "gauge"),
"grid.power": ("electricity", "gauge"), # signed (import +): its positive part
"gas.energy": ("gas", "counter"), "heat.energy": ("heat", "counter")}
"gas.energy": ("gas", "counter"), "heat.energy": ("heat", "counter"),
# a boiler's own gas counter (simulated boilers map it; before 2026-11-08 not booked)
"boiler.gas_energy": ("gas", "counter"),
# heating oil (methods.md E15): kWh (Hi) where the device reports energy, else its oil meter's
# litres × the Parameters row de.heizoel_kwh_per_l (the tariff bundle's params)
"oil.energy": ("oil", "counter"), "oil.volume": ("oil", "litres"),
"boiler.oil_energy": ("oil", "counter"), "boiler.oil_volume": ("oil", "litres")}
OIL_KWH_PER_L = "de.heizoel_kwh_per_l"
PREFER = ("grid.import_energy", "grid.import_power", "grid.power") # electricity: the counter first
# gas and oil: the house's meter before the boiler's own (one of them, never both: the same fuel)
PREFER_FUEL = ("gas.energy", "boiler.gas_energy", "oil.energy", "oil.volume", "boiler.oil_energy", "boiler.oil_volume")
PARAM_FORECAST = "costs.forecast_days" # the Parameters page (fleet), in the tariff bundle's params
FORECAST_DAYS = (1, 62) # the parameter's valid range
REPORT_EVERY_S = 60 # HA's figures made again at most this often (announce.py)
@@ -86,7 +95,7 @@ def _finite(v) -> bool:
return isinstance(v, (int, float)) and not isinstance(v, bool) and math.isfinite(v)
def amounts(records: list[dict], span_s: float) -> dict[str, float | None]:
def amounts(records: list[dict], span_s: float, oil_kwh_per_l: float | None = None) -> dict[str, float | None]:
"""A closed window's kWh per carrier from the cost aggregator's records
(telemetry.Aggregator): the counters' deltas (Wh) where the carrier has
a counter that has a reading, else its power's mean (W) over the
@@ -101,13 +110,18 @@ def amounts(records: list[dict], span_s: float) -> dict[str, float | None]:
if kind == "counter":
v = r.get("delta") if r.get("last") is not None else None
v = v / 1000.0 if _finite(v) else None
elif kind == "litres":
# litres of oil: kWh only with the conversion row (none: no value, never litres as kWh)
v = r.get("delta") if r.get("last") is not None else None
v = v * oil_kwh_per_l if _finite(v) and _finite(oil_kwh_per_l) and oil_kwh_per_l > 0 else None
else:
m = r.get("mean")
v = (max(m, 0.0) if q == "grid.power" else m) * span_s / 3600.0 / 1000.0 if _finite(m) else None
by.setdefault(carrier, {}).setdefault(q, []).append(v)
out = {}
for carrier, qs in by.items():
order = [q for q in PREFER if q in qs] if carrier == "electricity" else list(qs)
order = [q for q in PREFER if q in qs] if carrier == "electricity" else \
[q for q in PREFER_FUEL if q in qs] + [q for q in qs if q not in PREFER_FUEL]
got = None
for q in order:
vals = qs[q]
@@ -140,6 +154,7 @@ class Ledger:
"offer INTEGER NOT NULL DEFAULT 0) WITHOUT ROWID")
self.pending: dict[int, dict] = {} # slot -> the latest plan's estimate for it (step 0)
self._booked = 0 # the last slot whose plan estimate is booked
self.outlook: dict | None = None # the last plan's heat outlook: carrier, eur, input_kwh, hours
def close(self):
with self.lock:
@@ -191,7 +206,11 @@ class Ledger:
estimate of slots that ended. Returns the kWh booked per carrier."""
span = max(0.0, end - start)
slot = int(start // SLOT_S * SLOT_S)
got = amounts(records, span)
try:
kwh_l = float(self.bundles.box_params(self.clock()).get(OIL_KWH_PER_L))
except (TypeError, ValueError, AttributeError):
kwh_l = None
got = amounts(records, span, kwh_l)
sp = self.bundles.slot_prices(start, self.clock())
fixed = self.bundles.fixed_charges(start)
with self.lock:
@@ -234,6 +253,10 @@ class Ledger:
optimizer.step_costs), booked when the quarter-hour ends; a later
plan in the same quarter-hour replaces it."""
se, start, step = plan.get("steps_eur"), plan.get("start"), plan.get("step_s") or SLOT_S
# the heat no room model plans: its expected bill over the plan's horizon (methods A2.16, A12)
ho = plan.get("heat_outlook")
self.outlook = {k: ho.get(k) for k in ("carrier", "eur", "input_kwh")} | {
"hours": round((plan.get("steps") or 0) * step / 3600.0)} if isinstance(ho, dict) else None
if not isinstance(se, dict) or not _finite(start):
return
now = self.clock()
@@ -301,7 +324,7 @@ class Ledger:
with self.lock:
kept = {c for (c,) in self.db.execute("SELECT DISTINCT carrier FROM months")}
sp = self.bundles.slot_prices(now, now) or {}
for c in ("gas", "heat"):
for c in ("gas", "heat", "oil"):
if c in kept or (sp.get(c) or {}).get("price") is not None:
out.append(c)
return out
@@ -504,6 +527,12 @@ def costs_section(ledger: Ledger, now: float, lang: str, meters: dict[str, bool]
notes += [r["note"] for r in out["carriers"].values() if r.get("note")]
if days is not None:
notes.append(names.name("cs_forecast_how", lang, int(days)))
ho = getattr(ledger, "outlook", None)
if ho and _finite(ho.get("eur")):
# the heat no room model plans: what the last plan expects it to cost (methods A2.16)
out["heat_outlook"] = ho
notes.append(names.name("cs_heat_outlook", lang, names.name("carrier_" + str(ho.get("carrier")), lang),
ho.get("hours"), _eur(ho["eur"], lang), names.number(ho.get("input_kwh") or 0.0, lang, 1)))
out["md"] = hdr + "\n".join(rows) + "\n\n<small>" + " ".join(notes) + "</small>"
return out