Repository navigation
Conversation
FlexMeasures is going to serve, next to a schedule's values, a "response" array with one rule per slot: a reference, a gain (0 or -1) and a band given as a list of ranges, all in the schedule's unit (FlexMeasures/flexmeasures#2894). The new flexmeasures_client.recourse module parses that array into ResponseRule objects, pairs them with the schedule's start, resolution, unit and values in a ResponseSchedule, and evaluates a rule with the pure respond() function: clip(p + gain * residual, band), where a continuous answer in a hole between ranges becomes the nearest edge, on a tie the edge on the side of the plan. A residuals() helper walks a response order and returns the residual each device in the chain is given. A schedule without "response" parses to rules None and follows the plan; a length mismatch between "response" and "values" raises InvalidResponseError. Closes the parsing and evaluation part of #297. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
When get_schedule converts the unit itself, for servers older than v0.32.0, the references and band edges of the "response" array now move along with the values, by the same conversion, so that values, references and bands stay in one unit. A schedule served without "response" is returned exactly as before. Part of #297. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
The flex-context fields reference-sensor, response-order, retrigger-on-band-exit and reference-at-start, and the flex-model's response block, are sent to the server as given by trigger_schedule and trigger_and_get_schedule, and a served "response" array reaches the caller as served. No code change was needed; the tests pin the behaviour down for the controller that relies on it. Part of #297. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…prise examples/recourse_rules.py triggers a schedule with reference-sensor, response-order and a response block, fetches it, parses the rules and prints the setpoint per slot for a scripted surprise at the meter. It exits 0 when every setpoint respects the rules, 1 on a failure or a broken invariant, and 2 when the server served the schedule without rules, saying so, so that it doubles as a smoke test against a server that serves rules. Part of #297. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…hangelog Part of #297. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
The per-slot key is now "react": the share of the residual the device absorbs, so that the setpoint is clip(p - react * residual, band). The server serves 0 (follow the plan) or 1 (absorb one to one) in this version; the client accepts any share in [0, 1], as intermediate shares are reserved for a later version, and rejects a value outside [0, 1] with InvalidResponseError. React 0 returns the plan value untouched, as gain 0 did. Part of #297. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
The flex-context fields reference-sensor and reference-at-start no longer exist in the contract; the presence of a non-empty response-order opts in, and retrigger-on-band-exit stays. The example and the scheduling guide drop the two fields, and the pass-through tests pin response-order, retrigger-on-band-exit and the flex-model response block, together with one made-up field, to show that the pass-through is generic. Part of #297. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
The opt-in is now a boolean "react" on the flex-model item of each device that is to respond. There is no response-order, no flex-model response block and no retrigger-on-band-exit any more, so the example sends only "react": true next to the device's usual fields, and the guide shows the same. The pass-through tests pin "react" on the flex-model item, together with one made-up field in the flex-context and one in the flex-model item, to show that the pass-through is generic. Part of #297. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…action The schedule GET now serves its rules under "reaction" rather than "response", with the entries unchanged: a reference, a react share in [0, 1] and a band, one per value. The client module follows the naming: flexmeasures_client.reaction replaces flexmeasures_client.recourse, with ReactionRule (method react_to), ReactionSchedule (method react_at), parse_reaction, InvalidReactionError, the pure function react in place of respond, and convert_reaction_units. The example is examples/reaction_rules.py. The guide spells the rule out as clip(p - react * surprise, band) with surprise = (meter - sum of device power) - reference, and says that react shares between 0 and 1 are reserved for a later version. Part of #297. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
Merging the v0.10.0 release left the entry under the v0.10.0 heading, since that release turned the Unreleased section it was written in into the release's section. A new Unreleased section holds it, for the release after v0.10.0. Part of #297. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
Coverage Report for CI Build 47665427511Coverage increased (+0.2%) to 96.442%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
Flix6x
commented
Nov 24, 2026
Introduce the chain before it is used, give devices with operation modes their own worked example, present the loop as the single-device case, and state that all quantities share the sign convention of the schedule values. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
The planner always knows a device's power capacity, so every side of a band range is a number. The parser now rejects a null side instead of treating it as unbounded, and the guide says so, in place of the sentence about null. Also phrase the chain definition more simply. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
8 tasks done
A synthetic, deterministic day for a bidirectional charger: a forecast of the house, the actual with a cloud, a sunny spell and the oven, the plan a scheduler would make, and the reaction rules that would come with it, evaluated with the client's react(). It draws two pictures for the scheduling guide (matplotlib, in a new examples dependency group) and prints the cost of the day four ways. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…here the plan holds the connection Wider lines underneath, so a series stays visible where the three coincide. The toy classifier reacted in a slot where the plan merely crossed zero with the charger idle; it now requires the plan to hold the connection. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
Eight minutes at the meter's sampling rate around a surprise the charger cannot absorb within its band: the rule pins the charger to its band edge at once, the residual waits out the dwell, one retrigger fires with fresh meter data, nothing else fires while the request is in flight even when the surprise flaps, and the new plan widens the band and closes the residual. Setpoints come from the client's react(); the rest is scripted. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…round trip The dip in the surprise is the oven's heating element cycling off, named as such. The planner's turnaround is 30 seconds, what a scheduling round trip takes in practice, and the window shrinks to four minutes to match. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…st the band ExitGate is the one stateful piece next to the pure functions: fed every sample's residual, it says when a residual left outside the band is worth a new plan, needing both the dwell (the slowest commanded device's settle time plus one sample) and a minimum energy past the band, so a kettle passes and an oven fires; nothing fires while a request is in flight. Its thresholds are parameters, not client settings: the controller that runs the loop owns them. The band-exit example gains a kettle before the oven and a panel for the gate. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
Member
Author
|
The guide's three pictures live in FlexMeasures/screenshots#21 and are embedded from that repository's main branch, so that PR should merge before this one's docs are built. Latest commits here add the saldering and band-exit examples that draw them, and |
… tries again A device that is slower than the settle time, or that refuses a switch, made the policy return to the old mode and its timing, and so switch again on the next sample: one real switch counted three times, and an S2 device would be sent the instruction every 30 s. After a refusal the policy now waits the least gap (at least the settle time) from the report, and counts a new attempt at the same mode in retries rather than switches. hold_until takes an optional target mode, as an S2 timer blocks single transitions, so other switches stay free. A device that refuses a change forced at a slot start reports a mode the new band no longer has; the previous band now counts too, so that report is followed rather than dropped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…n helpers and the probe The bare numbers only bridged existing callers, and nobody passes them any more, so a unit is now required: to_unit and the helpers raise a TypeError for a number, and the probe refuses an option without its unit, naming the unit it expects. ModePolicy's margin defaults to None, no margin, which needs no unit; ImplicitUnitWarning is gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…a schedule A new plan made from the same forecasts is the same plan, so a retrigger helps most when the forecasts of the inflexible sensors take the latest measurements into account. refresh_forecasts(sensor_ids, horizon, timeout, budget) triggers a forecast per sensor from now, waits for them together, and reports per sensor: refreshed, skipped (beyond the client's trigger budget, since a server limits forecast triggers and the client's retries would otherwise hold the schedule back), failed, or timed out. It never fails the schedule that follows; the caller triggers that as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…s, as its asset cache The initialisation test compares the client's settings, and drops its private caches first; the new record of forecast trigger times is one of them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…hangelog entries as additions Three of the four Fixed entries fixed things this PR itself added, never released, and refresh_forecasts is new; they join Added, split into five bullets. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
…earest edge for a current range the band lacks Python's json, and so aiohttp, reads NaN and Infinity; a NaN reference made every residual NaN and react() raise at meter speed. clip_to_band raised on an empty min() when given a current range with no edge on its side, such as a stale mode from the previous slot. Signed-off-by: F.N. Claessen <claessen@seita.nl>
…lds no vulnerable Pillow Pillow is patched for GHSA-332p-7hrc-92wf only from 13.0.0, which needs Python 3.11, so the Python 3.10 entry kept 12.3.0, which Socket still flags. The examples' tests already skip the drawing without matplotlib; the client keeps supporting Python 3.10. Signed-off-by: F.N. Claessen <claessen@seita.nl>
…hedule; skip a forecast the server refuses for its limit A server counts schedule and forecast triggers together (FlexMeasures: 10 per 5 minutes per account), so a budget of forecasts alone let a refresh use up the trigger the schedule that follows needs: the server then refused that schedule, and the client retried it for up to request_retry_timeout (200 s). A forecast refused with a 429 was retried in the same way, and the ConnectionError at the end of the retries escaped refresh_forecasts. Now every accepted trigger counts, one is left for the schedule, a 429 skips the forecast (and the rest of the list) at once, any error is reported per sensor, and timeout bounds the triggers as well as the waits. The docstring points to post_sensor_data rather than the deprecated post_measurements. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
…ts, still importable from reaction Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…tside it A rule without a headroom gets (0, 0), so older servers keep their meaning, and a null side is infinite. excess() gives the part of a residual outside the headroom, unabsorbed() what the device left of it for the exit gate, and convert_reaction_units() converts the headroom with the values. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…om, and gate only what it left of that Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…rift gates Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…te at zero Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
… for every device Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…unk by its step Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
13 tasks done
…ction and residuals, as a flexible device without react is part of the reference Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
… the reaction rules changelog entries Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…ction, and that a controller keeps its own load balancing Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…built on the reaction helpers Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…, so a device moving further than asked sends no band exit Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…n the chain, given the setpoints last commanded A curtailed PV that gets less sun than its setpoint allows departs from its plan without reacting; that departure is a deviation, also for the devices before it in the chain. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…s, in the order in which they react Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…ion rules, in the order in which they react" This reverts commit fc3e3a8: the server-side list of reacting devices waits for a design of a central schedule response. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…how to find those set on the server Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…or to others making up for it, so a covered shortfall does not swing Of its residual with and without its own departure, a device is given the one closer to zero. Leaving its own departure out made a PV curtail against a battery covering its shortfall, sample after sample; putting it in made a PV alone raise its cap without end. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
… the deviation is passed in Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…what the device can do now before it goes to unabsorbed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
…its shortfall, and that a device falling short is not commanded further Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE Signed-off-by: F.N. Claessen <claessen@seita.nl>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
FlexMeasures serves, next to a schedule's
values, areactionarray with one rule per slot (FlexMeasures/flexmeasures#2894): areference(the uncontrolled power the plan is balanced against), areactshare in [0, 1], aband(a list of[low, high]ranges, every side a finite number) and aheadroom(the[low, high]deviations the device lets pass,nullon a side where it never reacts; FlexMeasures/flexmeasures#2965), all in the schedule's unit. A home controller evaluates the rule locally at meter speed:This PR gives the client the parsing and the evaluation, so that every path to a device shares one semantics, plus the four stateful helpers a live controller needs between samples, and three examples.
Module
flexmeasures_client.reaction. The rule functions are pure; the helpers hold the little state a live loop needs, and none does I/O.ReactionRule(reference, react, band, headroom=(0, 0))with.react_to(p, residual, current=None),.excess(residual)and.contains(value);reactis accepted anywhere in [0, 1] (the server serves 0 or 1 in this version) and rejected outside it.ReactionSchedule(start, resolution, unit, values, rules, sensor), built withReactionSchedule.from_schedule(schedule_dict);rulesisNonefor a schedule served without rules, andreact_at(when, residual)then follows the plan.parse_reaction(schedule) -> list[ReactionRule] | None: absent key →None; a length mismatch, a malformed entry, areactoutside [0, 1], anullband side, a headroom that is not a pair holding zero, or a plan value outside its own band (0.5 W tolerance, as the controllers that consume the array check it) →InvalidReactionError. An absentheadroom(a server from before it) is[0, 0], the rule as before; anullside is infinite.react(rule, p, residual, current=None):react == 0returnsp; otherwise clipsp − react · excessto the band. A value in a hole between ranges becomes the edge on the side of the device's current mode (by default the range holdingp), never the far side: with a balance-point reference, nearest-edge rounding would stop a charging car at zero deviation.clip_to_band(value, band, current=None, towards=None),excess(residual, headroom),unabsorbed(rule, p, residual, setpoint)(what the device was asked to absorb and did not, to feed to the exit gate: only what is left in the direction of the excess, so a residual within the headroom, or a device moving further than asked into its next mode, sends no band exit),residuals(deviation, chain, plans, realized, setpoints=None)for the chain of reacting devices (each device is handed the residual minus what the ones before it moved, and applies the headroom, a distance at the connection, to that; given the setpoints last commanded, a device's departure beyond its setpoint, such as a curtailed PV short of sun, goes to every other device in the chain, not only those after it, while the device itself reacts neither to its own departure nor to others making up for it, so a covered shortfall does not swing),convert_reaction_units(reaction, convert).ExitGate(dwell, min_energy, tolerance): a residual outside every band fires one retrigger once it has lasted the dwell and the energy past the band reaches the threshold; nothing fires while a request is in flight; a new plan resets it.DriftGate(threshold=0.5, lead=None, targets=[], threshold_in_window=None): integrates the device's realised minus planned power into a signed drift in kWh, and asks for one new plan once its size reaches the threshold, or a set time before a target when there is any drift; a target may be a window, past only after its end, with an optional tighter threshold inside it. Absorbing moves the state of charge away from the plan, and that drift builds up until a new plan starts from the measured state of charge; a deviation within the headroom is not absorbed, so it adds no drift. The exit gate cannot see it, as it counts only what no device absorbed.reason: band exit, drift, drift before target, drift in target window), and the exit gate how long the current excursion has lasted.Smoother(window, median_samples): a median over the last few samples, then a rolling mean, on the deviation.ModePolicy(dwell=None, margin=0.0): holds the device's current operation mode within a slot by clipping to it, and switches (also into idle) only after the raw setpoint has lain inside another mode, past the hole by a margin, for the dwell; the default never switches within a slot, leaving mode changes to the next schedule.Which devices the client subtracts. Only the devices that got reaction rules (that set
react), whether or not they react in the slot. The server's reference includes the plans of flexible devices withoutreact(FlexMeasures/flexmeasures#2965), so such a device belongs neither in the meter subtraction nor inresiduals'plansandrealized; passing it would count its departure twice. A device with rules but react 0 in a slot stays inplansandrealized, so its departure from plan is handed down the chain.Unit conversion: on the path where
get_scheduleconverts units itself (servers older than v0.32.0) thereactionentries are converted with the same call as the values. For newer servers theunitquery parameter is passed and the server servesreactionin that unit.Server minimum:
REACTION_MIN_SERVER_VERSIONis 1.2.0, compared by release generation, so 1.2.0rc1 counts. An older server refusesreactwith a 422, so the probe checks the version first and exits 2 without triggering.ensure_minimum_server_versionnow compares release generations too, asserver_version_at_leastalready did, instead of refusing a release candidate as too old.Opt-in: a boolean
react: trueon the flex-model item of each device, passed through as given (tests pin one made-up field in each of flex-context and flex-model to show that the pass-through is generic).Examples (
matplotlibin the newexamplesdependency group; pictures in thescreenshotsrepo, FlexMeasures/screenshots#21):examples/reaction_rules.py: triggers a schedule withreact: true, fetches it, parses the rules and walks a day of slots with a scripted deviation (or a recorded one,--deviation-csv) through the smoother, the rule, the mode policy and the exit gate; every knob is an option (--window,--median,--dwell,--min-energy,--tolerance,--mode-dwell,--mode-margin,--drift,--drift-lead,--target-at,--target-window,--drift-in-window,--turnaround); the two gates share one request in flight.--logwrites one CSV row per sample (deviation, setpoints, residual, energy past the band, drift, mode, retriggers with their reason), and--followtails a recording as it grows, for watching a live controller's samples, with--refetchto trigger a fresh schedule periodically. Exit codes: 0 rules served and respected, 1 failure or broken invariant, 2 no rules served, so it doubles as a smoke test.examples/reaction_rules_saldering.py: a synthetic day after net metering ends, plain plan against reaction rules, with the cost at the meter.examples/reaction_rules_retrigger.py: a band exit: a kettle passes the gate, an oven fires one retrigger, the new plan closes the residual.Docs: the scheduling guide says up front that reaction rules are no protection function: physical limits stay with the installation's own protection and local load balancing, and the helpers come without warranty. It gained a section with subsections for a single device, operation modes (the rounding and the mode policy), several devices, a day after saldering, a band exit, and retriggering after absorbing; the README points to it; a changelog entry under Unreleased. The section's introduction describes the headroom, and the subsections show it at work.
referenceis documented as the uncontrolled power the plan is balanced against, and what the meter shows against it as the deviation from that balance point, which in a mode-shaped slot is non-zero for a house exactly as forecast and is rounded back to the plan.Closes #297
How to test
Against a FlexMeasures server that serves reaction rules (FlexMeasures/flexmeasures#2905), with a schedulable power sensor:
The suite passes without a server (1170 tests, including the S2 ones).
Notes for review
uv.lockforks Pillow: 13.0.0 (patched for GHSA-332p-7hrc-92wf) for Python 3.11 and later, 12.3.0 for 3.10, which Pillow 13 no longer supports; the fork comes from aconstraint-dependenciesline inpyproject.toml. Raisingrequires-pythonto 3.11 would remove the vulnerable entry altogether.Checklist
CHANGELOG.rst🤖 Generated with Claude Code
https://claude.ai/code/session_01WGt2vUmJ4goxUYxexuArDE