Skip to content

Expression Language

James Dunkerley edited this page Dec 3, 2026 · 3 revisions

The expression language

Home

Written against d5aad1f44 on 20 Nov 2026.

In short. Conductor has one small formula language, used in two places. A custom constraint is a linear rule the solver must keep, such as "wind supplies at least 30% of the load". A custom statistic is a formula over a solved run's results, such as a capacity factor. Both name components and attributes the same way, so what you learn for one works in the other. No expression runs code.

sum(Generator[carrier="wind"].p) >= 0.3 * sum(Load.p_set)
sum(supply(Generator), over=time) / (optimal_capacity(Generator) * total_timestep_weight())

The first line is a constraint. The second is a statistic: each generator's capacity factor.

Where you write one

Custom constraint Custom statistic
Where The Constraints section of the network page Custom statistics on the network's Statistics page
What it is Two sides joined by ==, <= or >= One formula, with no comparison at the top
When it is used At each solve, as extra rows of the optimisation On request, against the results of the run you choose
What it gives back A dual, and whether it was binding A number, or a table by component, by timestep or both
Editing it Makes a new version and clears results Makes a new version and keeps results, so the change shows on past runs
Saved with A name and an optional description A name, an optional unit label and an optional description

A workflow can also add, change and remove a network's constraints, with the Add constraint, Update constraint and Remove constraint steps.

Both are saved on the network, so they are versioned, compared and restored with it. A constraint that does not check is refused when it is saved. A filter that matches no component is a warning, not an error, because a later component may match it.

Naming values

A value is a component type, an optional selection in square brackets, and an attribute.

You write It means
Generator.p p of every generator, at every timestep
Generator[carrier="wind"].p_nom p_nom of the generators whose carrier is wind
Link["charger"].p_nom p_nom of the one link named charger
Link["a", "b"].p_nom p_nom of the links named a and b
Generator.bus.marginal_price For each generator, the price at its bus
Generator[bus.carrier="AC"].p p of the generators whose bus has the carrier AC

The names are the framework's own. On a PyPSA network they are PyPSA's types and attributes. On a Calliope network they are Node, Tech and TechNode, filtered by techs, nodes and carriers, as in TechNode[techs="ccgt", carriers="electricity"].flow_out.

A step through a reference, such as .bus., goes one step only. Generator.bus.carrier is allowed; a chain of two steps is not.

Filters

Several filters separated by commas must all hold. A filter reads input values only.

Filter Holds when
carrier="wind" The value is exactly wind
carrier!="wind" The value is anything else
carrier in ("wind", "solar") The value is one of the list
carrier not in ("wind", "solar") The value is none of the list
carrier like "wind*" The value matches the pattern. * is any text, ? is one character, and case is ignored
p_nom > 100 A numeric input compares so. <, <=, > and >= take a number

A text value takes either kind of quote. Inside one, \", \' and \\ are the only escapes. A quoted name on its own, Link["charger"], is short for Link[name="charger"], and is not mixed with other filters.

On a PyPSA type with two ends, such as a line or a link, bus reads bus0, and bus0 and bus1 are filter keys too.

The three kinds of attribute

Each attribute is an input, a variable or a result, and the kind decides where it may stand.

Kind Examples (PyPSA) In a constraint In a statistic
Input p_max_pu, marginal_cost, efficiency, p_set A known number Its input value
Variable p, p0, e, p_nom, s_nom, e_nom What the solver decides Its solved value
Result marginal_price, p_nom_opt, every measure Refused Its solved value

A constraint that names Generator.p_nom_opt is refused with "write p_nom", because in a constraint the capacity is the variable p_nom.

Shapes: components and time

Every value is a grid of component × timestep. Generator.p has both dimensions; Generator.p_nom has components only. Three things change a shape.

  • sum and mean collapse. With nothing more they collapse both dimensions to one number. over=components collapses the components and keeps time; over=time collapses time and keeps the components.
  • One written name drops the component dimension. Link["charger"].p_nom is one number. A filter that happens to match one component keeps the dimension, so a shape never depends on the data.
  • Two sides align by component name and timestep. A dimension only one side has is repeated across the other. Where the two sides select different components, the ones they share are used, and sides that share none are refused.

So a constraint is one row for each cell its two sides line up on:

Constraint Rows
sum(Generator[carrier="solar"].p_nom) <= 5000 One
Link["charger"].p_nom == 0.95 * Link["discharger"].p_nom One
sum(Generator[carrier="wind"].p, over=components) >= 0.3 * sum(Load.p_set, over=components) One for each timestep
Generator.p <= 0.9 * Generator.p_max_pu * Generator.p_nom One for each generator at each timestep

A constraint with many rows reports a dual and a binding flag for each row, summarised on the run page (for example, binding in 37 of 8,760 timesteps).

Time is weighted

When sum, mean or count collapses time, each timestep counts for its weighting, the hours it stands for. So sum(Generator.p) over time is an energy in MWh, not a sum of MW readings, mean is a weighted average, and count gives weighted timesteps.

A measure over time, such as supply, is already an energy and is not weighted again. The check keeps the two apart: adding or comparing an energy and a power is refused (supply(Generator) - Generator.p), and so is a value that would count the hours twice (supply(Generator) * supply(Generator)).

Functions

Function Gives In a constraint
sum(value, over=…) The total Yes
mean(value, over=…) The average. Also takes a condition: the share of cells it holds for Yes, over values
min(value, over=…), max(value, over=…) The smallest or largest Over inputs only
abs(value) Each value without its sign Over inputs only
count(value, over=…) How many values, or how many cells a condition holds for No
where(condition, value, otherwise) The value where the condition holds, else otherwise No
total_timestep_weight() The sum of the timestep weightings. hours() is its old name and still works No
objective() The objective value of the solve No

What a constraint may hold

A constraint must be linear, so:

  • A variable may be multiplied or divided only by an input or a number. Two variables multiplied are refused. A division by an input that is zero fails the run, naming the constraint.
  • > and < are refused: an optimiser cannot hold a strict inequality, so write >= or <=. A single = is refused; write ==.
  • A result, a measure and a condition are refused.

Conditions (statistics only)

A comparison is a condition, and it stands only inside count, mean or where. Conditions join with and, or and not, and compare with ==, !=, <, <=, > and >=.

count(Bus.marginal_price > 100, over=time)
mean(where(Bus.marginal_price < 0, Bus.marginal_price), over=time)
sum(where(Generator.p > 0, Generator.p_nom))

A where with no otherwise leaves the other cells out: sum adds only the cells kept, mean averages over them, and a mean of none is a gap, not zero. Write where(condition, value, 0) to fill them. A division by zero is an error, except in a cell that where leaves out.

Measures (statistics only)

A measure is a result the solve works out and stores for each component, with the framework's own calculation. It is written as a function over a selection: supply(Generator[carrier="wind"]), or capex() for every component. A measure takes no over=; wrap it in sum or mean.

Measure Gives Unit (PyPSA) Over time Calliope
optimal_capacity The capacity the solve chose MW No Yes
installed_capacity The capacity before the solve MW No No
optimal_energy_capacity The energy capacity chosen, for stores and storage units MWh No No
installed_energy_capacity The energy capacity before the solve, for stores and storage units MWh No No
supply The energy put into the system MWh Yes Yes
withdrawal The energy taken out of the system MWh Yes Yes
capex The capital cost of the capacity currency No Yes
opex The operating cost currency Yes Yes
start_up_opex, shut_down_opex The start-up or shut-down cost, which opex includes currency Yes No
revenue What the component earned at the market price currency Yes No
congestion_rent What a line earned from the price difference between its buses currency Yes No
load_weighted_price A bus's price over the horizon, weighted by what it withdraws currency/MWh No No

Three more are built in and worked out from the measures on request:

Built-in Gives Unit (PyPSA) Calliope
capacity_factor Supply over the horizon as a share of what the optimal capacity could supply p.u. Yes
curtailment The energy a component could have supplied but did not, at each timestep MWh No
market_value The average price the supply earned: revenue over supply currency/MWh No

Calliope reports no prices, so the priced measures are refused on a Calliope network, and its units are the words power, energy and cost, because a Calliope model fixes none. Values are never converted; the app scales a column for display, and the unit label of a custom statistic is free text.

A statistic cannot name another statistic.

Examples

Statistic Expression
Total system cost sum(capex()) + sum(opex())
Energy by generator sum(supply(Generator), over=time)
System operating cost at each timestep sum(opex(), over=components)
Objective for each weighted timestep objective() / total_timestep_weight()
Timesteps with a price above 100, by bus count(Bus.marginal_price > 100, over=time)
Market value of wind sum(supply(Generator[carrier like "wind*"]) * Generator.bus.marginal_price) / sum(supply(Generator[carrier like "wind*"]))
Constraint Expression
Charger and discharger sized together Link["charger"].p_nom == 0.95 * Link["discharger"].p_nom
A minimum wind share of energy sum(Generator[carrier="wind"].p) >= 0.3 * sum(Load.p_set)
A cap on gas energy sum(Generator[carrier="gas"].p) <= 1800
A cap on solar capacity in one region sum(Generator[carrier="solar", bus="SA"].p_nom) <= 5000

The editor

The expression field checks as you type, in the browser, and the server checks again on save.

  • Completion. Ctrl+Space or Alt+Space opens the list of types, attributes (with units), filter keys, values and functions that fit at the cursor. Enter or Tab takes one, and Escape closes the list.
  • Colour. Types, functions, attributes, values and keywords each have a colour.
  • Problems. Each error or warning is underlined over its whole range, and the message of the one at the cursor shows under the field.
  • At the cursor. Under the field the app reads the reference at the cursor in words, shows the signature of the function you are inside, and previews the first few values or matched components.

Limits today

  • PostgreSQL warehouse. It does not yet store measures or the duals of a many-row constraint, so the app reports them as unavailable there. A solve still succeeds.
  • No conditions in constraints, and no rolling sums, new variables or quadratic terms.
  • No reference chains and no statistic that names another.
  • Units are labels. Nothing checks or converts them beyond the energy and power check above.

For contributors

The grammar is contracts/grammar/Expression.g4, with the function table, the messages and the shared cases beside it. The HTTP API and the contracts explains how the Python and TypeScript parsers are generated from it and kept in step. The designs are in docs/specs/: 2026-10-16-custom-constraints-design.md, 2026-10-27-expressions-and-results-design.md and 2026-11-07-expression-editor-design.md.

Clone this wiki locally