Controlling Equipment
Most adapters only read from equipment. An adapter can also steer it: change a setpoint, pause a run, move to the next phase. LEAF then listens on a control topic for each instance and hands every message to the adapter’s controller. The LEAF UI builds a control form for each instance from the controls the adapter declares, so operators do not need to write MQTT messages by hand.
This page uses leaf-indpensim-api, an adapter for a simulated penicillin bioreactor, as the example.
Enabling control for an instance
Section titled “Enabling control for an instance”Control is off by default. Turn it on per instance with control: true in the configuration:
EQUIPMENT_INSTANCES: - equipment: adapter: leaf_indpensim_api data: root: demo instance_id: reactor1 organisation: wur department: ssb requirements: base_url: https://models-reactor.containers.wurnet.nl reactor_id: <id> control: true
OUTPUTS: - plugin: MQTT broker: localhost port: 1883LEAF then subscribes to the instance’s control topic,
<root>/<organisation>/<department>/<adapter_id>/<instance_id>/controlhere demo/wur/ssb/leaf_indpensim_api/reactor1/control, and passes every message on it to the
controller. Control requires an MQTT output, because that is where the messages arrive. Anyone who
can publish to the control topic can steer the equipment, so restrict it with broker ACLs.
Control messages
Section titled “Control messages”A control message is a LeafPoint, the same format adapters use to
publish measurements. The metric tag names what to change and the value field holds its new
value:
{ "measurement": "leaf_indpensim_api", "tags": {"metric": "T_sp", "instance_id": "reactor1"}, "fields": {"value": 305.0}, "timestamp": 1793309669}Every control is “set this metric to this value”. Commands without a natural number become a
metric with a text value, for example batch → "paused" or step → "next". A setpoint uses
the same metric name as the measurement it controls, so the request and its effect line up in
the data. Any other fields (such as a reason) travel next to value.
From the command line:
mosquitto_pub -t 'demo/wur/ssb/leaf_indpensim_api/reactor1/control' \ -m '{"measurement": "leaf_indpensim_api", "tags": {"metric": "T_sp"}, "fields": {"value": 300.0}}'Control results
Section titled “Control results”LEAF publishes every control message, applied or rejected, from MQTT or from the UI, on
<root>/<organisation>/<department>/<adapter_id>/<instance_id>/control/resulthere demo/wur/ssb/leaf_indpensim_api/reactor1/control/result, so what was sent to the equipment is stored next to
the measurements. The record is the control LeafPoint with three extra tags: source (mqtt or ui), status
(applied when control() returned, rejected when a check or the controller raised) and, for a rejection, error
with the reason:
{ "measurement": "leaf_indpensim_api", "tags": {"metric": "T_sp", "instance_id": "reactor1", "source": "mqtt", "status": "rejected", "error": "T_sp = 400.0 is above its maximum 373.15"}, "fields": {"value": 400.0}, "timestamp": 1793309669.0}A message without fields keeps its raw text in a message field. LEAF subscribes only to …/control itself, so results
are never fed back to the controller, and failing to publish a result never makes the command fail.
Writing a controller
Section titled “Writing a controller”Put a controller.py next to the adapter’s adapter.py, with a subclass of AbstractController.
LEAF loads it when an instance has control: true and calls control() for each message.
self.adapter is the running adapter, so the controller can use its connection details.
import json
from leaf.adapters.equipment_adapter import AbstractControllerfrom leafobject import LeafPoint
class Controller(AbstractController): def control(self, topic: str, data: str) -> None: point = LeafPoint() point.from_json(json.loads(data)) metric = point.get_metric() value = point.fields.get("value") if metric == "T_sp" and isinstance(value, (int, float)): self.adapter.set_temperature(value) else: raise ValueError(f"Unknown control {metric!r} = {value!r}")Guidelines:
- Accept only what you expect. Reject unknown metrics and values with an exception. The control topic should never reach anything beyond the controls you list.
- Raise on failure. For MQTT messages, LEAF logs the error and reports it as an
AdapterLogicError; the adapter keeps running. For the UI, the error is shown to the operator. Either way, the command is published as rejected on the control result topic. - Keep it thin. Translate the message into the equipment’s own API and nothing more. The indpensim controller is a table from metric and value to an HTTP endpoint.
Declaring controls for the LEAF UI
Section titled “Declaring controls for the LEAF UI”The Control tab in the LEAF UI shows a card for each running instance with control: true.
Its form comes from a controls list in the adapter’s device.json, next to requirements:
{ "adapter_id": "leaf_indpensim_api", "requirements": {"base_url": "str", "reactor_id": "str", "interval": "int"}, "controls": [ {"metric": "batch", "label": "Batch", "type": "choice", "options": ["paused", "running", "aborted"], "confirm": ["aborted"], "fields": [{"name": "reason", "type": "text", "required": false}]}, {"metric": "step", "label": "Simulation step", "type": "choice", "options": ["next"]}, {"metric": "T_sp", "label": "Temperature setpoint", "type": "number", "reset": "recipe"} ]}Each entry is one row in the form. When the operator presses a button, LEAF builds the LeafPoint (tagged like the instance’s measurements) and passes it to the controller directly.
| key | required | meaning |
|---|---|---|
metric | yes | The metric this control sets; sent as tags.metric. |
type | yes | number (input plus Set button), choice (one button per option) or text (input plus Set button). |
label | no | Name shown in the form; defaults to metric. |
options | for choice | The values to offer, one button each. |
unit | no | Shown next to a number input. |
min, max | no | Limits for a number control. LEAF rejects values outside them. |
reset | no | Adds a button that sends this value as text, e.g. "recipe" to drop a setpoint override. |
confirm | no | Values that ask the operator to confirm first, e.g. ["aborted"]. |
fields | no | Extra inputs sent next to value. Each has name, type (text or number) and required (default true). |
An instance whose adapter declares no controls still appears in the Control tab, with a note that messages can be sent on the control topic.
Checks and limits
Section titled “Checks and limits”When an adapter declares controls, LEAF checks every control message against them before the
controller sees it, whether the message comes from MQTT or from the UI. The metric must be
declared, a choice value must be one of its options, and a number must lie within its
min and max. A control’s reset value is always allowed. A rejected message is logged as a
warning and never reaches the equipment; in the UI the operator sees why, and it is published with
status: rejected on the control result topic. Adapters without a
controls list are not checked, so their controllers must validate messages themselves.
The adapter’s min and max are the widest range the equipment allows. An instance can narrow
them in its configuration with control_limits, for example to keep one experiment within a
tighter temperature band:
control: true control_limits: T_sp: min: 295 max: 305control_limits can only tighten: LEAF refuses to start the instance if a limit lies outside the
adapter’s own range, names a control the adapter does not declare, or is set on a control that is
not a number. The Control tab shows the resulting range under the input.
Keep the controls list and the controller in step. A test that sends every declared control
through the controller catches the two drifting apart; the indpensim adapter has one
(test_device_controls_match_controller).