Getting started

Add flexibility, run the examples, and settle an order against a baseline in one short program.

On this page

Add the crate#

cargo add flexibility
# With converters, for example:
cargo add flexibility --features uftp,eebus,s2,openadr

Every feature needs Rust 1.88 or later. The default build's runtime dependencies are jiff (time, with the time-zone database bundled, re-exported as flexibility::jiff) and sha2 (for digests). Each converter feature adds its protocol crate: uftp, eebus, s2-kit, openadr, ieee2030-5, iec60870, cls-eedi and metering.

Run the examples#

Each example asserts its own results, so a run that finishes is a run that passed.

CommandShows
cargo run --example evening_capa DSO cap, a connection limit and a § 14a guarantee combined; a charging plan checked; explain and headroom
cargo run --example settle_an_orderan mbma@1 baseline, a settlement, and both parties comparing one digest
cargo run --example verify_a_receiptread the other side's settlement back and find where you disagree
cargo run --example charge_by_sevenan EV's departure energy, a pool of cars, and a split checked
cargo run --example offer_from_a_poolthree home batteries: steady headroom, a split by it that holds, and an offer that leaves a quarter hour out
cargo run --features uftp --example check_a_settlementa UFTP FlexOrder settled, its FlexOrderSettlement item accepted by uftp's own check, then checked from the message alone
cargo run --features eebus,metering --example control_boxa § 14a control box: the minimum from metering, an LPC limit, the failsafe refused below the minimum, and Lastgang energy as actual power
cargo run --features openadr,s2 --example curtail_pva VTN's OpenADR export cap and the array's range, sent to the inverter as an S2 power envelope

Settle an order#

A DSO ordered 3 kW less consumption from 17:00 to 19:00. The site normally draws 7 kW; during the order it drew 4.5 kW, and 3 kW in the last half hour. This is examples/settle_an_order.rs, shortened:

use flexibility::baseline::{self, Mbma};
use flexibility::jiff::civil::{date, time};
use flexibility::jiff::tz::TimeZone;
use flexibility::{Deviation, Grid, IspDuration, MeasurementPoint, Series, kw, settle};

let berlin = TimeZone::get("Europe/Berlin")?;
let grid = Grid::day(date(2026, 12, 1), &berlin, IspDuration::FIFTEEN_MINUTES)?;
let evening = grid.window(time(17, 0, 0, 0), time(19, 0, 0, 0))?;
let event = grid.isps(evening).ok_or("the evening is on the grid")?; // 69..=76

let measured = Series::new(
    grid.clone(),
    (1..=grid.count())
        .map(|isp| match isp {
            75.. if event.contains(&isp) => kw!(3),
            _ if event.contains(&isp) => kw!(4.5),
            _ => kw!(7),
        })
        .collect(),
)?;

// The method the contract names, with its parameters: no defaults.
let base = baseline::mbma(&measured, evening, Mbma { n: 4, gap: 1 })?;
let actual = measured.over(evening)?;
let ordered = Series::filled(actual.grid().clone(), Deviation::from_watts(-3_000));
let dso = settle(&base, &actual, &ordered, MeasurementPoint::ConnectionPoint)?;

// Delivered is capped at the order; the shortfall is the deficiency.
assert_eq!(dso.lines()[0].delivered.to_string(), "-2.5 kW");
assert_eq!(dso.lines()[0].deficiency.to_string(), "500 W");
assert_eq!(dso.lines()[7].delivered.to_string(), "-3 kW");
println!("{dso}");

println!("{dso}") prints one line per ISP, then the totals and the digest:

ISP 69 (17:00–17:15 +01:00): baseline 7 kW, actual 4.5 kW, ordered -3 kW, delivered -2.5 kW, deficiency 500 W
…
ISP 76 (18:45–19:00 +01:00): baseline 7 kW, actual 3 kW, ordered -3 kW, delivered -3 kW, deficiency 0 W
mbma@1: delivered -5.25 kWh, deficiency 750 Wh, digest sha256:0f3bfec48789c6a3194424f656554fd023ec7d42dc68ea356176b7f78a6f1b4d

The full example also measures the hour after the order (for the rebound), so its day of meter data, and with it the baseline's input digest and the settlement digest, differ: sha256:00af5069….

The aggregator runs the same code on its own copy of the meter data. If both digests are equal, both sides computed the same settlement from the same inputs. If not, Settlement::diff says where they part; see Settlement.

When something is refused#

Every operation that can fail returns Result<_, flexibility::Error>. The message names the values involved, and Error::hint() gives a one-line remedy where the crate knows one:

use flexibility::Power;
let err = "4.0001 kW".parse::<Power>().unwrap_err();
assert_eq!(err.to_string(), "power `4.0001 kW`: not a whole number of watts");
assert_eq!(err.hint(), Some("power is whole watts: at most three decimals in kW and six in MW"));

Next#