11 KiB
Water Ready False Positive Fix — Implementation Plan
Goal: Eliminate false "water ready" detections caused by stale/zero power readings when the boiler switch is on but hasn't started drawing power yet.
Tech Stack: C# / .NET, NetDaemon, xUnit + Moq + FluentAssertions
Problem Analysis
Observed Behavior (Apr 10, 08:32-08:58)
The sensor.boilermonitor_water_ready oscillated yes/no 10 times in 26 minutes. All entries show boiler_switch_state=on and boiler_power_watts=0W. The boiler was turned on during a cheap NordPool interval but the power sensor reported 0W, triggering false "water ready" detection.
Root Causes
-
No "was heating" prerequisite —
WaterReadyService.IsWaterReady()triggers on any low-power reading while the switch is on. It doesn't verify that the boiler was actually observed heating first. A legitimate "water ready" event requires the boiler to have been heating (high power) and then power dropped (heating finished). A boiler that was just turned on but hasn't ramped up yet should NOT trigger "water ready". -
Fixed warm-up grace period too short — The 2-minute
WarmUpGracePeriodassumes the boiler starts drawing power within 2 minutes. Some boilers take longer, or the power sensor can have reporting delays. Once the grace period expires, 0W readings immediately trigger false detections. -
No debouncing — A single poll reading below threshold triggers the detection. Transient 0W readings from sensor lag, network hiccups, or reporting gaps cause false positives.
-
In-memory cooldown state lost on restart —
_waterReadySinceis only in memory. When the daemon restarts, the cooldown is lost, allowing immediate re-detection. This causes oscillation when paired with the above issues.
Impact
- False "hot water is ready" push notifications (max 2 per hour due to notification cooldown, but still annoying)
- Rapid boiler on/off cycling: the control service turns the boiler off when water is "ready", then back on next cheap interval, repeating every few minutes
- Wasted energy and potential relay wear on the boiler switch
Solution Design
Fix 1: Require "was heating" before detecting water ready
The most impactful fix. Track whether the boiler has been observed actively heating (power >= threshold) at least once since the last turn-on. Only allow water ready detection after heating has been observed.
Why: A boiler that was just turned on and reads 0W hasn't finished heating — it hasn't started yet. The legitimate "water ready" pattern is: turn on → power rises → power drops → water ready.
Fix 2: Debounce water ready detection
Require N consecutive below-threshold readings before declaring water ready. This filters out transient sensor glitches.
Why: A single 0W reading could be a sensor reporting gap. Requiring consecutive readings confirms the power has genuinely dropped.
Fix 3: Replace fixed grace period with dynamic "await first heating" behavior
Instead of a fixed 2-minute warm-up grace period, keep suppressing water ready detection until the boiler has been observed drawing power at least once. The grace period effectively becomes unbounded until the first high-power reading.
Why: Fix 1 and Fix 3 are complementary. Fix 3 replaces the brittle time-based heuristic with a state-based one.
Fix 4: Persist cooldown timestamp
Store _waterReadySince in the HA entity attributes so the cooldown survives daemon restarts.
Why: Prevents re-triggering water ready immediately after a restart within the cooldown window.
File Structure
| File | Change |
|---|---|
BoilerMonitor.Manager/WaterReadyService.cs |
Modified — add _hasObservedHeating, consecutive reading counter, persist/restore cooldown |
BoilerMonitor.Manager/Interface/IWaterReadyService.cs |
Modified — add RecordHeatingObserved(), RestoreCooldownState() |
BoilerMonitor.Manager/BoilerMonitorInit.cs |
Modified — call RecordHeatingObserved() when isHeating=true, restore cooldown on startup |
BoilerMonitor.Manager/BoilerMonitorEntityService.cs |
Modified — persist/read cooldown timestamp in water ready entity attributes |
BoilerMonitor.Manager/Interface/IBoilerMonitorEntityService.cs |
Modified — add ReadPersistedCooldownTimestamp() |
BoilerMonitor.Models/BoilerMonitorConsts.cs |
Modified — add ConsecutiveReadingsRequired constant |
BoilerMonitor.Manager.Tests/WaterReadyServiceTests.cs |
Modified — add tests for all new behaviors |
Implementation Steps
Step 1: Add "was heating" tracking and debouncing to WaterReadyService
- Add
_hasObservedHeating(bool) field — resets tofalseonNotifyTurnedOn(), set totruevia newRecordHeatingObserved()method - Add
_consecutiveBelowThresholdCount(int) field — increments on each below-threshold reading, resets to 0 on above-threshold reading - Add
ConsecutiveReadingsRequiredconstant toBoilerMonitorConsts(value: 2 — at 30s poll interval, this means 60s of confirmed low power) - Modify
IsWaterReady()logic:- After cooldown check (unchanged)
- After null/switch-off checks (unchanged)
- Remove the warm-up grace period check entirely (replaced by
_hasObservedHeating) - New: if
!_hasObservedHeating, returnfalse(boiler hasn't been seen heating yet) - New: if
powerWatts < threshold, increment_consecutiveBelowThresholdCount; otherwise reset to 0 and returnfalse - New: only return
truewhen_consecutiveBelowThresholdCount >= ConsecutiveReadingsRequired
- Update
NotifyTurnedOn()to reset_hasObservedHeating = falseand_consecutiveBelowThresholdCount = 0 - Add
RecordHeatingObserved()method that sets_hasObservedHeating = true - Update
IWaterReadyServiceinterface withRecordHeatingObserved()
Step 2: Integrate heating observation into poll loop
- In
BoilerMonitorInit.PollAndUpdateAsync(), after computingisHeating:if (isHeating) { _waterReadyService.RecordHeatingObserved(); } - Remove the
IsWithinWarmUpGracePeriod()reference from any documentation/comments (the warm-up grace period is replaced)
Step 3: Persist and restore cooldown state
- In
BoilerMonitorEntityService.UpdateWaterReadyAsync(), addcooldown_sinceattribute (ISO 8601 string or null) - Add
ReadPersistedCooldownTimestamp()method toBoilerMonitorEntityService— readscooldown_sincefrom the water ready entity attributes - Add
RestoreCooldownState(DateTimeOffset? waterReadySince)method toWaterReadyService - In
BoilerMonitorInit.InitializeAsync(), after restoring persisted state, restore cooldown:var persistedCooldown = _entityService.ReadPersistedCooldownTimestamp(); if (persistedCooldown.HasValue) { _waterReadyService.RestoreCooldownState(persistedCooldown.Value); } - Update
IWaterReadyServiceinterface withRestoreCooldownState() - Update
IBoilerMonitorEntityServiceinterface withReadPersistedCooldownTimestamp()
Step 4: Update unit tests
- New test:
IsWaterReady_SwitchOnLowPowerButNeverObservedHeating_ShouldReturnFalse— switch=on, power=0, but noRecordHeatingObserved()call → false - New test:
IsWaterReady_AfterObservedHeatingAndPowerDrops_ShouldReturnTrue— switch=on, callRecordHeatingObserved(), then power=0 with consecutive readings → true - New test:
IsWaterReady_SingleBelowThresholdReading_ShouldReturnFalse— only 1 consecutive low reading, need 2 → false - New test:
IsWaterReady_ConsecutiveBelowThresholdReadings_ShouldReturnTrue— 2 consecutive → true - New test:
IsWaterReady_BelowThresholdResetByAboveThreshold_ShouldRequireConsecutiveAgain— counter resets on high power - New test:
NotifyTurnedOn_ShouldResetHasObservedHeating— verify thatNotifyTurnedOn()resets the flag - New test:
RecordHeatingObserved_ShouldEnableDetection— verify the flag is set - New test:
RestoreCooldownState_ShouldResumeActiveCooldown— verify restored cooldown works - New test:
RestoreCooldownState_WithExpiredTimestamp_ShouldNotActivateCooldown— expired cooldown doesn't restore - Update existing tests — remove warm-up grace period tests (replaced by
_hasObservedHeatingtests), update any test that expects immediate detection withoutRecordHeatingObserved()
Step 5: Remove deprecated warm-up grace period
- Remove
IsWithinWarmUpGracePeriod()private method fromWaterReadyService - Remove
_lastTurnedOnAtfield (no longer needed —NotifyTurnedOnonly resets_hasObservedHeatingand debounce counter) - Remove
WarmUpGracePeriodconstant fromBoilerMonitorConsts(or keep for backwards compatibility with a deprecation comment) - Update
NotifyTurnedOn()to not set_lastTurnedOnAt
Detection Logic Before vs After
Before (current — buggy)
IsWaterReady(switch, power, threshold):
if cooldown active → return true
if switch is null or power is null → return false
if switch != "on" → return false
if within 2-minute warm-up grace → return false
return power < threshold
Flaw: After grace period expires, ANY low-power reading triggers detection — even if the boiler was never observed heating.
After (fixed)
IsWaterReady(switch, power, threshold):
if cooldown active → return true
if switch is null or power is null → return false
if switch != "on" → return false
if NOT hasObservedHeating → return false ← NEW: must have seen heating first
if power >= threshold → reset consecutive count, return false
increment consecutive count
if consecutive count < required → return false ← NEW: debounce
start cooldown, return true
Result: Water ready only fires after the boiler was observed actively heating AND power has been consistently below threshold for 2+ consecutive polls (60+ seconds at 30s interval).
Risk Assessment
| Risk | Mitigation |
|---|---|
RecordHeatingObserved() never called if power sensor is broken |
If the sensor never reports high power, water ready never triggers — the boiler control service will still turn off via budget exhaustion or NordPool interval ending |
Daemon restarts during heating cycle lose _hasObservedHeating |
Acceptable — after restart, the first high-power poll restores it. At worst, one extra grace cycle before detection resumes |
ConsecutiveReadingsRequired = 2 delays legitimate detection by 30s |
Acceptable tradeoff — 30s extra delay vs false positive notifications |
| Removing warm-up grace period entirely | The _hasObservedHeating flag is strictly stronger — it never expires prematurely like the fixed 2-minute window |