Repository navigation
fix(octopus): count the IOG cap in minutes the car charged, not whole completed half hours (#6252) - #6255
fix(octopus): count the IOG cap in minutes the car charged, not whole completed half hours (#6252)#6255chalfontchubby wants to merge 1 commit into
Conversation
… completed half hours (#6252) Octopus counts "the actual amount of time the car is charged for" towards the 6-hour Intelligent cap, but reports completed dispatches as whole half hours, so a half hour charged in for two minutes used thirty. Where every car with Intelligent dispatches has a car_charging_energy sensor, a past minute its raw records show no charging in now uses none of the cap, while still fitting the budget so its half hour stays cheap. Unknown minutes (before the first record, after the last, across a reset or an unavailable spell) still count; nothing changes without a sensor or under the window billing rule. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
springfall2008
left a comment
There was a problem hiding this comment.
Automated comment from the triage bot.
Reviewed head f56c5a4. 5 findings posted inline, 0 in this body.
The core arithmetic checks out against the tests: I traced octopus_cap_entity_idle() minute by minute over the cap_actual_runs_out and cap_actual_trickle fixtures (including the two minutes 518-519 that the first-step-after-a-pause rule marks busy) and the grant/over-cap split matches the expectations exactly. take_cap_dispatch_minutes() still returns a prefix of wanted_minutes, so both counters keep agreeing. Findings are about the inputs it trusts, the cost of the read, and coverage.
| for car_n in cars: | ||
| if car_n >= len(entities) or not is_entity_id(entities[car_n]): | ||
| return None | ||
| car_idle = self.octopus_cap_entity_idle(entities[car_n]) |
There was a problem hiding this comment.
Automated comment from the triage bot.
car_charging_energy is not a per-car list of car chargers: it is a generic excluded-load list. docs/car-charging.md (line 80-85) shows [zappi_regex, sensor.mixergy_ID_energy, sensor.ashp_energy_today] as the recommended shape, and the debug journal records the same ("car_charging_energy is a generic excluded-load list (any incrementing kWh sensor, heat pumps too)"). Taking entities[car_n] as car car_n's charging record therefore only holds while every non-car entry sits after that car's charger.
Failure scenario. Two cars with Intelligent dispatches, list [sensor.zappi_car1_session_energy, sensor.mixergy_ID_energy, sensor.ashp_energy_today, sensor.zappi_car2_session_energy] (the natural way to extend the documented example). Car 1 gets sensor.mixergy_ID_energy, so its idle set marks car 1's charging minutes as idle whenever the hot water tank is off. The intersection in octopus_cap_idle_load() then removes those minutes from the shared budget, the cap under-counts, and Predbat prices dispatches past the 6-hour allowance at the cheap rate and plans the house battery to charge in them - the opposite error to the one #6252 reports, and the one that costs the user money.
Verified: read the docs example, the journal entry, and car_manual_soc_energy_history() (fetch.py:2186), which indexes the same way - so the positional indexing is not new, but there a wrong entry only mis-assigns a load-subtraction input. Not confirmed: how many users have dispatches for more than one car and a multi-load list, so this is config-dependent. A log naming the sensor used per car would make it visible; the multi-load case could also be documented on car_charging_energy itself.
| value = None | ||
| records.append((minute, value)) | ||
| records.sort(key=lambda record: record[0]) | ||
| while len(records) > 1 and records[1][0] <= cutoff: |
There was a problem hiding this comment.
Automated comment from the triage bot.
get_history_wrapper(days=2) does not bound the records this loop has to trim. HAHistory.get_history() answers from the cached store whenever it holds at least days, and EntityHistory.to_records() has no time filter - it materialises the whole store, which is max_days_previous days (8 by default: max(days_previous) + 1 with days_previous = [7]). So every cycle this re-formats, re-parses and re-sorts ~8 days of a car energy sensor to use 36 hours of it.
Cost. records.pop(0) is a memmove of the remaining list on each call, so the trim is quadratic in the record count. A sensor that records every 5 s while charging - the shape car_energy_records() models in the new test - gives ~3,600 records per charging hour; 6.5 days of that is ~23,000 front-pops from a list of ~28,000, i.e. a few GB of memmove per car per 5-minute cycle, on top of to_records()'s per-record datetime.fromtimestamp().isoformat() and a str2time() parse here. At one record a minute the cost is negligible, so the impact scales with the sensor's recording rate - I could not measure it on real hardware from here.
Simpler form. The store is already oldest-first, so bisect the cutoff index and slice (records = records[start_index:]) instead of popping, or give to_records() an optional start epoch and keep the days=2 intent. This runs for every IOG setup that has car_charging_energy, on the same cycle as load_car_energy()'s own read of the same entity, so it is a doubling of a cost that is already paid once.
|
|
||
| # The past minutes no car charged in, which use none of the Octopus Intelligent daily cap (#6252): read once | ||
| # every car's dispatches are in, before the car plans below and the rates count the cap with them | ||
| self.octopus_cap_idle = self.octopus_cap_idle_load() |
There was a problem hiding this comment.
Automated comment from the triage bot.
Nothing in the PR exercises this line or its position. run_cap_actual_charging_tests calls octopus_cap_idle_load() directly with get_history_wrapper monkeypatched, so the wiring could regress silently: moved above the dispatch loop at fetch.py:2581-2590 it reads last cycle's self.octopus_slots; moved below rate_add_io_slots() at fetch.py:1882, or dropped in a refactor, the rates get None and the cap quietly reverts to counting whole half hours - which is main's behaviour, so the whole suite stays green either way.
That ordering is the load-bearing part of the change (it is the first thing the PR description calls out), so it deserves the same guard test_set_rate_thresholds.py:1244 already uses for a comparable trap. A case that runs fetch_sensor_data_cars() against a stubbed history and asserts my_predbat.octopus_cap_idle is the expected set would cover both the reset at line 2530 and the read here.
| - **octopus_slot_low_rate** - Default is `true`, meaning any Octopus Intelligent Slot reported will be at the lowest rate if at home. If `false` the existing rates only will be used which is only suitable for tariffs other than IOG. | ||
|
|
||
| - **octopus_slot_max** - Sets the maximum cheap rate dispatch time per 24-hour period, in 30-minute slots (12 is 6 hours). It is counted in the minutes of each dispatch as Octopus reports them, so a 10-minute dispatch uses 10 minutes of it. With more than one car on Intelligent Octopus the limit is shared between them (see [Multiple cars with Octopus Intelligent Go](#multiple-cars-with-octopus-intelligent-go-iog)). Dispatches beyond this limit will use standard rates. | ||
| - **octopus_slot_max** - Sets the maximum cheap rate dispatch time per 24-hour period, in 30-minute slots (12 is 6 hours). It is counted in the minutes of each dispatch as Octopus reports them, so a 10-minute dispatch uses 10 minutes of it. Octopus counts the time the car actually charges for, but reports a finished dispatch in whole half hours, so when **car_charging_energy** is set for every car with Intelligent dispatches, a minute that has passed with no charging recorded on it uses none of the limit. A minute after the sensor last changed, or while it was unavailable, still counts until a later reading shows what happened. With more than one car on Intelligent Octopus the limit is shared between them (see [Multiple cars with Octopus Intelligent Go](#multiple-cars-with-octopus-intelligent-go-iog)). Dispatches beyond this limit will use standard rates. |
There was a problem hiding this comment.
Automated comment from the triage bot.
This sentence promises the behaviour whenever car_charging_energy is set for every car with Intelligent dispatches, but octopus_cap_entity_idle() returns None - silently disabling it - unless the sensor's unit_of_measurement is kWh, Wh or MWh (OCTOPUS_CAP_ENERGY_UNITS, octopus.py:43). A power-unit sensor is handled for this same setting elsewhere: minute_data() integrates W against required_unit="kWh" here and in the Load ML component.
Cost. A user with a W/kW car energy sensor reads this page, expects a 2-minute charge in a completed half hour to use 2 minutes, and gets main's whole-half-hour count with nothing in the log to say why. Either say in this paragraph that the sensor must report energy, or handle the power units as minute_data() already does. The PR body records this as a known limit, but the user-facing page is where the reader looks.
| idle = None | ||
| for car_n in cars: | ||
| if car_n >= len(entities) or not is_entity_id(entities[car_n]): | ||
| return None |
There was a problem hiding this comment.
Automated comment from the triage bot.
This return None, and the one at line 3966, are the four ways the feature decides it has nothing to go by: a car with dispatches but no car_charging_energy entry, an entry that fails is_entity_id(), a sensor with no usable history, and a non-energy unit. All four are silent, and the existing Octopus: Intelligent slot minutes for day N: X of Y max line (octopus.py:4757) still reports the undiscounted count when they fire.
Cost. This is the exact report a user will file against #6252 - "it is still counting whole half hours" - and the log gives a maintainer nothing to tell the four causes apart, nor to tell a working setup apart from one that quietly fell back. One line, once per change of cause, would settle it; RepeatLogGate is already used in this file for exactly that pattern, so no new mechanism is needed.
Posted by Claude on behalf of @chalfontchubby.
Fixes #6252.
Problem
On IOG-SMB tariffs Predbat counts the 6-hour cap in the minutes of each dispatch Octopus reports. That is right for planned dispatches, which carry exact times. But Octopus reports a completed dispatch as whole half-hour blocks, so a half hour the car charged in for 2 minutes uses 30 minutes of the cap.
In #6252, for 16 Nov 12:00 to 17 Nov 12:00, Predbat counted 17 completed half hours (510 min). The Ohme app shows 6h10m (370 min) of active charging: 140 minutes over-counted. Predbat decided the allowance was spent overnight and priced the 17 Nov 03:00 and 06:00–08:00 dispatches at peak, so the house battery did not charge in them. The reporter worked around it with
octopus_slot_max: 32.What Octopus says it counts
From Octopus's Intelligent Octopus Go charge limit FAQ:
Relation to #5510
#5510 decided Predbat counts the start and end Octopus sends for a completed dispatch and does not try to work out real charging minutes. That rested on Octopus sending the real start and end, and it doesn't: in the #6252 log every
completedDispatchesentry is a whole half hour (16:30–17:00,16:00–16:30, ...) with adeltakWh, and evenflexPlannedDispatchesare mostly on half-hour boundaries. The dispatch list cannot say how long the car charged, and the FAQ says that is what counts. This PR reverses that part of #5510, only where a car energy sensor gives the answer. Everything else in #5510 stays.Change
octopus_cap_idle_load()(read once a cycle infetch_sensor_data_cars(), after the dispatches are in and before the car plans and rates use them): the past minutes in which no car with Intelligent dispatches charged. Each such car needs its owncar_charging_energysensor with history, as the cap is per account; otherwise nothing is taken as idle.octopus_cap_entity_idle()works from the sensor's raw Home Assistant records, not the padded and smoothedcar_charging_energyseries, which reads as flat (idle) before its history starts and after its last update. A minute is idle only when it lies between two readings and the sensor rose by less than 0.5 kW in it. Unknown, so still counted: before the first record, after the last one, across a reset, and from anunavailablerecord to the next reading. The first rise after a pause is taken as charging for the minutes it would have needed at 0.5 kW before its record. A sensor records a change late, and a polled one later still, so smoothing would otherwise spread that rise back across the pause.take_cap_dispatch_minutes()replacestake_cap_minutes()and is shared by both counters:rate_add_io_slots(), andoctopus_cap_segments()for the car plan andoctopus_over_cap_minutes(). An idle minute still fits while budget is left, so its half hour stays cheap as Octopus bills it, but it uses none of the budget.intelligent_dispatch_billing: window), which bills the whole window whether the car charges or not (asdispatch_too_short()does).octopus_slot_maxindocs/car-charging.mdanddocs/apps-yaml.md.No setting changes. Without
car_charging_energy, behaviour is unchanged.Tests
run_cap_actual_charging_testsintests/test_rate_add_io_slots.pybuilds raw HA records (a reading every 5 s while charging) and runs the real loader. Each counting case goes through both counters.The new rate cases fail against main's
octopus.pyand pass with the change../run_pre_commit(including the quick suite) passes locally. Reviewed by a cold subagent and/code-review; the stale-idle ordering, padded-history and unavailable-gap findings from those are fixed here.Known limits
minute_data(). A power-unit sensor (W or kW) is not used.octopus_slot_max, unchanged).Settled billing
The reporter's settled Octopus half-hourly data for Mon 16 and Tue 17 Nov has every half-hour that had a dispatch billed at 6.60p, car and house. That includes Tue 06:00–08:00, which main priced at peak after counting whole half-hours. This matches counting the minutes the car charged. The earlier standard-rate figures were Octopus's first estimate. Predbat's History view still shows those figures, because it never reads a complete day again; that is a separate bug, #6274.
🤖 Generated with Claude Code