Track radiator lag across relay changes
The lag proxy was reset to the new relay command at every relay change and after any skipped interval, so it read 1.0 straight after ON and 0.0 straight after OFF. This disabled the lag model around every boiler cycle: on a synthetic lagged plant cycled by a hysteresis thermostat the learner's heat gain came out 34% low (0.079 vs 0.12) and the loss term 43% low, and the optimizer started post-transition plans from the wrong lag. Track the lag at every poll with a valid relay state, independently of room sensors. Integrate a single change between polls at its last_changed time. For ambiguous cases (round trip between polls, relay outage, restart, clock jump) restart from time-in-state with an explicit error half-width, and learn only once it is at most 0.1 (about 32 min after an HA restart). The same synthetic test now learns 0.1195/0.00389 (true 0.12/0.004). Expose heat_lag, heat_lag_uncertainty and sample_result on Status so users can see why a poll was or was not learned. The HA test fixture relay now has an hour-old timestamp; a new test covers a freshly written relay state. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
387929b01b
commit
c3646ba876
6 files changed
+220
-28
No files matched your search
@@ -135,5 +135,19 @@ class ModelTests(unittest.TestCase):
|
||||
self.assertEqual(restored.p[0][0], m.PRIOR_P[0][0])
|
||||
self.assertEqual(restored.p[0][1], restored.p[1][0])
|
||||
|
||||
def test_lag_helpers(self):
|
||||
self.assertAlmostEqual(m.advance_lag(0, True, 300), m.HEAT_LAG)
|
||||
self.assertAlmostEqual(m.advance_lag(1, False, 1e9), 0)
|
||||
self.assertEqual(m.lag_from_time_in_state(True, 0), (0.5, 0.5))
|
||||
lag, half_width = m.lag_from_time_in_state(False, 3600)
|
||||
self.assertAlmostEqual(lag, half_width)
|
||||
self.assertLess(half_width, 0.03)
|
||||
# Any starting lag in [0, 1] ends inside the stated interval.
|
||||
for start in (0.0, 0.3, 1.0):
|
||||
for command in (False, True):
|
||||
lag, half_width = m.lag_from_time_in_state(command, 900)
|
||||
self.assertLessEqual(abs(m.advance_lag(start, command, 900) - lag), half_width + 1e-12)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user