Skip to content

Commit d6ae10c

Browse files
authored
Fix log when multiple batteries are attached to an inverter (#1262)
Without this, when 10 kW is distributed to 3 batteries, 2 of them attached to a single inverter, the debug log displayed the same power for the two batteries, totalling to higher than what was requested, which is misleading (formatted for readability): Distributing power 10 kW between the batteries { ComponentId(1005): Power(value=6720.751321413104, exponent=0), ComponentId(1004): Power(value=6720.751321413104, exponent=0), ComponentId(1001): Power(value=3279.248678586895, exponent=0) } With this change, the log looks like this: Distributing power 10 kW between the batteries: (CID1004, CID1005): 6.72 kW, CID1001: 3.28 kW Also improved the formatting.
2 parents f546654 + 6ad1cd8 commit d6ae10c

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

RELEASE_NOTES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@
3030
## Bug Fixes
3131

3232
- When using the new wall clock timer in the resampmler, it will now resync to the system time if it drifts away for more than a resample period, and do dynamic adjustments to the timer if the monotonic clock has a small drift compared to the wall clock.
33+
34+
- A power distributor logging issue is fixed, that was causing the power for multiple batteries connected to the same inverter to be reported incorrectly.

src/frequenz/sdk/microgrid/_power_distributing/_component_managers/_battery_manager.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,25 +266,34 @@ async def _distribute_power(
266266
Result from the microgrid API.
267267
"""
268268
distributed_power_value = request.power - distribution.remaining_power
269-
battery_distribution: dict[ComponentId, Power] = {}
269+
battery_distribution: dict[frozenset[ComponentId], Power] = {}
270+
battery_ids: set[ComponentId] = set()
270271
for inverter_id, dist in distribution.distribution.items():
271272
for battery_id in self._inv_bats_map[inverter_id]:
272-
battery_distribution[battery_id] = (
273-
battery_distribution.get(battery_id, Power.zero()) + dist
274-
)
275-
_logger.debug(
276-
"Distributing power %s between the batteries %s",
277-
distributed_power_value,
278-
str(battery_distribution),
279-
)
273+
battery_ids.add(battery_id)
274+
battery_distribution[self._inv_bats_map[inverter_id]] = dist
275+
if _logger.isEnabledFor(logging.DEBUG):
276+
_logger.debug(
277+
"Distributing power %s between the batteries: %s",
278+
distributed_power_value,
279+
", ".join(
280+
(
281+
str(next(iter(cids)))
282+
if len(cids) == 1
283+
else f"({', '.join(str(cid) for cid in cids)})"
284+
)
285+
+ f": {power}"
286+
for cids, power in battery_distribution.items()
287+
),
288+
)
280289

281290
failed_power, failed_batteries = await self._set_distributed_power(
282291
distribution, self._api_power_request_timeout
283292
)
284293

285294
response: Success | PartialFailure
286295
if len(failed_batteries) > 0:
287-
succeed_batteries = set(battery_distribution.keys()) - failed_batteries
296+
succeed_batteries = battery_ids - failed_batteries
288297
response = PartialFailure(
289298
request=request,
290299
succeeded_power=distributed_power_value - failed_power,
@@ -294,7 +303,7 @@ async def _distribute_power(
294303
excess_power=distribution.remaining_power,
295304
)
296305
else:
297-
succeed_batteries = set(battery_distribution.keys())
306+
succeed_batteries = battery_ids
298307
response = Success(
299308
request=request,
300309
succeeded_power=distributed_power_value,

0 commit comments

Comments
 (0)