# Boiler Anti-Cycling & Debug Diagnostic Entity **Date:** 2026-03-21 **Status:** Approved ## Problem The boiler rapidly toggles on/off every 2-3 minutes during cheap intervals. The boiler's internal thermostat naturally cycles power — the heating element cuts out briefly when it reaches its internal setpoint, then kicks back in. The current `WaterReadyService` interprets a single low-power reading as "water is ready" and triggers a full switch-off. Next poll, the boiler turns back on because the cheap interval is still active. Additionally, there is no single place to observe the full set of decision inputs in real time, making it difficult to debug threshold-related issues. ## Solution Overview Three coordinated changes: 1. **Sustained water-ready confirmation** — require consecutive low-power readings before declaring water ready 2. **Minimum hold time** — prevent rapid state transitions with configurable on/off hold durations 3. **Debug diagnostic entity** — expose all decision inputs as a single HA sensor with rich attributes ## Section 1: Sustained Water-Ready Confirmation Modify `WaterReadyService` to require N consecutive low-power readings before returning `true`. - Add `_consecutiveLowPowerCount` counter (thread-safe, using existing `_lock`) - Each `IsWaterReady` call: if power < threshold, increment counter; otherwise reset to 0 - Return `true` only when counter reaches `DefaultWaterReadyConsecutiveCount` - Warm-up grace period unchanged — still resets on `NotifyTurnedOn()` - `NotifyTurnedOn()` also resets the consecutive counter - Expose current count via a new property for the debug entity ### Constants - `DefaultWaterReadyConsecutiveCount = 3` in `BoilerMonitorConsts` (at 30s polling = 90 seconds sustained) ### Interface Change - Add `int ConsecutiveLowPowerCount { get; }` to `IWaterReadyService` ## Section 2: Minimum Hold Time (Anti-Cycling Guard) Add minimum hold durations to `BoilerControlService` to prevent rapid state transitions. - Add `TimeProvider` dependency (consistent with `WaterReadyService`) - Track `_lastTransitionAt` (DateTimeOffset) and `_lastTransitionAction` (BoilerAction) with a `Lock` - In `DetermineAction`: if state change requested but hold time not elapsed, return `NoChange` and log suppression - No hold enforced on the very first action after startup ### Constants - `DefaultMinOnHoldSeconds = 600` (10 minutes) — once turned ON, stays ON at least this long - `DefaultMinOffHoldSeconds = 300` (5 minutes) — once turned OFF, stays OFF at least this long ### Return Type Change `DetermineAction` returns a new `BoilerActionResult` record instead of bare `BoilerAction`: ```csharp public record BoilerActionResult { public required BoilerAction Action { get; init; } public required string Reason { get; init; } public required bool HoldSuppressed { get; init; } public required TimeSpan HoldTimeRemaining { get; init; } } ``` ## Section 3: Debug Diagnostic Entity New MQTT sensor: `sensor.boilermonitor_debug` **State:** Last action taken (`TurnOn`, `TurnOff`, `NoChange`, `HoldSuppressed`) **Attributes:** | Attribute | Type | Description | |---|---|---| | `last_action` | string | Action taken this poll | | `last_action_reason` | string | Human-readable reason | | `last_action_timestamp` | string | ISO 8601 | | `is_now_cheap` | bool | Effective cheap interval state | | `water_ready` | bool | Current water ready determination | | `water_ready_consecutive_count` | int | Consecutive low-power readings | | `water_ready_threshold` | int | Required consecutive count | | `budget_exhausted` | bool | Daily budget used up | | `budget_override` | bool | Budget override switch | | `boiler_power_watts` | double | Current power reading | | `heating_threshold_watts` | double | Configured threshold | | `boiler_switch_state` | string | Current HA switch state | | `hold_active` | bool | Hold preventing state change | | `hold_time_remaining_seconds` | int | Seconds left in hold | | `hold_type` | string | `on_hold` / `off_hold` / `none` | | `skip_price_cap` | bool | Raw skip price cap state | | `poll_timestamp` | string | When this poll ran | **Implementation:** - Entity ID added to `BoilerMonitorConsts` - Part of Boiler Monitor HA device group - Icon: `mdi:bug-outline` - Created, reset, updated, cleaned up via `BoilerMonitorEntityService` - `PollAndUpdateAsync` passes full `BoilerActionResult` to entity service ## Files Affected | File | Change | |---|---| | `BoilerMonitor.Models/BoilerMonitorConsts.cs` | New constants | | `BoilerMonitor.Manager/Interface/IWaterReadyService.cs` | Add `ConsecutiveLowPowerCount` property | | `BoilerMonitor.Manager/WaterReadyService.cs` | Consecutive count logic | | `BoilerMonitor.Manager/Interface/IBoilerControlService.cs` | Return `BoilerActionResult` | | `BoilerMonitor.Manager/BoilerControlService.cs` | Hold time + richer return | | `BoilerMonitor.Models/Enums/BoilerAction.cs` | Unchanged | | `BoilerMonitor.Manager/Models/BoilerActionResult.cs` | New record | | `BoilerMonitor.Manager/Models/BoilerMonitorUpdateContext.cs` | Add debug fields | | `BoilerMonitor.Manager/Interface/IBoilerMonitorEntityService.cs` | Add debug entity methods | | `BoilerMonitor.Manager/BoilerMonitorEntityService.cs` | Create/reset/update/cleanup debug entity | | `BoilerMonitor.Manager/BoilerMonitorInit.cs` | Wire up new return types, pass debug context | | `BoilerMonitor.Manager.Tests/` | Tests for all new behavior |