Skip to content

Commit ddca86b

Browse files
committed
Fix log when multiple batteries are attached to an inverter
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. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 16887b1 commit ddca86b

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,19 +266,26 @@ 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] = {}
270270
battery_ids: set[ComponentId] = set()
271271
for inverter_id, dist in distribution.distribution.items():
272272
for battery_id in self._inv_bats_map[inverter_id]:
273273
battery_ids.add(battery_id)
274-
battery_distribution[battery_id] = (
275-
battery_distribution.get(battery_id, Power.zero()) + dist
276-
)
277-
_logger.debug(
278-
"Distributing power %s between the batteries %s",
279-
distributed_power_value,
280-
str(battery_distribution),
281-
)
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+
)
282289

283290
failed_power, failed_batteries = await self._set_distributed_power(
284291
distribution, self._api_power_request_timeout

0 commit comments

Comments
 (0)