Settlement
Delivered flexibility and power deficiency per ISP exactly as UFTP 3.1 § 2.6 computes them, with a canonical text, a sha256 digest and diff.
The rule#
settle(&baseline, &actual, &ordered, point) settles each ISP of an event against a baseline. All three series are on the event's ISPs (Series::over cuts a day's measurement to them). Per ISP, consumption positive, with baseline b, actual a and ordered deviation o (the sum of the orders on that ISP):
| Quantity | Rule |
|---|---|
| deviation | d = a − b |
| along the order | r = d · sign(o) |
| delivered | clamp(r, 0, |o|) · sign(o) |
| deficiency | max(0, |o| − r) |
Both properties come from UFTP 3.1 § 2.6:
- Delivered is capped at the order. Flexibility beyond what was ordered is not delivered flexibility ("Passive contributions … are not rewarded").
- Deficiency is single-sided. Over-delivery is never a deficiency ("negative deviations are set to 0 since these are not penalized"). A site that moves the wrong way has a deficiency larger than the order.
Delivered is a Deviation in the order's direction; deficiency is a non-negative Power. An ISP with no order (o = 0) settles to zero delivered and zero deficiency.
UFTP's own example#
UFTP 3.1 § 2.6 settles a baseline of 10 MW with 2 MW ordered down (o = −2 MW) against five allocations. settle reproduces the table row for row; this is a test in the crate:
| Actual | r | Delivered (UFTP prints the magnitude) | Delivered in this crate | Deficiency |
|---|---|---|---|---|
| 7 MW | 3 MW | 2 MW | −2 MW | 0 |
| 8 MW | 2 MW | 2 MW | −2 MW | 0 |
| 9 MW | 1 MW | 1 MW | −1 MW | 1 MW |
| 10 MW | 0 | 0 | 0 | 2 MW |
| 11 MW | −1 MW | 0 | 0 | 3 MW |
use flexibility::baseline;
use flexibility::jiff::{civil::date, tz::TimeZone};
use flexibility::{Deviation, Grid, IspDuration, MeasurementPoint, Provenance, Schedule, Series, Window, mw, settle};
let berlin = TimeZone::get("Europe/Berlin")?;
let day = Grid::day(date(2026, 12, 1), &berlin, IspDuration::FIFTEEN_MINUTES)?;
let five = day.slice(69..=73)?;
let all = Window::new(five.start(), five.end())?;
let prognosis = Schedule::new(
Series::filled(five.clone(), mw!(10)),
Provenance::new("agr.example", "d-prognosis-1", five.start()),
);
let base = baseline::dprognosis(&prognosis, all)?;
let actual = Series::new(five.clone(), vec![mw!(7), mw!(8), mw!(9), mw!(10), mw!(11)])?;
let ordered = Series::filled(five, Deviation::from_watts(-2_000_000));
let s = settle(&base, &actual, &ordered, MeasurementPoint::ConnectionPoint)?;
let delivered: Vec<i64> = s.lines().iter().map(|l| l.delivered.watts() / 1_000_000).collect();
let deficiency: Vec<i64> = s.lines().iter().map(|l| l.deficiency.watts() / 1_000_000).collect();
assert_eq!(delivered, [-2, -2, -1, 0, 0]);
assert_eq!(deficiency, [0, 0, 1, 2, 3]);An upward order mirrors the rule: with 2 MW ordered up from 10 MW, actuals of 11, 13 and 9 MW deliver 1, 2 and 0 MW with deficiencies 1, 0 and 3 MW.
Which meter measured it#
The fourth argument is the MeasurementPoint: ConnectionPoint, the meter the Network Code on Demand Response makes the default (Art 12), or MeasurementPoint::device("hp-1"), a dedicated measurement device (Art 13). There is no default. The point is in the canonical text ("measured_at":"connection-point" or {"device":"hp-1"}), so it is in the digest: a sub-meter series cannot pass as the fiscal meter's, because the other side sees it. Whether the contract allows a device is the contract's; the crate records it.
What a settlement holds#
A Settlement is the receipt of the computation, not only its result:
| Accessor | Gives |
|---|---|
lines() | one Line per ISP: isp (label), baseline, actual, ordered, deviation, delivered, deficiency |
delivered(), deficiency() | the totals as Energy (watt-seconds), exact |
method() | the baseline's method id |
measured_at() | the measurement point |
baseline() | the digest of the baseline settled against |
inputs() | the digests of actual and ordered |
canonical(), digest() | the canon@1 text and its sha256: digest |
Its Display prints one line per ISP, then the totals and the digest:
ISP 71 (17:30–17:45 +01:00): baseline 10 MW, actual 9 MW, ordered -2 MW, delivered -1 MW, deficiency 1 MW
…
dprognosis@1: delivered -1.25 MWh, deficiency 1.5 MWh, digest sha256:c7b9655aae3dffb93cd5dfdc321ce95b4518e6289cccccf3905a456e18440efdThe crate computes quantities, not money. Price × quantity, penalties and the bill are the caller's.
The canonical text and digest#
"Same inputs, same digest" holds only if both sides encode the same values to the same bytes. So the encoding is part of the contract and has a name: canon@1. It uses only open standards (RFC 8785 and SHA-256), so any language recomputes it without this crate.
- JSON in the RFC 8785 canonical form, restricted to integers, strings, arrays and objects: keys sorted, no whitespace, strings escaped minimally. There are no floats anywhere.
- Every integer lies within ±2^53 (
MAX_EXACT), where RFC 8785 numbers are exact, so any RFC 8785 library re-encodes the text to the same bytes. A value beyond (a power above 9 PW, an energy total above about 2.5 TWh in watt-seconds) is refused when the settlement is built, never rounded. - Every document names itself in a
profilefield:flexibility.settlement@1,flexibility.baseline@1,flexibility.rebound@1, and for inputsflexibility.power-series@1andflexibility.deviation-series@1. The same bytes are never two kinds of document. - Instants are RFC 3339 UTC; ISP lengths are ISO 8601 durations (
PT15M); power is signed watts; totals are watt-seconds. - The
Digestis the SHA-256 of the canonical text, writtensha256:and 64 lowercase hex digits. It parses back from text withstr::parse. - A change to a document's shape is a new profile version (
flexibility.settlement@2).
The canonical text of the example above:
{"grid":{"count":5,"first":69,"isp":"PT15M","start":"2026-12-01T16:00:00Z"},"inputs":{"actual":"sha256:e46112d298d6f0f26b3be92266888244b24c831466368c86be7caa178d99ccef","baseline":"sha256:666961130d946bc40b96802c210864b24df2ed1b20c36743ab1ead9cae4636da","ordered":"sha256:ca47e1351f3e292790d8af1f11aae7308120d73c2f59ca78c7a0c2b26e9baba4"},"lines":[{"actual":7000000,"baseline":10000000,"deficiency":0,"delivered":-2000000,"deviation":-3000000,"isp":69,"ordered":-2000000},{"actual":8000000,"baseline":10000000,"deficiency":0,"delivered":-2000000,"deviation":-2000000,"isp":70,"ordered":-2000000},{"actual":9000000,"baseline":10000000,"deficiency":1000000,"delivered":-1000000,"deviation":-1000000,"isp":71,"ordered":-2000000},{"actual":10000000,"baseline":10000000,"deficiency":2000000,"delivered":0,"deviation":0,"isp":72,"ordered":-2000000},{"actual":11000000,"baseline":10000000,"deficiency":3000000,"delivered":0,"deviation":1000000,"isp":73,"ordered":-2000000}],"measured_at":"connection-point","method":"dprognosis@1","profile":"flexibility.settlement@1","totals":{"deficiency_ws":5400000000,"delivered_ws":-4500000000}}A settlement's text carries the baseline's digest, not the baseline itself; the baseline has its own text (Baseline::canonical()), and each of its inputs is referenced by digest in turn. canonical_series(&series)? gives the text of a single power series, the document behind the actual input digest, for publishing one input.
Comparing two settlements#
Settlement::diff(&theirs) returns None when both are the same settlement, or the first Difference, checked in this order:
Difference::Method: the baseline methods differ.Difference::MeasurementPoint: they were measured at different points (measured at: ours connection point, theirs device hp-1).Difference::Grid: the settlements cover different ISPs.Difference::Line { isp, field, ours, theirs }: the first ISP and field whose value differs.Difference::Input { name, ours, theirs }: the first input whose digest differs —baseline, thenactual, thenordered— when every line agrees.
assert_eq!(agr.digest(), dso.digest());
assert_eq!(agr.diff(&dso), None);A different meter value in the event names its ISP:
ISP 71 (17:30–17:45 +01:00): actual ours 4 kW, theirs 4.001 kWA difference outside the event (an MBMA day measured differently in the morning) leaves the lines equal and shows as an input difference on baseline.
What came back afterwards: rebound#
After an order, deferred load returns: the NC DR's rebound effect (Art 2(30)), which it asks baselining to consider (Art 14(3)). settlement.rebound(&baseline, &actual, point) reports, for the window right after the settlement (the ISPs of actual), the deviation from the baseline opposite to the order, per ISP and in total, never negative.
let after = grid.window(time(19, 0, 0, 0), time(20, 0, 0, 0))?;
let through = Window::new(evening.from(), after.until())?;
let rebound = dso.rebound(
&baseline::mbma(&measured, through, Mbma { n: 4, gap: 1 })?,
&measured.over(after)?,
MeasurementPoint::ConnectionPoint,
)?;
println!("{rebound}");ISP 77 (19:00–19:15 +01:00): baseline 7 kW, actual 9 kW, rebound 2 kW
…
rebound 2 kWh after settlement sha256:00af5069…, digest sha256:5083ab2e…- Compute the baseline over the order and the window together:
mbma@1over the window alone would read the order's own reduced ISPs as "before", and overstate the rebound, so a baseline that does not cover the order is refused. Its values during the order must be the settlement's. - The window must start where the settlement ends, on the same ISP length, with the same method and measurement point. An order that is zero throughout or changes direction has no one opposite, and is refused.
- The
Reboundhas its own canonical text and digest, and names the settlement's digest among its inputs. Netting, pricing or ignoring it is the contract's.
To check a settlement someone else sent, see Verifying a settlement.