Score forecasts against reality; expose forecast trajectories

There was no evidence of how accurate the thermal model is: the only
number was an in-sample 5-minute error.

At every poll, record each room's reading, the outdoor temperature, the
radiator-lag estimate and the model parameters. 15, 30, 60 and 120
minutes later, replay the room from that poll with the parameters it
had then (out of sample), the outdoor temperature held constant and the
radiator lag that actually followed from the relay's real history, and
compare with the reading now. A no-change forecast is scored alongside.
Only spans with regular polls and a well-known lag are scored. 7 days
of per-room statistics are saved with the model (optional "evaluation"
payload key; about 18 KB at most).

Four new sensors, Heating MPC Forecast Error 15/30/60/120 min, show the
mean absolute error with bias, persistence error, skill and per-room
detail. On the synthetic lagged plant the 60-minute error is 0.03 °C
against 0.39 °C for persistence.

Room forecast entities gain forecast_15min and plan_15min attributes
and are labelled counterfactual. Volatile attributes are unrecorded.
The model is now saved after scoring, so saved statistics are current.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
buzeraandClaude Opus 5.5 committed 2026-10-26 22:35:13 +02:00
1 parent 43915ba810
commit 3afb6688a5
9 files changed
+454 -16

No files matched your search

+33 -1
View File
@@ -423,7 +423,7 @@ class BoundaryTests(unittest.IsolatedAsyncioTestCase):
for entity, value in (('sensor.room', temp), ('sensor.outside', outside)):
self.states[entity].state = f'{value:.5f}'
self.states[entity].last_reported = self.now
await self.sample()
data = await self.sample()
command = temp < 20.7 or (on and temp < 21.3)
if command != on: # relay switches 120 s after this poll
self.switch('on' if command else 'off', 180)
@@ -436,6 +436,11 @@ class BoundaryTests(unittest.IsolatedAsyncioTestCase):
self.assertGreater(model.on_count, 100)
self.assertAlmostEqual(model.theta[1], b, delta=0.1 * b)
self.assertAlmostEqual(model.theta[0], a, delta=0.25 * a)
# Walk-forward replay with the real relay history beats persistence clearly.
accuracy = data['forecast_accuracy'][60]
self.assertGreater(accuracy['samples'], 1400)
self.assertLess(accuracy['mae'], 0.06)
self.assertGreater(accuracy['skill'], 0.85)
async def test_several_relay_changes_between_polls_are_integrated(self):
advance = coordinator.advance_lag
@@ -654,3 +659,30 @@ class BoundaryTests(unittest.IsolatedAsyncioTestCase):
self.assertIs(type(data['count']), int)
float(data['error_sum'])
[float(v) for v in data['theta'] + [v for row in data['p'] for v in row]]
async def test_forecasts_are_scored_saved_and_restored(self):
for _ in range(5):
self.fresh('sensor.room', 'sensor.outside')
data = await self.sample()
accuracy = data['forecast_accuracy']
self.assertEqual((accuracy[15]['samples'], accuracy[30]['samples'], accuracy[120]['samples']),
(2, 0, 0))
# The default parameters predict cooling while the room held 20 °C.
self.assertLess(accuracy[15]['rooms']['sensor.room']['bias'], 0)
self.assertEqual(accuracy[15]['persistence_mae'], 0)
payload = self.c._store.async_save.call_args.args[0]
restored = coordinator.HeatingMPCCoordinator(self.hass, self.entry)
restored._store.async_load.return_value = payload
await restored.async_restore_model()
self.assertEqual(restored._evaluator.summary(['sensor.room']), self.c._evaluator.summary(['sensor.room']))
# A poll without a usable relay state breaks the replay window.
self.states['switch.boiler'].state = 'unavailable'
self.fresh('sensor.room', 'sensor.outside')
await self.sample()
self.states['switch.boiler'].state = 'off'
for _ in range(3):
self.fresh('sensor.room', 'sensor.outside')
data = await self.sample()
self.assertEqual(data['forecast_accuracy'][15]['samples'], 2)
self.fresh('sensor.room', 'sensor.outside')
self.assertEqual((await self.sample())['forecast_accuracy'][15]['samples'], 3)