From af4ec01dc718bb072894465472df0fb4d1eb6b5f Mon Sep 17 00:00:00 2001 From: Manuel Date: Sun, 23 Jul 2023 21:45:48 +0200 Subject: [PATCH] added current average of last 5 minutes --- CHANGELOG.md | 4 +- etc/dbus-serialbattery/battery.py | 8 +-- etc/dbus-serialbattery/dbushelper.py | 61 ++++++++++++++++------ etc/dbus-serialbattery/qml/PageBattery.qml | 6 +++ etc/dbus-serialbattery/utils.py | 2 +- 5 files changed, 61 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4e73afc..1762738d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## v1.0.x * Added: Bluetooth: Show signal strength of BMS in log by @mr-manuel * Added: Create unique identifier, if not provided from BMS by @mr-manuel +* Added: Current average of the last 5 minutes by @mr-manuel * Added: Daly BMS: Auto reset SoC when changing to float (can be turned off in the config file) by @transistorgit * Added: Exclude a device from beeing used by the dbus-serialbattery driver by @mr-manuel * Added: Implement callback function for update by @seidler2547 @@ -12,7 +13,6 @@ * Added: Temperature names to dbus and mqtt by @mr-manuel * Added: Use current average of the last 300 cycles for time to go and time to SoC calculation by @mr-manuel * Added: Validate current, voltage, capacity and SoC for all BMS. This prevents that a device, which is no BMS, is detected as BMS. Fixes also https://github.com/Louisvdw/dbus-serialbattery/issues/479 by @mr-manuel -* Changed: Calculate only positive Time-to-SoC points by @mr-manuel * Changed: Enable BMS that are disabled by default by specifying it in the config file. No more need to edit scripts by @mr-manuel * Changed: Fix daly readsentence by @transistorgit * Changed: Fix Sinowealth not loading https://github.com/Louisvdw/dbus-serialbattery/issues/702 by @mr-manuel @@ -25,6 +25,8 @@ * Changed: Improved driver reinstall when multiple Bluetooth BMS are enabled by @mr-manuel * Changed: Improved Jkbms_Ble driver by @seidler2547 & @mr-manuel * Changed: Reduce the big inrush current if the CVL jumps from Bulk/Absorbtion to Float https://github.com/Louisvdw/dbus-serialbattery/issues/659 by @Rikkert-RS & @ogurevich +* Changed: Time-to-Go and Time-to-SoC use the current average of the last 5 minutes for calculation by @mr-manuel +* Changed: Time-to-SoC calculate only positive points by @mr-manuel * Removed: Cronjob to restart Bluetooth service every 12 hours by @mr-manuel diff --git a/etc/dbus-serialbattery/battery.py b/etc/dbus-serialbattery/battery.py index a89a4744..b6111786 100644 --- a/etc/dbus-serialbattery/battery.py +++ b/etc/dbus-serialbattery/battery.py @@ -260,10 +260,12 @@ def prepare_voltage_management(self) -> None: or utils.BULK_AFTER_DAYS < bulk_last_reached_days_ago ) ): + """ logger.info( f"set bulk_requested to True: first time (0) or {utils.BULK_AFTER_DAYS}" + f" < {round(bulk_last_reached_days_ago, 2)}" ) + """ self.bulk_requested = True self.bulk_battery_voltage = round(utils.BULK_CELL_VOLTAGE * self.cell_count, 2) @@ -396,7 +398,7 @@ def manage_charge_voltage_linear(self) -> None: chargeMode = "Float" # reset bulk when going into float if self.bulk_requested: - logger.info("set bulk_requested to False") + # logger.info("set bulk_requested to False") self.bulk_requested = False # IDEA: Save "bulk_last_reached" in the dbus path com.victronenergy.settings # to make it restart persistent @@ -436,7 +438,7 @@ def manage_charge_voltage_linear(self) -> None: self.charge_mode += " (Linear Mode)" # uncomment for enabling debugging infos in GUI - # """ + """ self.charge_mode_debug = ( f"max_battery_voltage: {round(self.max_battery_voltage, 2)}V" ) @@ -550,7 +552,7 @@ def manage_charge_voltage_step(self) -> None: self.charge_mode = "Float" # reset bulk when going into float if self.bulk_requested: - logger.info("set bulk_requested to False") + # logger.info("set bulk_requested to False") self.bulk_requested = False self.bulk_last_reached = current_time diff --git a/etc/dbus-serialbattery/dbushelper.py b/etc/dbus-serialbattery/dbushelper.py index a564141e..3266665f 100644 --- a/etc/dbus-serialbattery/dbushelper.py +++ b/etc/dbus-serialbattery/dbushelper.py @@ -315,6 +315,12 @@ def setup_vedbus(self): # Create TimeToGo item if utils.TIME_TO_GO_ENABLE: self._dbusservice.add_path("/TimeToGo", None, writeable=True) + self._dbusservice.add_path( + "/CurrentAvg", + None, + writeable=True, + gettextcallback=lambda p, v: "{:0.2f}A".format(v), + ) # Create TimeToSoc items if len(utils.TIME_TO_SOC_POINTS) > 0: @@ -592,6 +598,20 @@ def publish_dbus(self): if len(self.battery.current_avg_lst) > 300: del self.battery.current_avg_lst[0] + """ + logger.info( + str(self.battery.capacity) + + " - " + + str(utils.TIME_TO_GO_ENABLE) + + " - " + + str(len(utils.TIME_TO_SOC_POINTS)) + + " - " + + str(int(time()) - self.battery.time_to_soc_update) + + " - " + + str(utils.TIME_TO_SOC_RECALCULATE_EVERY) + ) + """ + if ( self.battery.capacity is not None and (utils.TIME_TO_GO_ENABLE or len(utils.TIME_TO_SOC_POINTS) > 0) @@ -608,30 +628,35 @@ def publish_dbus(self): 2, ) + self._dbusservice["/CurrentAvg"] = self.battery.current_avg + crntPrctPerSec = ( abs(self.battery.current_avg / (self.battery.capacity / 100)) / 3600 ) # Update TimeToGo item if utils.TIME_TO_GO_ENABLE: - # Update TimeToGo item, has to be a positive int since it's used from dbus-systemcalc-py - self._dbusservice["/TimeToGo"] = ( - abs( - int( - self.battery.get_timeToSoc( - # switch value depending on charging/discharging - utils.SOC_LOW_WARNING - if self.battery.current_avg < 0 - else 100, - crntPrctPerSec, - True, + if self.battery.current_avg is not None: + # Update TimeToGo item, has to be a positive int since it's used from dbus-systemcalc-py + self._dbusservice["/TimeToGo"] = ( + abs( + int( + self.battery.get_timeToSoc( + # switch value depending on charging/discharging + utils.SOC_LOW_WARNING + if self.battery.current_avg < 0 + else 100, + crntPrctPerSec, + True, + ) ) ) + if self.battery.current_avg + and abs(self.battery.current_avg) > 0.1 + else None ) - if self.battery.current_avg - and abs(self.battery.current_avg) > 0.1 - else None - ) + else: + self._dbusservice["/TimeToGo"] = None # Update TimeToSoc items if len(utils.TIME_TO_SOC_POINTS) > 0: @@ -643,6 +668,12 @@ def publish_dbus(self): ) except Exception: + exception_type, exception_object, exception_traceback = sys.exc_info() + file = exception_traceback.tb_frame.f_code.co_filename + line = exception_traceback.tb_lineno + logger.error( + f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}" + ) pass if self.battery.soc is not None: diff --git a/etc/dbus-serialbattery/qml/PageBattery.qml b/etc/dbus-serialbattery/qml/PageBattery.qml index 286ce74c..ea7b44f5 100644 --- a/etc/dbus-serialbattery/qml/PageBattery.qml +++ b/etc/dbus-serialbattery/qml/PageBattery.qml @@ -94,6 +94,12 @@ MbPage { ] } + MbItemValue { + description: qsTr("Current (last 5 minutes avg.)") + item.bind: service.path("/CurrentAvg") + show: item.seen + } + MbItemValue { id: soc diff --git a/etc/dbus-serialbattery/utils.py b/etc/dbus-serialbattery/utils.py index 73bab00a..081ee930 100644 --- a/etc/dbus-serialbattery/utils.py +++ b/etc/dbus-serialbattery/utils.py @@ -38,7 +38,7 @@ def _get_list_from_config( # Constants - Need to dynamically get them in future -DRIVER_VERSION = "1.0.20230717dev" +DRIVER_VERSION = "1.0.20230723dev" zero_char = chr(48) degree_sign = "\N{DEGREE SIGN}"