Skip to content

Commit 2a240fe

Browse files
Update battery pool SoC calculation
Fix the SoC calculation such that it never exceeds 100%. Signed-off-by: Matthias Wende <[email protected]>
1 parent 3eae96b commit 2a240fe

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/frequenz/sdk/timeseries/battery_pool/_metric_calculator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,8 @@ def calculate(
424424
soc_scaled = (
425425
(soc - soc_lower_bound) / (soc_upper_bound - soc_lower_bound) * 100
426426
)
427-
soc_scaled = max(soc_scaled, 0)
427+
# we are clamping here because the SoC might be out of bounds
428+
soc_scaled = min(max(soc_scaled, 0), 100)
428429
timestamp = max(timestamp, metrics.timestamp)
429430
used_capacity_x100 += usable_capacity_x100 * soc_scaled
430431
total_capacity_x100 += usable_capacity_x100

src/frequenz/sdk/timeseries/battery_pool/battery_pool.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,16 +347,17 @@ def consumption_power(self) -> FormulaEngine[Power]:
347347
def soc(self) -> MetricAggregator[Sample[Percentage]]:
348348
"""Fetch the normalized average weighted-by-capacity SoC values for the pool.
349349
350-
The values are normalized to the 0-100% range.
350+
The values are normalized to the 0-100% range and clamped if the SoC is out of
351+
bounds.
351352
352353
Average soc is calculated with the formula:
353354
```
354355
working_batteries: Set[BatteryData] # working batteries from the battery pool
355356
356-
soc_scaled = max(
357+
soc_scaled = min(max(
357358
0,
358359
(soc - soc_lower_bound) / (soc_upper_bound - soc_lower_bound) * 100,
359-
)
360+
), 100)
360361
used_capacity = sum(
361362
battery.usable_capacity * battery.soc_scaled
362363
for battery in working_batteries

0 commit comments

Comments
 (0)