Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Include the power of non-working components in pool bounds - #1477

Merged
shsms merged 11 commits into
frequenz-floss:v1.x.xfrom
shsms:pool-bounds-unreachable-power
Nov 2, 2026
Merged

shsms merged 11 commits into
frequenz-floss:v1.x.xfrom
shsms:pool-bounds-unreachable-power

Conversation

@shsms

@shsms shsms commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

When some batteries stop working (for example, a set-power call fails and the battery is blocked), the battery pool's bounds cover only the working batteries. The power manager clamps the target to those bounds. Then the power distributor subtracts the power still measured on the non-working batteries from the target, so the target was cut twice. With many batteries blocked, the inverters stayed near 0 W.

This branch shifts the inclusion bounds of the battery, PV and steam boiler pools by the power measured on their components that aren't working.

Changes

  • A new UnreachablePowerTracker measures that power with the component graph formula, so it gets the same meter fallback as the distributor. It restarts its formula when the set of non-working components changes. The distributor's component managers now use it instead of their own code.
  • In the distributor, a non-working battery that shares an inverter with a working battery no longer counts as unreachable. The component graph can't make a formula for only some of the batteries behind an inverter, so such requests got no result.
  • The PV and steam boiler bounds trackers start tracking unreachable power again after run_forever restarts them.
  • Worth a look: exclusion bounds are not shifted, and the SystemBounds docs now say they can lie outside the inclusion bounds. The distributor and each pool run their own unreachable-power formula; they don't share one.
  • The tests replace the formula pool with a stub (tests/utils/unreachable_power.py), because formulas produce no values in the mock fixtures.
  • Independent of Share reference stores between pools for all and explicit components #1476. Once that is merged, the PV and steam boiler control-method test fixtures here can go down to num_namespaces=2.

@shsms
shsms requested a review from a team as a code owner October 29, 2026 17:59
@shsms
shsms requested review from florian-wagner-frequenz and removed request for a team October 29, 2026 17:59
@github-actions github-actions Bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:data-pipeline Affects the data pipeline part:microgrid Affects the interactions with the microgrid labels Oct 29, 2026
@shsms
shsms marked this pull request as draft October 29, 2026 18:00
@shsms
shsms force-pushed the pool-bounds-unreachable-power branch from 4b3c9c8 to 37ddd47 Compare October 30, 2026 15:57
shsms added 6 commits October 30, 2026 17:21
Tests start the streams before the code under test subscribes, so each
stream's first sample was lost. The second sample then raced the test's
fixed wait, and a 50 ms stall made the request arrive before any data.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Stopping a cache yields to the event loop, and a request being
distributed meanwhile can change the dict, which raised a RuntimeError
on shutdown.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
It measures the components of a set that aren't working with a power
formula, and restarts the formula when that subset changes. Used in the
following commits, by the power distributor and by the pools.

A component that is controlled together with a working one, like a
battery behind the inverter of a working battery, counts as working. A
formula that can't be generated is logged once, and nothing is tracked
until the subset changes, so callers don't fail on every update.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Each requested set now gets its own tracker, with its own formula pool.
Before, one pool served every requested set, and a formula was only
stopped once no set used it anymore.

Nothing is tracked while none of the requested components is working,
because the managers can't distribute power then.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
The battery manager commands the inverters of the working batteries, so
the other batteries behind those inverters aren't unreachable. It
counted them as unreachable anyway. The component graph can't make a
power formula for only some of the batteries behind an inverter, so the
unreachable power of the whole request was left out.

The subscription now runs after the request is checked, because the
expansion needs valid battery IDs.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
The test fixtures don't feed the unreachable-power formulas, so tests
that need a measured power for components that aren't working patch the
formula pool with this stub. Used in the following commits.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
@shsms
shsms force-pushed the pool-bounds-unreachable-power branch from 37ddd47 to f508179 Compare October 30, 2026 16:22
@shsms
shsms marked this pull request as ready for review October 30, 2026 16:27
Marenz
Marenz previously requested changes Nov 2, 2026

@Marenz Marenz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is right, and the examples in the tests match what I get when I work through the numbers by hand. Two things I would like to change before merge.

  1. _unreachable_power.py:202 stores the sample even when its value is empty, and power then returns None, so the bounds lose the shift. Formulas send an empty value whenever one of their inputs has a gap (formulas/_ast.py:149), and a component that is not working is the most likely one to have gaps. So the bounds will jump between shifted and not shifted every resampling period, and the target power will jump with them. Can we keep the last real value, maybe with a maximum age, instead of clearing it? The power distributor had the same problem before, so a fix here helps both.

  2. About the exclusion bounds: I think they need the same shift. The power the pool can reach is the working components' range moved by the measured power, so the whole range moves, the exclusion zone included. With 2 working batteries of +-10 kW, a +-2 kW dead band and 2 blocked batteries at -5 kW, the bounds become [-25 kW, +15 kW] but the exclusion zone stays at +-2 kW. Then a request for -1 kW is moved to -2 kW even though the working batteries would only need +3 kW, and a request for -4 kW is allowed even though the working batteries would need +1 kW, inside their dead band. Shifting both keeps 0 W allowed, so I don't see a downside.

One more thing, not a blocker: for PV and steam boilers the pool bounds count an uncertain component as usable (pv_pool/_system_bounds_tracker.py:105), but the power distributor does not (_pv_inverter_manager.py:124). A failed set-power gives UNCERTAIN, which is the case in the PR description, so for those two pools the power is still taken off the target twice. The battery pool is fine, because both sides use get_working_components(). Should that be part of this PR or a follow-up?

Small notes: please add an "Upgrading" entry for the new exclusion bounds meaning, since the old docs promised they are inside the inclusion bounds. The release notes line about sharing state between pools (line 22) is hard to read.

Thanks, nice work on the shared-inverter case and on the test that proves it.

@github-project-automation github-project-automation Bot moved this from To do to Review in progress in Python SDK Roadmap Nov 2, 2026
@shsms
shsms dismissed Marenz’s stale review November 2, 2026 10:52

Its too red

shsms added 5 commits November 2, 2026 15:29
The power distributor subtracts the measured power of non-working
batteries from the target, but the bounds only covered the working
batteries, so that power was taken off the target twice. The bounds
measure it with the same formula as the distributor, with the same meter
fallback, and count the batteries behind a working battery's inverter as
working, as the distributor does.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Both trackers summed the bounds of their working components the same
way. The next commit changes that sum in both.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Their power distributors subtract that power from the target too, as in
"Include non-working batteries' power in the battery pool bounds".

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
The PV and steam boiler bounds trackers stop tracking unreachable power
when they restart after an error. The status isn't sent again until it
changes, so the bounds lost the shift until then.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
@shsms
shsms force-pushed the pool-bounds-unreachable-power branch from f508179 to 5240655 Compare November 2, 2026 14:29
@shsms

shsms commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

The fix is right, and the examples in the tests match what I get when I work through the numbers by hand. Two things I would like to change before merge.

  1. _unreachable_power.py:202 stores the sample even when its value is empty, and power then returns None, so the bounds lose the shift. Formulas send an empty value whenever one of their inputs has a gap (formulas/_ast.py:149), and a component that is not working is the most likely one to have gaps. So the bounds will jump between shifted and not shifted every resampling period, and the target power will jump with them. Can we keep the last real value, maybe with a maximum age, instead of clearing it? The power distributor had the same problem before, so a fix here helps both.

You're right that the bounds and the target jump, but gaps aren't the main cause. The formulas have a fallback, like COALESCE(#8, #7, 0.0). With scripted streams, a dropout gave one empty sample and then the meter's value. Without a meter, it gave 0 W and no empty sample.

The bigger cause is the formula restart. Each change to the set of non-working components starts a new formula, and it has no value for up to 1 s. Keeping the last real value doesn't help there, because a new formula has no last value. Until it has one, the power distributor subtracts nothing, so total power can overshoot. If the bounds have no value either, the target also drops.

This was there before this PR, and the PR is still a clear improvement. I tested it with 7 × 100 kW batteries and 1 s resampling, with -600 kW requested and 3 inverters failing commands while their telemetry stayed live:

Before this PR This PR
Average total power -425 kW -600 kW
Seconds more than 20 kW off 42 of 44 7 of 44
Commands sent with nothing subtracted 6 7

So I'd leave this out of this PR and handle it in a follow-up. The idea, not tested yet, is to keep each component's power stream running, so that a change to the set doesn't restart a formula.

  1. About the exclusion bounds: I think they need the same shift. The power the pool can reach is the working components' range moved by the measured power, so the whole range moves, the exclusion zone included. With 2 working batteries of +-10 kW, a +-2 kW dead band and 2 blocked batteries at -5 kW, the bounds become [-25 kW, +15 kW] but the exclusion zone stays at +-2 kW. Then a request for -1 kW is moved to -2 kW even though the working batteries would only need +3 kW, and a request for -4 kW is allowed even though the working batteries would need +1 kW, inside their dead band. Shifting both keeps 0 W allowed, so I don't see a downside.

You're right that the working batteries' dead band moves with the measured power, but shifting the pool's exclusion bounds alone makes things worse. _check_request compares the full target with the unshifted exclusion bounds, and 0 W has special handling.

I ran your example through the real clamp, request check and distribution code, taking -5 kW as the total blocked power and ±1 kW as the dead band per battery:

Request Today: target → working batteries → total Exclusion bounds shifted: target → working batteries → total
-1 kW -2 → +3 kW ([+1.5, +1.5]) → -2 kW -1 → rejected with OutOfBounds
-4 kW -4 → +1 kW ([0, +1]) → -4 kW -3 → +2 kW ([+1, +1]) → -3 kW
-4.5 kW -4.5 → +0.5 kW ([-0.5, +1]) → -4.5 kW -3 → +2 kW ([+1, +1]) → -3 kW
-5 kW (working batteries idle) -5 → 0 kW ([0, 0]) → -5 kW -7 → -2 kW ([-1, -1]) → -7 kW

The shifted zone also leaves out the request that idles the working batteries. With -1 kW blocked, the zone is -3 to +1 kW. A 0 W request only passes because of the special handling, and -1 kW is moved to -3 kW.

The -4.5 kW row is a real problem, but it isn't new, because the distributor already subtracted that power before this PR. A proper fix needs _check_request and the zero rules in both algorithms to change as well, so I'd keep this PR as it is and handle it separately.

One more thing, not a blocker: for PV and steam boilers the pool bounds count an uncertain component as usable (pv_pool/_system_bounds_tracker.py:105), but the power distributor does not (_pv_inverter_manager.py:124). A failed set-power gives UNCERTAIN, which is the case in the PR description, so for those two pools the power is still taken off the target twice. The battery pool is fine, because both sides use get_working_components(). Should that be part of this PR or a follow-up?

The mismatch is real, but it was there before this PR, and I don't think it takes the power off twice. The bounds keep the uncertain component's whole range, so the target isn't clamped too far.

For example, A is working, B is uncertain at -10 kW, and each has a range of -30..0 kW. The bounds say -60..0 kW, but only -40..-10 kW is reachable. A -60 kW request gives A -30 kW, so the total is -40 kW, which is the most that's possible. The only wrong thing is the reported range. A double cut needs B's measured power to be outside its own reported bounds.

Fixing it now has a cost. Each failed set-power would then start a formula on the bounds side too, so these two pools would get the same short dip as the battery pool, and they don't have that dip in this case today. I'd rather do this as a follow-up together with the per-component power streams, where it wouldn't cause a dip.

Small notes: please add an "Upgrading" entry for the new exclusion bounds meaning, since the old docs promised they are inside the inclusion bounds.

Added an "Upgrading" entry for this.

The release notes line about sharing state between pools (line 22) is hard to read.

Reworded it. That line came in with #1476, but it isn't released yet, so I fixed it here.

@Marenz Marenz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is a good answer. I checked all three points and you are right on all of them.

On the empty samples: I generated the real formulas and ran the Coalesce node with scripted streams. With a meter I get one empty sample and then the meter value; without a meter I get 0 W and no empty sample. So my "jumps every resampling period" was wrong, and keeping the last value would only bridge that single sample. The restart is indeed the bigger cause. I also worked through the four combinations of "bounds shifted" and "distributor subtracts", and only one of them loses power, which is the one the code was always in before this PR. So the restart gap is what is left of the old bug, not a new one, and your numbers match that.

On the exclusion bounds: I reproduced your table with the real clamp and request check code and got the same four rows, including the OutOfBounds. So shifting them alone is worse, and my suggestion was wrong. A correct fix has to move the "0 W is always allowed" rule as well, in both algorithms and in _check_request.

On the uncertain components: agreed, it is not a double cut as long as the component's measured power is inside its own reported bounds. It can still happen when the component sits at an older setpoint while its reported bound has shrunk, so I wrote that condition into the follow-up issue.

I opened #1486, #1487 and #1488 for the three follow-ups, so this is good to merge from my side.

@github-project-automation github-project-automation Bot moved this from Review in progress to Review approved in Python SDK Roadmap Nov 2, 2026
@shsms
shsms added this pull request to the merge queue Nov 2, 2026
Merged via the queue into frequenz-floss:v1.x.x with commit b711453 Nov 2, 2026
9 checks passed
@shsms
shsms deleted the pool-bounds-unreachable-power branch November 2, 2026 14:54
@github-project-automation github-project-automation Bot moved this from Review approved to Done in Python SDK Roadmap Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:data-pipeline Affects the data pipeline part:docs Affects the documentation part:microgrid Affects the interactions with the microgrid part:tests Affects the unit, integration and performance (benchmarks) tests

Projects

Development

Successfully merging this pull request may close these issues.

2 participants