batpred icon indicating copy to clipboard operation
batpred copied to clipboard

Hypervolt component: a restart mid-session publishes session energy 0, and the car's energy is counted twice

Open chalfontchubby opened this issue 1 week ago • 1 comments

Opened by Claude on behalf of @chalfontchubby.

Summary: if Predbat restarts part-way through a car charge, the built-in Hypervolt component publishes session_energy as 0 until the charger sends its next session message. If that 0 stands for more than a few minutes, Predbat's history reader treats the drop as a counter reset and counts the session's energy so far a second time when the real value comes back. Home Assistant's long-term statistics count it twice too, because the sensor is total_increasing.

Mechanism (main at 378fef26)

  1. HypervoltCharger.session_energy_kwh defaults to 0.0 (hypervolt.py:155).
  2. On the first run, wait_for_first_message() waits for any message from the charger (fresh_chargers), not specifically a session message carrying watt_hours (hypervolt.py:1054-1058).
  3. publish_data() then publishes sensor.predbat_hypervolt_<id>_session_energy = 0.0 (hypervolt.py:1200).
  4. When the next session message arrives, apply_session() sets the running total again, so the sensor goes 4.5 → 0 → 4.5.
  5. minute_data(..., clean_increment=True) treats any drop of 1 kWh or more as a reset (utils.py:1719), so the rise from 0 back to 4.5 is counted as new energy. car_charging_energy is wired to this sensor by automatic_config(), so the car's energy, and from it the house load with the car removed, is overstated.

Reproduction (a script using the tests/test_hypervolt.py helpers _make_component and apply_session, feeding the published values through minute_data the way car_charging_energy is read). The case is a 5 kWh session, with Predbat restarted at 4.5 kWh:

Case Energy counted over the session
No restart 4.97 kWh
Sensor reads 0 for 1 minute, then 4.5 4.97 kWh (smoothed away)
Sensor reads 0 for 10 minutes, then 4.5 9.47 kWh
Sensor reads 0 for 25 minutes, then 4.5 9.47 kWh
Genuine new session after the restart (0 → 0.3) 5.27 kWh (5.30 true): correct

The first run publishing 0.0 before any session message was confirmed directly from the component.

When it matters: only when no session message arrives within a few minutes of the restart. While the car is actively charging, messages probably arrive often enough that the 0 is brief (not measured). The exposure is a restart while a V2 session is paused between dispatches, where the 0 can stand until charging resumes. On V3, watt_hours resets at each stop anyway, so a pause already starts a new session.

Suggested fix: don't publish session_energy (or publish it as unavailable) until a session message with watt_hours has arrived since startup, or restore the last value from before the restart. A regression test in tests/test_hypervolt.py should check that the first run publishes no 0, and that the history reader skips the unavailable rows cleanly.

Related: #6142 (session energy sensor recording no history at one site, a different cause).

🤖 Generated with Claude Code

chalfontchubby avatar Nov 20 '26 14:11 chalfontchubby

This is an automated first-pass triage; a maintainer will review before any action is taken.

Classification: bug · Priority: priority_low

The bug: after a restart the Hypervolt component publishes session_energy = 0 until a session message carrying watt_hours arrives; if that 0 is recorded and stands long enough that the next recorded value is the true session energy, minute_data(clean_increment=True) treats the drop as a counter reset and counts the session's energy so far a second time, overstating car_charging_energy and the house load with the car removed.

I re-checked the mechanism against current main (v9.9.0-2-g378fef26) and confirmed each step:

  • HypervoltCharger.session_energy_kwh defaults to 0.0 (hypervolt.py:155), and apply_snapshot — a non-session message — never sets it, but it does refresh the charger, so wait_for_first_message()'s fresh_chargers() check is satisfied (hypervolt.py:1054-1057) and publish_data() then publishes 0.0 at hypervolt.py:1200.
  • minute_data's clean_increment path pads a drop of ≥1 kWh back to the low value (utils.py:1719). Home Assistant records a total_increasing sensor only when it changes, so the next record after the spurious 0 is the real value d minutes later, and the interpolated 0→true rise is counted as fresh energy.

I reproduced it with a 5 kWh session, a restart at 4.5 kWh, and change-only records fed through the same minute_data(...) call minute_data_import_export makes for car_charging_energy:

0 stands for Energy counted (true 5.00 kWh)
1 minute 4.92 kWh (the 1-minute rise is capped by max_increment and dropped)
10 minutes 9.42 kWh
25 minutes 9.42 kWh

The pattern and magnitude match your report (absolute figures differ a little because the session model differs). So the report is sound: a zero held for more than a minute double-counts, a brief one does not. The existing hypervolt test suite passes on main.

The suggested fix looks right to me: don't publish session_energy (or publish it unavailable) until a watt_hours-bearing session message has arrived since startup, or restore the last value across a restart; and add a regression test that the first run publishes no 0 and that the reader skips the unavailable rows.

Keeping priority_low: the exposure is a restart while a V2 session is paused between dispatches, and while charging the next session message arrives quickly. No further information needed.

Related, not duplicates: #6142 (same sensor but a recorder-history cause), #5752 (the opposite problem — energy reaching the history late). No open fix PR for this.

springfall2008 avatar Nov 20 '26 14:11 springfall2008