Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions apps/predbat/car_charger_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,9 @@ def charger_control_dispatch_active(self, car_n, now):
stops the charger holding the car off for the first minutes of a new dispatch. With
octopus_intelligent_charging off Predbat follows only its own plan.

The planned dispatches come from octopus_intelligent_planned_slot where it is set, as they
do for the car's plan, so a dispatch that sensor leaves out does not run the charger.

The dispatch times on the sensor are judged against the clock rather than its on/off
state, which only refreshes every couple of minutes and would run the charger on past
the end of a dispatch. A sensor with no dispatch times falls back to its state.
Expand All @@ -535,13 +538,18 @@ def charger_control_dispatch_active(self, car_n, now):
slot = self.charger_control_dispatch_sensor(car_n)
if not slot:
return False
planned_slot = self.charger_control_planned_sensor(car_n, slot)
reads = [(planned_slot or slot, "planned_dispatches"), (slot, "completed_dispatches")]
if planned_slot:
reads.append((planned_slot, "completed_dispatches"))
dispatches = []
for attribute in ("planned_dispatches", "completed_dispatches"):
value = self.get_state_wrapper(slot, attribute=attribute)
for entity_id, attribute in reads:
value = self.get_state_wrapper(entity_id, attribute=attribute)
if isinstance(value, list):
dispatches.extend(value)
if not dispatches:
return self.get_state_wrapper(slot) == "on"
# The slot sensor's state would put back the dispatches the planned sensor left out
return False if planned_slot else self.get_state_wrapper(slot) == "on"
for dispatch in dispatches:
if not isinstance(dispatch, dict):
continue
Expand All @@ -564,6 +572,25 @@ def charger_control_dispatch_sensor(self, car_n):
return None
return slot

def charger_control_planned_sensor(self, car_n, slot):
"""The sensor this car's planned dispatches come from in place of its slot sensor, or None.

The same rule as Fetch.car_planned_slot_entity() and its caller: none set, a regex that matched
nothing, the slot sensor itself, or octopus_intelligent_slot_action_config in use all leave the
slot sensor as the plan.
"""
if "octopus_intelligent_slot_action_config" in self.args:
return None
planned = self.get_arg("octopus_intelligent_planned_slot", None, indirect=False)
if planned and not isinstance(planned, list):
planned = [planned]
if not planned or car_n >= len(planned):
return None
planned = planned[car_n]
if not planned or not isinstance(planned, str) or planned.startswith("re:") or planned == slot:
return None
return planned

async def charger_control_tick(self, now):
"""Run one cycle of charger control, releasing rather than just going quiet.

Expand Down
42 changes: 42 additions & 0 deletions apps/predbat/tests/test_car_charger_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,47 @@ def test_dispatch_times_judged_against_the_clock():
assert component.charger_control_dispatch_active(0, now) is True


PLANNED = "binary_sensor.car_planned_dispatches"


def test_planned_slot_sensor_replaces_the_slot_sensor_plan():
"""With octopus_intelligent_planned_slot set, its dispatches are the plan the charger follows, as they are
for the car's plan, so a dispatch it leaves out does not run the charger from the slot sensor."""
# The charger whose Octopus arrangement cannot be told, with control set to true
component = _octopus_component(control=True)
running = [{"start": "2026-06-01T00:00:00+00:00", "end": "2026-06-01T01:00:00+00:00"}]
component.sensors[DISPATCH].update({"state": "on", "planned_dispatches": running})
component.args["octopus_intelligent_planned_slot"] = [PLANNED]
component.sensors[PLANNED] = {"state": "on", "planned_dispatches": []}
run_async(component.charger_control_tick(_now()))
assert component.commands == [("a", "off", 0)], component.commands

# Nothing on either sensor: the slot sensor's state does not stand in for the planned sensor's empty plan
component.sensors[DISPATCH]["planned_dispatches"] = []
assert component.charger_control_dispatch_active(0, _now()) is False

# The planned sensor keeps the dispatch, and the charger runs before the plan shows it
component.sensors[PLANNED]["planned_dispatches"] = running
run_async(component.charger_control_tick(_now()))
assert component.commands == [("a", "off", 0), ("a", "on", 0)], component.commands

# One the planned sensor has seen finish counts too, as it does for the car's plan
component.sensors[PLANNED] = {"state": "off", "planned_dispatches": [], "completed_dispatches": running}
assert component.charger_control_dispatch_active(0, _now()) is True

# Naming the slot sensor itself, or a regex that matched nothing, leaves the slot sensor as the plan
component.sensors[DISPATCH]["planned_dispatches"] = running
component.sensors[PLANNED] = {"state": "off", "planned_dispatches": []}
for planned in (DISPATCH, [DISPATCH], ["re:(binary_sensor.nothing)"], [None]):
component.args["octopus_intelligent_planned_slot"] = planned
assert component.charger_control_dispatch_active(0, _now()) is True, planned

# The car plan does not read the planned sensor while an action config supplies the slots, so neither does the charger
component.args["octopus_intelligent_planned_slot"] = [PLANNED]
component.args["octopus_intelligent_slot_action_config"] = {"device_id": "x"}
assert component.charger_control_dispatch_active(0, _now()) is True


def test_is_charger_that_has_lost_its_value_is_not_a_no():
"""A dispatch sensor reporting is_charger as unknown or unavailable is cannot tell, not 'not the charger'."""
for lost in ("unknown", "unavailable", "none", ""):
Expand Down Expand Up @@ -1167,6 +1208,7 @@ def run_car_charger_control_tests(my_predbat=None):
test_octopus_rule_charge_point_releases_a_running_charger()
test_octopus_rule_car_integrated_follows_the_dispatch_sensor()
test_dispatch_times_judged_against_the_clock()
test_planned_slot_sensor_replaces_the_slot_sensor_plan()
test_is_charger_that_has_lost_its_value_is_not_a_no()
test_guest_switch_toggle()
test_octopus_rule_explicit_true_never_overrides_a_charge_point()
Expand Down
3 changes: 2 additions & 1 deletion docs/car-charging.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ With it set:
- Completed slots, which decide the off-peak rate shown for charging already done, come from **octopus_intelligent_slot**.
- Planned slots come from **octopus_intelligent_planned_slot**. Any planned slots on **octopus_intelligent_slot** are ignored for that car.
- Slots the planned sensor reports as already finished are also used, until **octopus_intelligent_slot** lists them as completed.
- A charger Predbat drives runs during the planned sensor's dispatches, not those on **octopus_intelligent_slot**, so a dispatch the planned sensor leaves out does not start it.

It is only read for a car that also has **octopus_intelligent_slot** set, and not when `octopus_intelligent_slot_action_config` is in use.
For multiple cars, set it to a list with one sensor per car.
Expand Down Expand Up @@ -718,7 +719,7 @@ The GivEnergy Gateway's `gateway_evc_control` (above) works the same way: off un
On Octopus Intelligent (or E.ON/EDF SmartFlex through the Kraken component), what Predbat does depends on which device Octopus drives for that car. Predbat reads this from the car's own dispatch sensor in `octopus_intelligent_slot`:

- **Octopus drives the charger** - your charger is the Intelligent device, or that car's Intelligent slots come from the Ohme. Predbat leaves the charger alone, and hands it back if it was holding it, so the two do not fight. Handing back never starts a charge: a charger Predbat had stopped is left for Octopus to start, an Ohme has max charge turned off, and a Zappi goes back to its own mode (Eco+ in place of Fast). The exception is a Hypervolt, which is let charge again: it is not known whether Octopus can undo a Hypervolt stop, and a stop it could not undo would leave the car unable to charge.
- **Octopus drives the car** - the car itself is the Intelligent device, so the car starts and stops its own charge. By default Predbat leaves the charger alone, and hands back a charger it was holding as usual, so one it had stopped is started again rather than left stopped. Turn on **switch.predbat_octopus_intelligent_charger_follows_car** (*expert mode*) to have Predbat drive the charger instead: on while a dispatch is running, off otherwise, so the car cannot charge outside the dispatches on its own timers. It follows both its plan and the dispatch times on the car's dispatch sensor, so a new dispatch starts the charger within a few minutes of Octopus announcing it rather than waiting for the next plan, and the charger stops when the dispatch ends.
- **Octopus drives the car** - the car itself is the Intelligent device, so the car starts and stops its own charge. By default Predbat leaves the charger alone, and hands back a charger it was holding as usual, so one it had stopped is started again rather than left stopped. Turn on **switch.predbat_octopus_intelligent_charger_follows_car** (*expert mode*) to have Predbat drive the charger instead: on while a dispatch is running, off otherwise, so the car cannot charge outside the dispatches on its own timers. It follows both its plan and the dispatch times on the car's dispatch sensor (on **octopus_intelligent_planned_slot** where it is set), so a new dispatch starts the charger within a few minutes of Octopus announcing it rather than waiting for the next plan, and the charger stops when the dispatch ends.
- **Predbat cannot tell** - for example the dispatch sensor comes from the Octopus Energy integration, which does not say, or Predbat's Octopus component has not discovered your devices yet. Setting the charger's control setting to `true` tells Predbat your charger is not the Octopus device, so Predbat drives the charger to its plan. If your charger is the Octopus device, leave its control setting off.

**switch.predbat_octopus_intelligent_charging** only changes Predbat's own planning. With it off, a charger Octopus drives is still left to Octopus - Octopus goes on switching it either way - and a charger for a car Octopus drives follows Predbat's own plan, whether or not **switch.predbat_octopus_intelligent_charger_follows_car** is on.
Expand Down
Loading