Skip to content

Commit

Permalink
fix: Battery life cycles calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Jul 18, 2024
1 parent 6d3367d commit 1f24340
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ parameters:
range:
min: 0
max: 2000
attributes: ["Battery Corrected Capacity"]

- name: "Battery Empty"
class: "voltage"
Expand Down Expand Up @@ -1000,7 +999,7 @@ parameters:
validation:
min: 0
max: 101
attributes: ["Battery Control Mode"]
attributes: ["Battery Control Mode", "Battery Corrected Capacity"]

# Battery - The power of battery is S16bit
- name: "Battery Power"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ parameters:
range:
min: 0
max: 2000
attributes: ["Battery Corrected Capacity"]

- name: "Battery Empty"
class: "voltage"
Expand Down Expand Up @@ -1006,7 +1005,7 @@ parameters:
validation:
min: 0
max: 101
attributes: ["Battery Control Mode"]
attributes: ["Battery Control Mode", "Battery Corrected Capacity"]

# Battery - The power of battery is S16bit
- name: "Battery Power"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ parameters:
range:
min: 0
max: 2000
attributes: ["Battery Corrected Capacity"]

- name: "Battery Empty"
class: "voltage"
Expand Down Expand Up @@ -1007,7 +1006,7 @@ parameters:
validation:
min: 0
max: 101
attributes: ["Battery Control Mode"]
attributes: ["Battery Control Mode", "Battery Corrected Capacity"]

# Battery - The power of battery is S16bit
- name: "Battery Power"
Expand Down
12 changes: 6 additions & 6 deletions custom_components/solarman/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,24 @@ def update(self):
if c > 1 or (c == 1 and self.sensor_name in self.coordinator.data):
match self.sensor_name:
case "Battery SOH":
battery_capacity = self.get_data("Battery Capacity", None)
total_battery_charge = self.get_data("Total Battery Charge", None)
today_battery_charge = self.get_data("Today Battery Charge", None)
battery_capacity = self.get_data("Battery Corrected Capacity", None)
if total_battery_charge and battery_capacity and self._battery_nominal_voltage and self._battery_life_cycle_rating:
self._attr_state = get_number(100 - total_battery_charge / get_battery_power_capacity(battery_capacity, self._battery_nominal_voltage) / (self._battery_life_cycle_rating * 0.05), self._digits)
case "Battery State":
battery_power = self.get_data("Battery Power", None)
if battery_power:
self._attr_state = "discharging" if battery_power > 50 else "charging" if battery_power < -50 else "standby"
case "Today Battery Life Cycles":
battery_capacity = self.get_data("Battery Capacity", None)
total_battery_charge = self.get_data("Total Battery Charge", None)
today_battery_charge = self.get_data("Today Battery Charge", None)
if today_battery_charge == 0:
self._attr_state = get_number(0, self._digits)
return
battery_capacity = self.get_data("Battery Corrected Capacity", None)
if today_battery_charge and battery_capacity and self._battery_nominal_voltage:
self._attr_state = get_number(get_battery_cycles(today_battery_charge, battery_capacity, self._battery_nominal_voltage), self._digits)
case "Total Battery Life Cycles":
battery_capacity = self.get_data("Battery Capacity", None)
total_battery_charge = self.get_data("Total Battery Charge", None)
today_battery_charge = self.get_data("Today Battery Charge", None)
battery_capacity = self.get_data("Battery Corrected Capacity", None)
if total_battery_charge and battery_capacity and self._battery_nominal_voltage:
self._attr_state = get_number(get_battery_cycles(total_battery_charge, battery_capacity, self._battery_nominal_voltage), self._digits)

0 comments on commit 1f24340

Please sign in to comment.