From 354e1c0b088409712abebed1058642e0e77f2ef2 Mon Sep 17 00:00:00 2001 From: Manuel Date: Sun, 24 Dec 2023 00:43:04 +0100 Subject: [PATCH] Calculated SOC: Added two decimals, added BMS SOC for MQTT & Node-RED --- etc/dbus-serialbattery/battery.py | 4 ++-- etc/dbus-serialbattery/dbushelper.py | 22 +++++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/etc/dbus-serialbattery/battery.py b/etc/dbus-serialbattery/battery.py index a7a00bab..8c871261 100644 --- a/etc/dbus-serialbattery/battery.py +++ b/etc/dbus-serialbattery/battery.py @@ -306,8 +306,8 @@ def soc_calculation(self) -> None: self.soc_calc_capacity_remain_lasttime = current_time # Calculate the SOC based on remaining capacity - self.soc_calc = max( - min((self.soc_calc_capacity_remain / self.capacity) * 100, 100), 0 + self.soc_calc = round( + max(min((self.soc_calc_capacity_remain / self.capacity) * 100, 100), 0), 2 ) def prepare_voltage_management(self) -> None: diff --git a/etc/dbus-serialbattery/dbushelper.py b/etc/dbus-serialbattery/dbushelper.py index 4af3eccf..0636c7e1 100644 --- a/etc/dbus-serialbattery/dbushelper.py +++ b/etc/dbus-serialbattery/dbushelper.py @@ -56,7 +56,7 @@ def __init__(self, battery): "allow_max_voltage": self.battery.allow_max_voltage, "max_voltage_start_time": self.battery.max_voltage_start_time, "soc_reset_last_reached": self.battery.soc_reset_last_reached, - "soc_calc": int(self.battery.soc_calc) + "soc_calc": self.battery.soc_calc if self.battery.soc_calc is not None else "", } @@ -180,7 +180,7 @@ def setup_instance(self): if "SocCalc" in value: self.battery.soc_calc = float(value["SocCalc"]) logger.info( - f"Soc_calc read from dbus: {int(self.battery.soc_calc)}" + f"Soc_calc read from dbus: {self.battery.soc_calc}" ) else: logger.info("Soc_calc not found in dbus") @@ -288,7 +288,7 @@ def setup_instance(self): ], "SocCalc": [ self.path_battery + "/SocCalc", - int(self.battery.soc_calc) if self.battery.soc_calc is not None else "", + self.battery.soc_calc if self.battery.soc_calc is not None else "", 0, 0, ], @@ -442,6 +442,10 @@ def setup_vedbus(self): # Create SOC, DC and System items self._dbusservice.add_path("/Soc", None, writeable=True) + # add original SOC for comparing + if utils.SOC_CALCULATION: + self._dbusservice.add_path("/SocBms", None, writeable=True) + self._dbusservice.add_path( "/Dc/0/Voltage", None, @@ -691,6 +695,10 @@ def publish_dbus(self): if self.battery.soc_calc is not None else None ) + # add original SOC for comparing + self._dbusservice["/SocBms"] = ( + round(self.battery.soc, 2) if self.battery.soc is not None else None + ) else: self._dbusservice["/Soc"] = ( round(self.battery.soc, 2) if self.battery.soc is not None else None @@ -1125,16 +1133,16 @@ def saveBatteryOptions(self) -> bool: + f"after {self.battery.max_voltage_start_time}" ) - if int(self.battery.soc_calc) != self.save_charge_details_last["soc_calc"]: - self.save_charge_details_last["soc_calc"] = int(self.battery.soc_calc) + if self.battery.soc_calc != self.save_charge_details_last["soc_calc"]: + self.save_charge_details_last["soc_calc"] = self.battery.soc_calc result = result and self.setSetting( get_bus(), "com.victronenergy.settings", self.path_battery, "SocCalc", - int(self.battery.soc_calc), + self.battery.soc_calc, ) - logger.debug(f"soc_calc written to dbus: {int(self.battery.soc_calc)}") + logger.debug(f"soc_calc written to dbus: {self.battery.soc_calc}") if ( self.battery.soc_reset_last_reached