Skip to content

Commit

Permalink
Calculated SOC: Added two decimals, added BMS SOC for MQTT & Node-RED
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-manuel committed Dec 23, 2023
1 parent 2f8a98d commit 354e1c0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions etc/dbus-serialbattery/battery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 15 additions & 7 deletions etc/dbus-serialbattery/dbushelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "",
}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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,
],
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 354e1c0

Please sign in to comment.