diff --git a/apps/predbat/fetch.py b/apps/predbat/fetch.py index 16f60d8f11..1ac5b1c934 100644 --- a/apps/predbat/fetch.py +++ b/apps/predbat/fetch.py @@ -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, @@ -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)) + 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 @@ -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 diff --git a/apps/predbat/predbat.py b/apps/predbat/predbat.py index 588b60aab8..e6c30c1da4 100644 --- a/apps/predbat/predbat.py +++ b/apps/predbat/predbat.py @@ -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 = {} diff --git a/apps/predbat/tests/test_octopus_charge_target.py b/apps/predbat/tests/test_octopus_charge_target.py new file mode 100644 index 0000000000..fffc0ef029 --- /dev/null +++ b/apps/predbat/tests/test_octopus_charge_target.py @@ -0,0 +1,318 @@ +# ----------------------------------------------------------------------------- +# Predbat Home Battery System +# Copyright Trefor Southwell 2026 - All Rights Reserved +# This application maybe used for personal use only and not for commercial use +# ----------------------------------------------------------------------------- +# fmt off +# pylint: disable=consider-using-f-string +# pylint: disable=line-too-long +# pylint: disable=attribute-defined-outside-init +""" +The Octopus Intelligent charge target is a percentage to add when the charger is linked, not a SoC (#3034). + +With the car linked Octopus's target is the SoC to charge the car to. With the charger linked Octopus +cannot see the car, so the same number is how much to add. Read as a SoC, a car plugged in at 39% with +"add 40%" was taken as already charged as soon as the charge Octopus does at plug-in took it past 40%, +and its Intelligent slots were then ignored. +""" +import copy +from datetime import timedelta + +IOG_SENSOR = "binary_sensor.octopus_charge_target_test" +BATTERY_KWH = 62.0 + +STATE_FIELDS = ( + "num_cars", + "car_charging_now", + "car_charging_planned", + "car_charging_soc", + "car_charging_soc_next", + "car_charging_limit", + "car_charging_limit_model", + "car_charging_rate", + "car_charging_loss", + "car_charging_plan_time", + "car_charging_slots", + "car_charging_manual_soc", + "car_charging_battery_size", + "octopus_slots", + "octopus_intelligent_charging", + "octopus_intelligent_ignore_unplugged", + "octopus_charge_target_base", + "octopus_charge_target_logged", + "dispatch_timeline_pending", + "rate_import", +) +SAVED_ARGS = ("octopus_intelligent_slot", "octopus_charge_limit", "octopus_ready_time", "car_charging_soc", "car_charging_limit") + + +def _check(name, condition, detail=""): + """ + Print an error for a failed condition and return whether it failed. + """ + if not condition: + print("ERROR: {} {}".format(name, detail)) + return True + return False + + +def _sensor(my_predbat, attributes, planned=None): + """ + Point the Octopus Intelligent slot at a dispatch sensor carrying these attributes and planned dispatches. + """ + my_predbat.args["octopus_intelligent_slot"] = IOG_SENSOR + my_predbat.ha_interface.set_state(IOG_SENSOR, "off", attributes={"planned_dispatches": planned or [], "completed_dispatches": [], **attributes}) + + +def _fetch(my_predbat, soc_percent, target_percent, plugged=True, save=True, car_limit_percent=100, charging_now=False, ignore_unplugged=False): + """ + Run the car fetch for one car at soc_percent with an Octopus target of target_percent, returning its charge limit. + """ + my_predbat.num_cars = 1 + my_predbat.car_charging_now = [charging_now] + my_predbat.car_charging_planned = [plugged] + my_predbat.car_charging_manual_soc = [False] + my_predbat.car_charging_battery_size = [BATTERY_KWH] + my_predbat.car_charging_limit = [BATTERY_KWH] + my_predbat.car_charging_rate = [7.4] + my_predbat.car_charging_plan_time = ["07:00:00"] + my_predbat.car_charging_slots = [[]] + my_predbat.octopus_slots = [[]] + my_predbat.octopus_intelligent_charging = True + my_predbat.octopus_intelligent_ignore_unplugged = ignore_unplugged + my_predbat.dispatch_timeline_pending = [] + # No rates, so the fetch leaves the shared fixture's rate statistics alone + my_predbat.rate_import = {} + my_predbat.args["car_charging_soc"] = soc_percent + my_predbat.args["car_charging_limit"] = car_limit_percent + my_predbat.args["octopus_charge_limit"] = target_percent + my_predbat.args.pop("octopus_ready_time", None) + my_predbat.fetch_sensor_data_cars(save=save) + return my_predbat.car_charging_limit[0] + + +def _run_detection(my_predbat): + """ + Which attribute says the target is a percentage to add, and which way round each one reads. + """ + failed = False + print("Test 1: telling a linked charger from a linked car") + cases = [ + ("Predbat Octopus component, charger", {"is_charger": True, "vehicle_battery_size_in_kwh": None, "charge_point_power_in_kw": 7.0}, True), + ("Predbat Octopus component, car", {"is_charger": False, "vehicle_battery_size_in_kwh": 75.0, "charge_point_power_in_kw": None}, False), + ("is_charger wins over the sizes", {"is_charger": False, "vehicle_battery_size_in_kwh": None, "charge_point_power_in_kw": 7.0}, False), + ("HA integration device_type, charger", {"device_type": "ELECTRIC_VEHICLE_CHARGERS", "vehicle_battery_size_in_kwh": None, "charge_point_power_in_kw": 7.0}, True), + ("HA integration device_type, car", {"device_type": "ELECTRIC_VEHICLES", "vehicle_battery_size_in_kwh": 75.0, "charge_point_power_in_kw": None}, False), + ("older HA integration, charger", {"vehicle_battery_size_in_kwh": None, "charge_point_power_in_kw": 7.0}, True), + ("older HA integration, charger listed without a power", {"vehicle_battery_size_in_kwh": None, "charge_point_power_in_kw": 0}, True), + ("older HA integration, car", {"vehicle_battery_size_in_kwh": 75.0, "charge_point_power_in_kw": None}, False), + ("both sizes given", {"vehicle_battery_size_in_kwh": 75.0, "charge_point_power_in_kw": 7.0}, None), + ("older HA integration, charger or car Octopus does not list", {"vehicle_battery_size_in_kwh": None, "charge_point_power_in_kw": None}, None), + ("no attributes at all", {}, None), + ] + for name, attributes, expected in cases: + my_predbat.ha_interface.set_state(IOG_SENSOR, "off", attributes=attributes) + result = my_predbat.octopus_charge_target_is_added(IOG_SENSOR) + failed |= _check("t1 " + name, result is expected, "expected {} got {}".format(expected, result)) + return failed + + +def _run_target_entity_detection(my_predbat): + """ + device_type on the car's charge target entity (BottlecapDave #1917) decides, ahead of the sizes; a target set as a + plain number has no entity to read, so the dispatch sensor alone decides. is_charger as a string is read too. + """ + failed = False + print("Test 1b: device_type on the charge target entity, and is_charger as a string") + target = "number.octopus_energy_test_intelligent_charge_target" + saved = my_predbat.args.get("octopus_charge_limit") + try: + my_predbat.args["octopus_charge_limit"] = target + sizes_say_car = {"vehicle_battery_size_in_kwh": 75.0, "charge_point_power_in_kw": None} + my_predbat.ha_interface.set_state(IOG_SENSOR, "off", attributes=sizes_say_car) + for device_type, expected in (("ELECTRIC_VEHICLE_CHARGERS", True), ("ELECTRIC_VEHICLES", False)): + my_predbat.ha_interface.set_state(target, "80", attributes={"device_type": device_type}) + result = my_predbat.octopus_charge_target_is_added(IOG_SENSOR, 0) + failed |= _check("t1b target entity says {}".format(device_type), result is expected, "expected {} got {}".format(expected, result)) + my_predbat.ha_interface.set_state(target, "80", attributes={}) + result = my_predbat.octopus_charge_target_is_added(IOG_SENSOR, 0) + failed |= _check("t1b target entity without device_type falls back to the sizes", result is False, "got {}".format(result)) + my_predbat.args["octopus_charge_limit"] = 80 + my_predbat.ha_interface.set_state(IOG_SENSOR, "off", attributes={"vehicle_battery_size_in_kwh": None, "charge_point_power_in_kw": 7.0}) + result = my_predbat.octopus_charge_target_is_added(IOG_SENSOR, 0) + failed |= _check("t1b a plain-number target is decided by the dispatch sensor", result is True, "got {}".format(result)) + for text, expected in (("true", True), ("False", False), ("unknown", None)): + my_predbat.ha_interface.set_state(IOG_SENSOR, "off", attributes={"is_charger": text}) + result = my_predbat.octopus_charge_target_is_added(IOG_SENSOR, 0) + failed |= _check("t1b is_charger '{}'".format(text), result is expected, "expected {} got {}".format(expected, result)) + finally: + if saved is None: + my_predbat.args.pop("octopus_charge_limit", None) + else: + my_predbat.args["octopus_charge_limit"] = saved + return failed + + +def _run_limit(my_predbat): + """ + The charge limit a charger-linked target gives, and that a car-linked one is unchanged. + """ + failed = False + charger = {"vehicle_battery_size_in_kwh": None, "charge_point_power_in_kw": 7.0} + car = {"vehicle_battery_size_in_kwh": BATTERY_KWH, "charge_point_power_in_kw": None} + + # The #3034 log: 39% of 62kWh, "add 40%", and the plug-in charge then takes the car to 43% + print("Test 2: a charger-linked target adds to the SoC at plug-in") + my_predbat.octopus_charge_target_base = {} + _sensor(my_predbat, charger) + _fetch(my_predbat, 39, 40, plugged=False) + limit = _fetch(my_predbat, 39, 40) + failed |= _check("t2 limit", abs(limit - (24.18 + 24.8)) < 0.01, "limit {}".format(limit)) + + print("Test 3: the limit holds while the car charges, so the car is not already charged") + limit = _fetch(my_predbat, 43, 40) + failed |= _check("t3 limit held", abs(limit - (24.18 + 24.8)) < 0.01, "limit {}".format(limit)) + failed |= _check("t3 not full", my_predbat.car_charging_soc[0] < limit, "soc {} limit {}".format(my_predbat.car_charging_soc[0], limit)) + + print("Test 4: unplugging starts the next charge from the SoC then") + _fetch(my_predbat, 30, 40, plugged=False) + limit = _fetch(my_predbat, 30, 40) + failed |= _check("t4 limit", abs(limit - (18.6 + 24.8)) < 0.01, "limit {}".format(limit)) + + print("Test 5: the car's own charge limit still caps it") + limit = _fetch(my_predbat, 30, 40, car_limit_percent=60) + failed |= _check("t5 capped", abs(limit - 37.2) < 0.01, "limit {}".format(limit)) + + print("Test 6: compare.py's re-runs take the live plug-in SoC and do not record one") + my_predbat.octopus_charge_target_base = {} + _fetch(my_predbat, 20, 40, plugged=False) + limit = _fetch(my_predbat, 10, 40, plugged=False, save=False) + failed |= _check("t6 base kept", my_predbat.octopus_charge_target_base == {0: 12.4}, "base {}".format(my_predbat.octopus_charge_target_base)) + failed |= _check("t6 limit from live", abs(limit - (12.4 + 24.8)) < 0.01, "limit {}".format(limit)) + + print("Test 7: a SoC sensor reading 0 for a cycle does not lose the plug-in SoC") + _fetch(my_predbat, 20, 40) + _fetch(my_predbat, 0, 40) + limit = _fetch(my_predbat, 25, 40) + failed |= _check("t7 limit", abs(limit - (12.4 + 24.8)) < 0.01, "limit {}".format(limit)) + + print("Test 8: a car charging now is plugged in") + my_predbat.octopus_charge_target_base = {} + _fetch(my_predbat, 20, 40, plugged=False) + limit = _fetch(my_predbat, 25, 40, plugged=False, charging_now=True) + failed |= _check("t8 limit", abs(limit - (12.4 + 24.8)) < 0.01, "limit {}".format(limit)) + + # Planned dispatches only count while Octopus keeps none for an unplugged car + start = (my_predbat.now_utc + timedelta(hours=1)).strftime("%Y-%m-%dT%H:%M:%S%z") + end = (my_predbat.now_utc + timedelta(hours=2)).strftime("%Y-%m-%dT%H:%M:%S%z") + planned = [{"start": start, "end": end, "charge_in_kwh": 7.0, "source": "smart-charge", "location": "AT_HOME"}] + print("Test 9: planned dispatches say the car is plugged in") + my_predbat.octopus_charge_target_base = {} + _sensor(my_predbat, charger) + _fetch(my_predbat, 20, 40, plugged=False) + _sensor(my_predbat, charger, planned=planned) + limit = _fetch(my_predbat, 25, 40, plugged=False) + failed |= _check("t9 limit", abs(limit - (12.4 + 24.8)) < 0.01, "limit {}".format(limit)) + + print("Test 10: but not with octopus_intelligent_ignore_unplugged, where they outlast the plug") + limit = _fetch(my_predbat, 50, 40, plugged=False, ignore_unplugged=True) + failed |= _check("t10 limit", abs(limit - (31.0 + 24.8)) < 0.01, "limit {}".format(limit)) + + print("Test 11: a car-linked target is still a SoC to reach") + my_predbat.octopus_charge_target_base = {} + _sensor(my_predbat, car) + limit = _fetch(my_predbat, 39, 40) + failed |= _check("t11 limit", abs(limit - 24.8) < 0.01, "limit {}".format(limit)) + + print("Test 12: a sensor that does not say is read as a SoC to reach, as before") + _sensor(my_predbat, {"vehicle_battery_size_in_kwh": BATTERY_KWH, "charge_point_power_in_kw": 7.0}) + limit = _fetch(my_predbat, 39, 40) + failed |= _check("t12 limit", abs(limit - 24.8) < 0.01, "limit {}".format(limit)) + return failed + + +def _run_plug_and_owner(my_predbat): + """ + The base SoC is held through a stale low reading, the plug sensor decides plugged where it is set, and a target + Ohme has claimed is its own SoC target. + """ + failed = False + charger = {"vehicle_battery_size_in_kwh": None, "charge_point_power_in_kw": 7.0} + saved_plugged = my_predbat.args.get("car_charging_plugged") + plug = "binary_sensor.test_car_plugged" + try: + print("Test 13: a stale low SoC reading while plugged in does not drag the plug-in SoC down") + my_predbat.octopus_charge_target_base = {} + _sensor(my_predbat, charger) + _fetch(my_predbat, 40, 40, plugged=False) + _fetch(my_predbat, 40, 40) + _fetch(my_predbat, 5, 40) + limit = _fetch(my_predbat, 45, 40) + failed |= _check("t13 limit", abs(limit - (24.8 + 24.8)) < 0.01, "limit {}".format(limit)) + + print("Test 14: the plug sensor keeps the car plugged in when car_charging_planned turns off at the end of a charge") + my_predbat.octopus_charge_target_base = {} + my_predbat.args["car_charging_plugged"] = plug + my_predbat.ha_interface.set_state(plug, "off") + _fetch(my_predbat, 39, 40, plugged=False) + my_predbat.ha_interface.set_state(plug, "on") + _fetch(my_predbat, 39, 40) + limit = _fetch(my_predbat, 79, 40, plugged=False) + failed |= _check("t14 base held", abs(limit - (24.18 + 24.8)) < 0.01, "limit {}".format(limit)) + my_predbat.args.pop("car_charging_plugged", None) + + print("Test 14b: the plug-in SoC is logged once at plug-in, and not on every cycle of a drive") + my_predbat.octopus_charge_target_base = {} + my_predbat.octopus_charge_target_logged = {} + logged = [] + saved_log = my_predbat.log + my_predbat.log = lambda message, *args, **kwargs: logged.append(message) + try: + for soc in (60, 55, 50, 45): + _fetch(my_predbat, soc, 40, plugged=False) + failed |= _check("t14b quiet while unplugged", not [m for m in logged if "when plugged in" in m], "logged {}".format(logged)) + _fetch(my_predbat, 45, 40) + _fetch(my_predbat, 50, 40) + finally: + my_predbat.log = saved_log + plug_in = [m for m in logged if "when plugged in" in m] + failed |= _check("t14b logged once at plug-in", len(plug_in) == 1 and "27.9kWh the car had" in plug_in[0], "logged {}".format(plug_in)) + + print("Test 15: a charge limit Ohme has claimed is its own target, a SoC to reach") + my_predbat.octopus_charge_target_base = {} + my_predbat.car_slot_owner = "ohme" + _fetch(my_predbat, 50, 80, plugged=False) + limit = _fetch(my_predbat, 50, 80) + failed |= _check("t15 not added", abs(limit - 49.6) < 0.01, "limit {}".format(limit)) + finally: + my_predbat.car_slot_owner = None + if saved_plugged is None: + my_predbat.args.pop("car_charging_plugged", None) + else: + my_predbat.args["car_charging_plugged"] = saved_plugged + return failed + + +def test_octopus_charge_target(my_predbat): + """ + The Octopus Intelligent charge target is a percentage to add when the charger is linked (#3034). + """ + print("*** Running test: octopus_charge_target") + saved_state = {field: copy.deepcopy(getattr(my_predbat, field, None)) for field in STATE_FIELDS} + saved_args = {key: copy.deepcopy(my_predbat.args[key]) for key in SAVED_ARGS if key in my_predbat.args} + try: + failed = _run_detection(my_predbat) + failed |= _run_target_entity_detection(my_predbat) + failed |= _run_plug_and_owner(my_predbat) + failed |= _run_limit(my_predbat) + finally: + for field, value in saved_state.items(): + setattr(my_predbat, field, value) + for key in SAVED_ARGS: + if key in saved_args: + my_predbat.args[key] = saved_args[key] + else: + my_predbat.args.pop(key, None) + my_predbat.ha_interface.dummy_items.pop(IOG_SENSOR, None) + print("*** octopus_charge_target test {}".format("FAILED" if failed else "PASSED")) + return failed diff --git a/apps/predbat/unit_test.py b/apps/predbat/unit_test.py index 71fb3dfb7d..820da101b5 100644 --- a/apps/predbat/unit_test.py +++ b/apps/predbat/unit_test.py @@ -224,6 +224,7 @@ from tests.test_fetch_url_cached import test_fetch_url_cached from tests.test_load_free_slot import test_load_free_slot from tests.test_car_charging_now import test_car_charging_now +from tests.test_octopus_charge_target import test_octopus_charge_target from tests.test_octopus_slots_change import test_octopus_slots_change from tests.test_dynamic_load import test_dynamic_load_car_slot_cancellation, test_dynamic_load_high_load_baseline from tests.test_dynamic_load_car import test_dynamic_load_car_not_charging @@ -586,6 +587,7 @@ def main(): ("octopus_dispatch_hold", test_octopus_dispatch_hold, "Octopus dispatch hold tests", False), ("dispatch_billing_window", test_dispatch_billing_window, "Dispatch billing window tests (E.ON)", False), ("car_charging_now", test_car_charging_now, "car_charging_now holds the battery and feeds the model but never adds a car slot", False), + ("octopus_charge_target", test_octopus_charge_target, "A charger-linked Octopus Intelligent target adds to the SoC at plug-in (#3034)", False), ("units", run_test_units, "Unit tests", False), ("manual_api", run_test_manual_api, "Manual API tests", False), ("manual_soc", run_test_manual_soc, "Manual SOC target tests", False), diff --git a/apps/predbat/utils.py b/apps/predbat/utils.py index 739d4af866..c4c8bbebee 100644 --- a/apps/predbat/utils.py +++ b/apps/predbat/utils.py @@ -3249,3 +3249,43 @@ def allocate_export_rates(needs, max_rates, p_fleet): remaining = 0.0 return alloc + + +# What the Octopus Energy integration's device_type attribute names a charger and a car as (BottlecapDave #1917) +INTELLIGENT_DEVICE_TYPE_CHARGER = "ELECTRIC_VEHICLE_CHARGERS" +INTELLIGENT_DEVICE_TYPE_CAR = "ELECTRIC_VEHICLES" + + +def intelligent_device_is_charger(dispatch_attribute, target_attribute=None): + """Is a car's Octopus Intelligent device its charger (True), the car itself (False), or can that not be told (None)? + + dispatch_attribute(name) reads an attribute of the car's dispatch sensor, target_attribute(name) one of its + Intelligent charge target entity, when there is one. Checked in order: + + - is_charger on the dispatch sensor, published by Predbat's own Octopus and Kraken components. + - device_type, which the Octopus Energy integration puts on the charge target entity (and is read from the + dispatch sensor too). + - Failing both, which size the integration's dispatch sensor fills in: only the charge point's power for a + charger, only the battery size for a car. + """ + is_charger = dispatch_attribute("is_charger") + if isinstance(is_charger, (bool, int, float)): + return bool(is_charger) + if isinstance(is_charger, str) and is_charger.strip().lower() not in ("", "unknown", "unavailable", "none"): + # The strings get_arg() and the charger control read as true + return is_charger.strip().lower() in ("on", "true", "yes", "enabled", "enable", "connected") + for read in (target_attribute, dispatch_attribute): + if read is None: + continue + device_type = read("device_type") + if device_type == INTELLIGENT_DEVICE_TYPE_CHARGER: + return True + if device_type == INTELLIGENT_DEVICE_TYPE_CAR: + return False + size = dispatch_attribute("vehicle_battery_size_in_kwh") + rate = dispatch_attribute("charge_point_power_in_kw") + if rate is not None and size is None: + return True + if size is not None and rate is None: + return False + return None diff --git a/docs/car-charging.md b/docs/car-charging.md index 874830e30a..d4d42edaec 100644 --- a/docs/car-charging.md +++ b/docs/car-charging.md @@ -152,6 +152,9 @@ For **multiple IOG-enrolled vehicles**, set this to a list with one sensor per c - **octopus_charge_limit** - Points to the Octopus Energy integration sensor that provides the car charging limit you want the car to charge to. For **multiple IOG-enrolled vehicles**, set this to a list with one sensor per car. +When Octopus is linked to your charger rather than your car, Octopus's target is how much charge to add, not the level to charge to. +Predbat detects this from the Octopus dispatch sensor (from Predbat's own Octopus component, or from which of the charge point power and the battery size the Octopus Energy integration fills in), and adds the target to the car's SoC when it was plugged in, still capped by **car_charging_limit**. It also reads a `device_type` attribute on the charge target entity you set as **octopus_charge_limit**, should the integration add one. +If neither says which is linked (for example a charger model Octopus does not list), the target is read as the level to charge to. If you are using Octopus-led charging with the [Octopus direct connection](energy-rates.md#octopus-energy-direct) method: