Skip to content
Merged
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
63 changes: 62 additions & 1 deletion apps/predbat/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"""

from datetime import datetime, timedelta
from utils import minutes_to_time, str2time, dp1, dp2, dp3, dp4, time_string_to_stamp, minute_data, get_now_from_cumulative, MinuteArray, strptime_aware, is_entity_id
from utils import minutes_to_time, str2time, dp1, dp2, dp3, dp4, time_string_to_stamp, minute_data, get_now_from_cumulative, MinuteArray, strptime_aware, is_entity_id, intelligent_device_is_charger
from const import (
MINUTE_WATT,
PREDICT_STEP,
Expand Down Expand Up @@ -2407,8 +2407,28 @@ def fetch_sensor_data_cars(self, save=True):
except (ValueError, TypeError):
self.log("Warn: octopus_charge_limit is set to a bad value {} for car {} in apps.yaml, must be a number".format(octopus_limit, car_n))
octopus_limit = None
# With the charger linked rather than the car, Octopus's target is a percentage to add, not a
# SoC to reach (#3034), so it counts from the SoC the car had when it was plugged in. The plug
# sensor decides where there is one - car_charging_planned can turn off at the end of a charge
# with the car still plugged in. Without one, planned dispatches only say the car is plugged in
# when they are dropped for an unplugged car
plug_arg = self.car_plug_arg(car_n)
plugged = self.car_plugged_in_now(car_n, plug_arg) if plug_arg else None
if plugged is None:
plugged = bool(self.car_charging_planned[car_n] or self.car_charging_now[car_n] or (planned and not self.octopus_intelligent_ignore_unplugged))
base_soc = self.octopus_charge_target_base_soc(car_n, plugged, save)
if octopus_limit:
octopus_limit = dp3(float(octopus_limit) * self.car_charging_battery_size[car_n] / 100.0)
added = self.octopus_charge_target_is_added(entity_id, car_n)
# Said when it changes, not on every cycle. While unplugged the base follows the SoC, so it
# is not a plug-in SoC yet and is left out
logged_key = (added, base_soc if plugged else None)
if save and logged_key != self.octopus_charge_target_logged.get(car_n):
self.octopus_charge_target_logged[car_n] = logged_key
if added and plugged:
self.log("Car {} Octopus is linked to the charger, so its target adds {}kWh to the {}kWh the car had when plugged in".format(car_n, octopus_limit, base_soc))
Comment thread
chalfontchubby marked this conversation as resolved.
Resolved
if added:
octopus_limit = dp3(base_soc + octopus_limit)
self.car_charging_limit[car_n] = min(self.car_charging_limit[car_n], octopus_limit)
if octopus_ready_time:
self.car_charging_plan_time[car_n] = octopus_ready_time
Expand Down Expand Up @@ -2471,6 +2491,47 @@ def fetch_sensor_data_cars(self, save=True):
if save:
self.car_manual_soc_mirror()

def octopus_charge_target_is_added(self, entity_id, car_n=0):
"""
Is the Octopus Intelligent charge target for this dispatch sensor a percentage to add, rather than a SoC to reach?

Octopus takes a percentage to add when the charger is linked, and a SoC to reach when the car is (#3034).
Which device is linked is decided by intelligent_device_is_charger(), from the dispatch sensor and the
car's octopus_charge_limit entity when that is an entity rather than a number. None when nothing says,
which keeps the target read as a SoC to reach, as it always was - and for car 0 when another charger
component (Ohme) has claimed its charge limit, which is then that charger's own SoC target.
"""
if car_n == 0 and self.car_slot_owner not in (None, "octopus"):
# Another charger component (Ohme) has wired octopus_charge_limit to its own target, a SoC to reach
return None
target = self.get_arg("octopus_charge_limit", None, indirect=False, index=car_n)
target_attribute = None
if is_entity_id(target):
target_attribute = lambda name: self.get_state_wrapper(entity_id=target, attribute=name)
return intelligent_device_is_charger(lambda name: self.get_state_wrapper(entity_id=entity_id, attribute=name), target_attribute)

def octopus_charge_target_base_soc(self, car_n, plugged, save):
"""
The car's SoC (kWh) when it was plugged in, which a charger-linked Octopus target adds to.

While the car is unplugged this follows its SoC; once it is plugged in it is held at the last reading from
before, so a stale or partial low reading while it charges cannot drag the base, and with it the limit,
down. A SoC of 0 is a sensor that could not be read, so it never replaces a base. Not saved: after a
restart part way through a charge it starts again from the SoC then, which can only put the limit
higher, not lower. Only the live fetch records it - compare.py's re-runs (save=False) take the live
fetch's as it is, since car_charging_planned there is no longer the plug sensor's reading.
"""
soc = self.car_charging_soc[car_n]
base = self.octopus_charge_target_base.get(car_n)
if not save:
return soc if base is None else base
if soc > 0 and (not plugged or not base):
base = soc
if base is None:
return soc
self.octopus_charge_target_base[car_n] = base
return base

def fetch_pv_forecast(self):
"""
Fetch PV forecast data from one or more sensors
Expand Down
3 changes: 3 additions & 0 deletions apps/predbat/predbat.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,9 @@ def reset(self):
self.octopus_url_cache = {}
self.dispatch_timeline_last = {}
self.dispatch_timeline_pending = []
self.octopus_charge_target_base = {}
# car_n -> (target added, base SoC) last logged, so the charger-linked target is logged when it changes
self.octopus_charge_target_logged = {}
self.dispatch_unconfirmed_last = {}
self.ge_url_cache = {}
self.github_url_cache = {}
Expand Down
Loading
Loading