Limits and envelopes
Caps over a window, envelopes that only get stricter, provenance on every bound, schedules checked ISP by ISP, explain, headroom, and § 14a guarantees.
This page follows examples/evening_cap.rs: a grid operator caps a site's consumption for the evening peak, the site's connection limits it all day, § 14a EnWG keeps 4.2 kW guaranteed, and an aggregator's charging plan is checked against all of it.
Provenance#
Every limit, guarantee and schedule carries a Provenance: who set it, under which reference (an order, rule or contract id), and when. It displays as dso.example flex-order-42.
The values are opaque: the crate authenticates nobody. Who may issue a limit is the integrator's decision.
Limits and bounds#
Bounds is an optional minimum and an optional maximum (both inclusive, the minimum never above the maximum). With consumption positive, a consumption cap is a maximum and a feed-in cap is a negative minimum. A Limit is bounds over a window, with provenance:
| Constructor | Bounds |
|---|---|
Limit::consumption_cap(window, max, who) | at most max (a negative max demands feed-in) |
Limit::feed_in_cap(window, amount, who) | at least -amount; a negative amount is refused |
Limit::consumption_floor(window, min, who) | at least min: a must-run load |
Limit::feed_in_floor(window, amount, who) | at most -amount: a generator that must keep producing |
Limit::band(window, min, max, who) | between both: a device's own range (-10 kW to 0 W for a PV array) |
Limit::new(window, Bounds::new(min, max)?, who) | anything else |
Envelopes only get stricter#
An Envelope is the allowed range per ISP of a grid. apply adds a limit to every ISP its window overlaps (even partly); combine merges two envelopes. Both take the lower maximum and the higher minimum in every ISP, so an envelope never widens. If an ISP is left with no allowed power, the result is Error::EmptyRange naming that ISP.
use flexibility::jiff::civil::{date, time};
use flexibility::jiff::tz::TimeZone;
use flexibility::{Envelope, Grid, Guarantee, IspDuration, Limit, Provenance, Window, kw};
let berlin = TimeZone::get("Europe/Berlin")?;
let grid = Grid::day(date(2026, 12, 1), &berlin, IspDuration::FIFTEEN_MINUTES)?;
let day = Window::new(grid.start(), grid.end())?;
let evening = grid.window(time(17, 0, 0, 0), time(19, 0, 0, 0))?;
let noon = grid.local(time(12, 0, 0, 0))?;
let rule = Provenance::new("bnetza", "§ 14a minimum", grid.start());
let connection = Provenance::new("site.example", "connection-11kW", grid.start());
let dso = Provenance::new("dso.example", "flex-order-42", noon);
let envelope = Envelope::unbounded(grid.clone())
.guarantee(&Guarantee::consumption(day, kw!(4.2), rule)?)?
.apply(&Limit::consumption_cap(day, kw!(11), connection.clone()))?
.apply(&Limit::consumption_cap(evening, kw!(4.2), dso.clone()))?;Envelope::bounds(isp), max_source(isp) and min_source(isp) read one ISP; when two limits set the same bound, the one applied first keeps the credit.
Checking a schedule#
A Schedule is planned power per ISP, with provenance. Schedule::check returns every ISP in which the plan leaves the envelope, in order, each as a Violation that reads as a sentence:
use flexibility::{Broken, Schedule, Series};
// A car charging at 7.4 kW from 16:00 to 20:00, nothing otherwise.
let charging = grid.window(time(16, 0, 0, 0), time(20, 0, 0, 0))?;
let hours = grid.isps(charging).ok_or("the window is on the grid")?;
let plan = Series::new(
grid.clone(),
(1..=grid.count())
.map(|isp| if hours.contains(&isp) { kw!(7.4) } else { kw!(0) })
.collect(),
)?;
let schedule = Schedule::new(plan, Provenance::new("agr.example", "charging-plan-7", noon));
let violations = schedule.check(&envelope)?;
assert_eq!(violations.len(), 8); // ISPs 69 to 76
assert_eq!(violations[0].broken, Broken::AboveMax(kw!(4.2)));
assert_eq!(violations[0].source, dso);ISP 69 (17:00–17:15 +01:00): planned 7.4 kW above max 4.2 kW set by dso.example flex-order-42An empty vector means the plan fits. The crate checks plans; it does not find them.
Explain: why an ISP allows what it allows#
Envelope::explain(isp) returns the effective bounds, who set each, and every other limit applied to that ISP that is in force but looser (shadowed):
let why = envelope.explain(69).ok_or("ISP 69 is on the grid")?;
assert_eq!(why.max_source.as_ref(), Some(&dso));
assert_eq!(why.shadowed.len(), 1);
println!("{why}");ISP 69 (17:00–17:15 +01:00): max 4.2 kW (dso.example flex-order-42)
shadowed: max 11 kW (site.example connection-11kW)With a guarantee (below) in force, the explanation lists it too:
ISP 69 (17:00–17:15 +01:00): max 8.5 kW (dso.example lpc-2026-12-01-17)
guaranteed: consumption of 8.001 kW stays allowed (bnetza.de BK6-22-300 Anlage 1 Ziff. 4.5)The Explanation is data (label, bounds, max_source, min_source, shadowed, guaranteed); the text above is its Display.
Headroom: how far a plan may move#
Envelope::headroom(&schedule) gives, per ISP, how far the plan may still move as deviations from it: up to the maximum and down to the minimum, None where there is no bound. Where the plan already breaks a bound, that side has the other sign by the excess.
let room = envelope.headroom(&schedule)?;
let up = |isp| room.get(isp).and_then(|h| h.up).map(|d| d.to_string());
assert_eq!(up(65).as_deref(), Some("+3.6 kW")); // 16:00, under the 11 kW connection limit
assert_eq!(up(69).as_deref(), Some("-3.2 kW")); // 17:00, 3.2 kW over the DSO's capGuarantees: the § 14a minimum#
Under § 14a EnWG a grid operator may reduce a controllable consumer only down to a minimum power. Registered as a Guarantee, that minimum makes the envelope refuse any bound that cuts below it, instead of applying it. A guarantee adds no bound of its own.
let too_deep = envelope.apply(&Limit::consumption_cap(evening, kw!(3), dso.clone()));
println!("{}", too_deep.unwrap_err());ISP 69 (17:00–17:15 +01:00): bound 3 kW (dso.example flex-order-42) breaks the guarantee of 4.2 kW (bnetza § 14a minimum)The error names both parties. Guarantee::consumption(window, amount, who) keeps consumption of amount allowed; Guarantee::feed_in(window, amount, who) keeps feed-in of amount allowed. Envelope::guarantee itself fails if a bound already in the envelope breaks the new guarantee, and combine carries the guarantees of both inputs and checks them all.
Requests are relative#
A limit is absolute: at most 4.2 kW. A flexibility request is relative to a baseline: consume at least 10 kW less than you would have (UFTP: "a request is always relative to the agreed baseline"). A DeviationEnvelope holds a request: per ISP an optional Band with the lowest and highest allowed Deviation and a Disposition — Requested (a deviation is needed), Available (there is room) or Unstated.
let need = Band::new(Deviation::from_watts(-30_000), Deviation::from_watts(-10_000), Disposition::Requested)?;
let two = Grid::new("2026-12-01T00:00:00Z".parse()?, IspDuration::FIFTEEN_MINUTES, 2, TimeZone::UTC)?;
let dso_request = Provenance::new("dso.example", "request-7", two.start());
let request = DeviationEnvelope::new(Series::new(two.clone(), vec![Some(need), None])?, dso_request);
let offer = Series::new(two, vec![Some(Deviation::from_watts(-5_000)), None])?;
for misfit in request.check(&offer)? {
println!("{misfit}");
}ISP 1 (00:00–00:15 +00:00): offered -5 kW outside -30 kW to -10 kW (requested) set by dso.example request-7Each Misfit has a MisfitKind:
| Kind | The ISP |
|---|---|
Outside | offered outside its band |
Unrequested | offered where the request names no band |
NotOffered | left out of the offer, and its band excludes 0 W |
The offer is a Series<Deviation>, or a Series<Option<Deviation>> with None where it names no ISP, on the request's grid or a slice of it. Leaving ISPs out is allowed: UFTP rejects an offer only when it names none of the Requested ISPs (§ 3.8.3). A DSO reads NotOffered as need still open; an aggregator checking its own offer can skip it.
request.resolve(&baseline) gives the absolute envelope once a baseline is agreed: baseline + min to baseline + max in every banded ISP, set by the request's provenance, so explain names the DSO. A UFTP FlexRequest converts with uftp::request.