From db2c783977b39cbd4c460d7791122897f8cd5a36 Mon Sep 17 00:00:00 2001 From: LKuemmel Date: Mon, 12 May 2025 15:52:21 +0200 Subject: [PATCH 1/2] Add currents for bat --- packages/helpermodules/setdata.py | 5 +++-- packages/modules/common/component_state.py | 7 +++++++ packages/modules/common/store/_battery.py | 1 + packages/modules/devices/generic/http/bat.py | 3 +++ packages/modules/devices/generic/http/config.py | 3 ++- packages/modules/devices/generic/json/bat.py | 2 ++ packages/modules/devices/generic/json/config.py | 2 ++ packages/modules/devices/generic/mqtt/bat.py | 2 ++ packages/modules/devices/openwb/openwb_flex/bat.py | 10 ++++++++++ 9 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/helpermodules/setdata.py b/packages/helpermodules/setdata.py index c66aefdc71..adaaa6062b 100644 --- a/packages/helpermodules/setdata.py +++ b/packages/helpermodules/setdata.py @@ -693,8 +693,7 @@ def process_pv_topic(self, msg: mqtt.MQTTMessage): elif "/get/power" in msg.topic: self._validate_value(msg, float) elif "/get/currents" in msg.topic: - self._validate_value( - msg, float, collection=list) + self._validate_value(msg, float, collection=list) else: self.__unknown_topic(msg) else: @@ -743,6 +742,8 @@ def process_bat_topic(self, msg: mqtt.MQTTMessage): "/get/daily_exported" in msg.topic or "/get/daily_imported" in msg.topic): self._validate_value(msg, float, [(0, float("inf"))]) + elif "/get/currents" in msg.topic: + self._validate_value(msg, float, collection=list) elif "/get/soc" in msg.topic: self._validate_value(msg, float, [(0, 100)]) elif "/get/fault_state" in msg.topic: diff --git a/packages/modules/common/component_state.py b/packages/modules/common/component_state.py index 97dc9f9cf9..437c02a68a 100644 --- a/packages/modules/common/component_state.py +++ b/packages/modules/common/component_state.py @@ -56,6 +56,7 @@ def __init__( exported: float = 0, power: float = 0, soc: float = 0, + currents: Optional[List[float]] = None, ): """Args: imported: total imported energy in Wh @@ -67,6 +68,12 @@ def __init__( self.exported = exported self.power = power self.soc = soc + if _check_none(currents): + currents = [0.0]*3 + else: + if not ((sum(currents) < 0 and power < 0) or (sum(currents) > 0 and power > 0)): + log.debug("currents sign wrong "+str(currents)) + self.currents = currents @auto_str diff --git a/packages/modules/common/store/_battery.py b/packages/modules/common/store/_battery.py index efb4c59ab3..7ca0e9d1a9 100644 --- a/packages/modules/common/store/_battery.py +++ b/packages/modules/common/store/_battery.py @@ -30,6 +30,7 @@ def set(self, bat_state: BatState): def update(self): try: + pub_to_broker("openWB/set/bat/"+str(self.num)+"/get/currents", self.state.currents, 2) pub_to_broker("openWB/set/bat/"+str(self.num)+"/get/power", self.state.power, 2) pub_to_broker("openWB/set/bat/"+str(self.num)+"/get/soc", self.state.soc, 0) pub_to_broker("openWB/set/bat/"+str(self.num)+"/get/imported", self.state.imported, 2) diff --git a/packages/modules/devices/generic/http/bat.py b/packages/modules/devices/generic/http/bat.py index 7835bc1f94..9e19fc3723 100644 --- a/packages/modules/devices/generic/http/bat.py +++ b/packages/modules/devices/generic/http/bat.py @@ -29,6 +29,8 @@ def initialize(self) -> None: self.store = get_bat_value_store(self.component_config.id) self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config)) + self.__get_currents = create_request_function( + self.kwargs['url'], self.component_config.configuration.currents_path) self.__get_power = create_request_function(self.kwargs['url'], self.component_config.configuration.power_path) self.__get_imported = create_request_function( self.kwargs['url'], self.component_config.configuration.imported_path) @@ -44,6 +46,7 @@ def update(self, session: Session) -> None: imported, exported = self.sim_counter.sim_count(power) bat_state = BatState( + currents=self.__get_currents(session), power=power, exported=exported, imported=imported, diff --git a/packages/modules/devices/generic/http/config.py b/packages/modules/devices/generic/http/config.py index f2d6bcfa7d..2c413bd701 100644 --- a/packages/modules/devices/generic/http/config.py +++ b/packages/modules/devices/generic/http/config.py @@ -25,9 +25,10 @@ def __init__(self, @auto_str class HttpBatConfiguration: - def __init__(self, power_path=None, soc_path=None, imported_path=None, exported_path=None): + def __init__(self, power_path=None, soc_path=None, currents_path=None, imported_path=None, exported_path=None): self.power_path = power_path self.soc_path = soc_path + self.currents_path = currents_path self.imported_path = imported_path self.exported_path = exported_path diff --git a/packages/modules/devices/generic/json/bat.py b/packages/modules/devices/generic/json/bat.py index 191cddc157..bc786cf7b5 100644 --- a/packages/modules/devices/generic/json/bat.py +++ b/packages/modules/devices/generic/json/bat.py @@ -29,6 +29,7 @@ def initialize(self) -> None: def update(self, response) -> None: config = self.component_config.configuration + currents = float(jq.compile(config.jq_currents).input(response).first()) power = float(jq.compile(config.jq_power).input(response).first()) if config.jq_soc != "": soc = float(jq.compile(config.jq_soc).input(response).first()) @@ -42,6 +43,7 @@ def update(self, response) -> None: imported, exported = self.sim_counter.sim_count(power) bat_state = BatState( + currents=currents, power=power, soc=soc, imported=imported, diff --git a/packages/modules/devices/generic/json/config.py b/packages/modules/devices/generic/json/config.py index fc714bbc84..8af1f1ba3d 100644 --- a/packages/modules/devices/generic/json/config.py +++ b/packages/modules/devices/generic/json/config.py @@ -24,10 +24,12 @@ def __init__(self, class JsonBatConfiguration: def __init__(self, + jq_currents: Optional[str] = None, jq_imported: Optional[str] = None, jq_exported: Optional[str] = None, jq_soc: str = "", jq_power: str = ""): + jq_currents = jq_currents self.jq_imported = jq_imported self.jq_exported = jq_exported self.jq_soc = jq_soc diff --git a/packages/modules/devices/generic/mqtt/bat.py b/packages/modules/devices/generic/mqtt/bat.py index f1d83110e5..45e08a83fd 100644 --- a/packages/modules/devices/generic/mqtt/bat.py +++ b/packages/modules/devices/generic/mqtt/bat.py @@ -26,6 +26,7 @@ def initialize(self) -> None: self.store = get_bat_value_store(self.component_config.id) def update(self, received_topics: Dict) -> None: + currents = received_topics.get(f"openWB/mqtt/bat/{self.component_config.id}/get/currents") power = received_topics.get(f"openWB/mqtt/bat/{self.component_config.id}/get/power") soc = received_topics.get(f"openWB/mqtt/bat/{self.component_config.id}/get/soc") if (received_topics.get(f"openWB/mqtt/bat/{self.component_config.id}/get/imported") and @@ -36,6 +37,7 @@ def update(self, received_topics: Dict) -> None: imported, exported = self.sim_counter.sim_count(power) bat_state = BatState( + currents=currents, power=power, soc=soc, imported=imported, diff --git a/packages/modules/devices/openwb/openwb_flex/bat.py b/packages/modules/devices/openwb/openwb_flex/bat.py index 78d7dc220b..494af8bf12 100644 --- a/packages/modules/devices/openwb/openwb_flex/bat.py +++ b/packages/modules/devices/openwb/openwb_flex/bat.py @@ -7,6 +7,7 @@ from modules.common.component_type import ComponentDescriptor from modules.common.fault_state import ComponentInfo, FaultState from modules.common.lovato import Lovato +from modules.common.mpm3pm import Mpm3pm from modules.common.sdm import Sdm120 from modules.common.sdm import Sdm630_72 from modules.common.simcount import SimCounter @@ -49,7 +50,16 @@ def update(self): imported = self.__client.get_imported() exported = self.__client.get_exported() + voltages = self.__client.get_voltages() + powers, power = self.__client.get_power() + + if isinstance(self.__client, Mpm3pm): + currents = [powers[i] / voltages[i] for i in range(3)] + else: + currents = self.__client.get_currents() + bat_state = BatState( + currents=currents, imported=imported, exported=exported, power=power From fb078b6358fb639416b360afc7b275c124ef41c2 Mon Sep 17 00:00:00 2001 From: LKuemmel Date: Tue, 13 May 2025 10:35:01 +0200 Subject: [PATCH 2/2] separate paths for currents --- packages/modules/devices/generic/http/bat.py | 9 ++++++--- packages/modules/devices/generic/http/config.py | 12 ++++++++++-- packages/modules/devices/generic/json/bat.py | 6 +++++- packages/modules/devices/generic/json/config.py | 6 ++++-- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/modules/devices/generic/http/bat.py b/packages/modules/devices/generic/http/bat.py index 9e19fc3723..d42c8060cf 100644 --- a/packages/modules/devices/generic/http/bat.py +++ b/packages/modules/devices/generic/http/bat.py @@ -9,7 +9,7 @@ from modules.common.fault_state import ComponentInfo, FaultState from modules.common.simcount import SimCounter from modules.common.store import get_bat_value_store -from modules.devices.generic.http.api import create_request_function +from modules.devices.generic.http.api import create_request_function, create_request_function_array from modules.devices.generic.http.config import HttpBatSetup @@ -29,8 +29,11 @@ def initialize(self) -> None: self.store = get_bat_value_store(self.component_config.id) self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config)) - self.__get_currents = create_request_function( - self.kwargs['url'], self.component_config.configuration.currents_path) + self.__get_currents = create_request_function_array(self.kwargs['url'], [ + self.component_config.configuration.current_l1_path, + self.component_config.configuration.current_l2_path, + self.component_config.configuration.current_l3_path, + ]) self.__get_power = create_request_function(self.kwargs['url'], self.component_config.configuration.power_path) self.__get_imported = create_request_function( self.kwargs['url'], self.component_config.configuration.imported_path) diff --git a/packages/modules/devices/generic/http/config.py b/packages/modules/devices/generic/http/config.py index 2c413bd701..f40fcee1e9 100644 --- a/packages/modules/devices/generic/http/config.py +++ b/packages/modules/devices/generic/http/config.py @@ -25,10 +25,18 @@ def __init__(self, @auto_str class HttpBatConfiguration: - def __init__(self, power_path=None, soc_path=None, currents_path=None, imported_path=None, exported_path=None): + def __init__(self, power_path=None, + soc_path=None, + current_l1_path=None, + current_l2_path=None, + current_l3_path=None, + imported_path=None, + exported_path=None): self.power_path = power_path self.soc_path = soc_path - self.currents_path = currents_path + self.current_l1_path = current_l1_path + self.current_l2_path = current_l2_path + self.current_l3_path = current_l3_path self.imported_path = imported_path self.exported_path = exported_path diff --git a/packages/modules/devices/generic/json/bat.py b/packages/modules/devices/generic/json/bat.py index bc786cf7b5..4b2cef9477 100644 --- a/packages/modules/devices/generic/json/bat.py +++ b/packages/modules/devices/generic/json/bat.py @@ -29,7 +29,11 @@ def initialize(self) -> None: def update(self, response) -> None: config = self.component_config.configuration - currents = float(jq.compile(config.jq_currents).input(response).first()) + currents = [0] * 3 + for i, c in enumerate(config.jq_currents): + if c is not None: + currents[i] = float(jq.compile(c).input(response).first()) + power = float(jq.compile(config.jq_power).input(response).first()) if config.jq_soc != "": soc = float(jq.compile(config.jq_soc).input(response).first()) diff --git a/packages/modules/devices/generic/json/config.py b/packages/modules/devices/generic/json/config.py index 8af1f1ba3d..430dc0ded8 100644 --- a/packages/modules/devices/generic/json/config.py +++ b/packages/modules/devices/generic/json/config.py @@ -24,12 +24,14 @@ def __init__(self, class JsonBatConfiguration: def __init__(self, - jq_currents: Optional[str] = None, + jq_current_l1: Optional[str] = None, + jq_current_l2: Optional[str] = None, + jq_current_l3: Optional[str] = None, jq_imported: Optional[str] = None, jq_exported: Optional[str] = None, jq_soc: str = "", jq_power: str = ""): - jq_currents = jq_currents + self.jq_currents = (jq_current_l1, jq_current_l2, jq_current_l3) self.jq_imported = jq_imported self.jq_exported = jq_exported self.jq_soc = jq_soc