Skip to content

Fix for issue #1008 - heatpump running shorter than minimum run length - #1073

Open
soeter01 wants to merge 74 commits into
corneel27:mainfrom
soeter01:main
Open

soeter01 wants to merge 74 commits into
corneel27:mainfrom
soeter01:main

Conversation

@soeter01

Copy link
Copy Markdown
Collaborator

No description provided.

@dewi-ny-je

Copy link
Copy Markdown
Contributor

LLM review

  1. Crash on every run with no heat demand (day_ahead.py ~2487 and
    ~3688). With heat_needed <= 0, hp_enabled now stays True, but the
    whole else: branch that creates h_hp, blocks_num, hp_start_index,
    block_len, hp_bl_on is skipped. After the solve, the report block
    if self.hp_present and self.hp_enabled: runs if blocks_num > 0:, so
    calc_optimum raises UnboundLocalError: cannot access local variable 'blocks_num' (and next h_hp[u].x on None). Both names are locals of
    calc_optimum, assigned only inside that branch (blocks_num at ~2904;
    the one at ~2628 is inside a """ string). This hits every heat pump user
    once today's heat is produced, and every run on mild days (degree days 0).
    The output step that should switch the pump off is never reached, so the
    Fix for on/off heatpump not switching off when required heat has been generated #1047 goal fails too. The scenario heatpump-mild-day-idle
    (scenarios/cases/heatpump.json, 18 °C, no heat demand) runs exactly this
    path, so CI's "Scenario suite (solve)" should fail. Fix: keep a separate
    flag, e.g. self.hp_planned = self.hp_enabled and heat_needed > 0, use it
    for the report/log block at ~3688 and keep hp_enabled for the output
    step; or initialise blocks_num = 0, h_hp/p_hp and guard the log loop.

  2. The current partial quarter still resets the count (part A, unchanged
    from Fix for issue #1008: run length of heat pump is sometimes shorter than minimum run length #1062 item 2). The last endpoint is now, compared with the total at
    the last quarter boundary. Checked on SQLite with the PR's query and loop:
    a pump that has run 3 h with the sensor updating every 60 s gives
    0.0 h when the calc starts at hh:15:05, and 2.75 h when it starts at
    hh:14:30. With 0 the code takes the "staat stil" path
    (first_block_start = None), so the solver may switch the pump off before
    the minimum run length: the Heating runlength not taken into account correctly in calculation when interval = 15min #1008 symptom. Fix: drop the partial segment
    when it is shorter than a few minutes, or let a partial segment without a
    rise keep the count instead of resetting it.

  3. Sensor resolution / update rate (part A). A quarter counts as running
    only if the sensor rose within that quarter. Many heat pumps report heat
    produced in whole kWh, or update every few minutes. Checked: 1 kWh
    resolution and 2 kW thermal (one step per 30 min) gives 0.0 h for a
    pump running for 3 h. Either look at a longer window (rise in the last
    30–60 min), or fall back to / combine with consumption (the existing
    get_heatpump_run_hours with wp_consumption_sensors).

  4. Daily-reset sensor breaks the count at midnight (Fix for issue #1008: run length of heat pump is sometimes shorter than minimum run length #1062 item 5).
    entity hp heat produced is the heat produced today (it is subtracted
    from today's need at ~2470). At 00:00 the total drops, the quarter is
    counted as "not running" and the count restarts. Checked: a pump running
    since 22:00 gives 0.25 h at 00:29:30. Treat total < previous as a reset
    and count the new value as production.

  5. Snapshot capture/replay doesn't cover the new method (Fix for issue #1008: run length of heat pump is sometimes shorter than minimum run length #1062 item 6).
    da_debug.py patches Report.get_heatpump_run_hours only. With
    entity hp heat produced set, a capture records nothing for run length,
    and a replay hits _FakeDbManager.__getattr__ (db_ha.engine) and fails
    with SnapshotMiss. The scenario corpus doesn't set that entity, so CI
    won't show it, but user snapshots of heat pump setups will. Patch the new
    method the same way, or keep the old name.

  6. Rounding throws away the gain (Fix for issue #1008: run length of heat pump is sometimes shorter than minimum run length #1062 item 7). With 1 h minimum and
    0.75 h run, max(1, 1 - floor(0.75)) plans another full hour (1.75 h).
    block_len is already int(first_block_len * 3600 / interval_s), so for
    15min use max(0.25, min_run_length - run_hours) (rounded up to the
    interval), for 1hour math.ceil on the remainder. Check the on/off
    blocks_num/last_block_len formulas with a fractional first block.

  7. Raw SQL portability (Fix for issue #1008: run length of heat pump is sometimes shorter than minimum run length #1062 item 10). WITH RECURSIVE and
    CAST(... AS FLOAT) need MySQL ≥ 8.0.17 / MariaDB ≥ 10.4.5. One query for
    all states of the entity in the window plus a Python loop would avoid
    dialect issues and match the SQLAlchemy Core style of the other HA queries.

Minor

  1. -2 ("geen data") is unreachable: quarters always yields rows, so an
    entity with no states (typo) returns 0 and the log says "Warmtepomp staat
    stil". Return -2 when all totals are NaN.
  2. The inner on/off if heat_needed > 0: ... else: "Geen warmtebehoefte" is
    now dead code; the commented-out # self.hp_enabled = False
    should go.
  3. "Warmtepomp draait al minimaal {run_hours} uur" now prints 0.75 etc.;
    the new info line fires every quarter while the first block runs.

@soeter01

soeter01 commented Nov 30, 2026 via email

Copy link
Copy Markdown
Collaborator Author

@dewi-ny-je

dewi-ny-je commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Diff provided:
pr1073-fixes.diff.txt

New findings

A. Blocking: crash when the sensor has no state older than 5 h
(da_report.py, loop in get_heatpump_run_hours). difference is only
assigned in the else branch, but every row inside the window appends
"diff": difference. If the first endpoint (now − 5 h) has no state, the
second row has previous_total is None and difference was never set:
UnboundLocalError, and the whole calc stops. Checked: no states at all
(typo or newly created entity) and history that starts 2 h ago both crash.
A sensor whose older states were purged by the recorder would do the same.
Fix: set difference = None (or float("nan")) before the if, and return
-2 when all totals are NaN.

B. Should fix: the 0.2 kWh threshold misses low and quantised output.
0.2 kWh per quarter is 0.8 kW thermal, and the test is strictly >.
Checked: 1 kW thermal on a sensor with 0.1 kWh steps gives per-quarter rises
of 0.2 or 0.3, so it reads 0.25 h for a pump that has run for 3 h; exactly
0.8 kW gives 0.0 h; 1 kWh resolution at 2 kW also gives 0.25 h. Float noise
makes it worse (10.2 - 10.0 = 0.19999…). The threshold also assumes the
sensor is in kWh; a Wh sensor makes it meaningless. Better: any rise counts,
look at a 30–60 min window for coarse sensors, and make the unit explicit
(or read unit_of_measurement).

C. Should fix: the midnight reset now forces a start. A drop is counted
as "running" (difference < 0). With a daily-reset heat sensor and the pump
off all evening, a calc between 00:00 and 00:15 sees the 35 → 0 drop and
gets 0.25 h (checked at 00:00:30 and 00:14:00). With eco/max demand that
takes the "draait korter dan minimum" branch: first_block_start = 0 and a
full min_run_length block, so the pump is switched on at midnight whether
or not that is the cheapest time. Once on, the next runs see it rising and
keep it on. Fix: on a drop, use the new total as the rise
(difference = total when total < previous), so a reset only counts when
heat was actually produced after it.

@soeter01

soeter01 commented Dec 2, 2026 via email

Copy link
Copy Markdown
Collaborator Author

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants