Introduce thermal zones as config subentries

Each temperature sensor used to be its own "room", so a room with two
sensors counted twice and comfort importance was tied to sensors.

zones.py adds Zone: a name, one or more sensors with sensor weights, a
comfort weight, an optional target offset and adjacent zones (made
symmetric). Sensor weights only combine readings; comfort weights only
steer the MPC; neither touches the learned thermal coefficients.

Zones are config subentries (HA's native "Add zone" / edit / delete on
the integration page), with validation that a sensor belongs to one
zone and is not an outdoor source, and a sensor-weight step for
multi-sensor zones. Initial setup still asks for room sensors and
weights and creates one zone per sensor. Configure no longer asks for
rooms or weights.

Config entry version 3. Migrating from 2 turns each room sensor into a
one-sensor zone with its comfort weight and the sensor's entity ID as
the zone key, so learned models, forecast entities (now linked to their
zone, so deleting a zone removes them) and accuracy statistics carry
over. Older versions cannot load a version 3 entry; rolling back needs
the HA backup.

A zone learns only across polls that used the same sensors (a sensor
dropping out shifts the mean by its offset); the new poll outcome
"sensors_changed" reports this. Status renames: input_quality.zones
(per-sensor detail nested), excluded_zones, retired_zone_models; adds
zone_names. Invalid stored comfort weights now fall back to 1.0, so the
invalid_weights status is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
buzeraandClaude Opus 5.5 committed 2026-10-27 02:32:11 +02:00
1 parent 220ac570d2
commit 46587af8a3
15 files changed
+867 -155

No files matched your search

+9 -3
View File
@@ -179,11 +179,12 @@ def optimize(
is_on: bool, seconds_since_change: float, horizon_minutes: int = 120,
minimum_on_minutes: int = 15, minimum_off_minutes: int = 10,
switching_cost: float = .15, heating_cost: float = .025,
comfort_band: float = .15,
comfort_band: float = .15, target_offsets: dict[str, float] | None = None,
) -> dict[str, Any]:
"""Enumerate 15min ON/OFF plans; honor minimum ON/OFF durations.
120min = 256 plans (fast on Raspberry Pi 5). All rooms share one burner demand.
`target_offsets` shifts individual zones' targets (°C) relative to `target`.
"""
if not temps or set(temps) != set(learners):
raise ValueError("Missing room temperatures or learner")
@@ -206,6 +207,11 @@ def optimize(
if not isfinite(total_weight):
raise ValueError("Invalid total weight")
norm = {room: float(weights[room]) / total_weight for room in temps}
offsets = target_offsets or {}
if any(not isfinite(float(offsets.get(room, 0.0))) for room in temps):
raise ValueError("Invalid target offset")
targets = {room: target + float(offsets.get(room, 0.0)) for room in temps}
mean_target = sum(norm[room] * targets[room] for room in temps)
blocks = horizon_minutes // 15
nsteps_per_block = 3
best = float("inf")
@@ -234,12 +240,12 @@ def optimize(
for tick in range(nsteps_per_block):
state, lag = simulate_step(state, outside, lag, bit, learners)
mismatch = sum(
norm[room] * max(0.0, abs(value - target) - comfort_band) ** 2
norm[room] * max(0.0, abs(value - targets[room]) - comfort_band) ** 2
for room, value in state.items()
)
# A lower bound on weighted average comfort also matters.
avg = sum(norm[r] * value for r, value in state.items())
mismatch += max(0.0, abs(avg - target) - comfort_band) ** 2
mismatch += max(0.0, abs(avg - mean_target) - comfort_band) ** 2
cost += 8.0 * mismatch + (heating_cost if bit else 0.0)
if idx * 15 + (tick + 1) * STEP_MINUTES == 60:
at_60 = dict(state)