CLI
The senzor-cli commands, options, exit codes, messages and logging.
senzor-cli is the headless runner. For a guided introduction, see
Running headless.
Synopsis
senzor-cli [OPTIONS] validate <PROJECT>
senzor-cli [OPTIONS] run <PROJECT> [--only <ID>[,<ID>…]] [--duration <DURATION>]
[--watch | --mcp-port <PORT> [--mcp-bind <ADDRESS>]]
senzor-cli [OPTIONS] clear-retained <PROJECT> [--only <ID>[,<ID>…]]
senzor-cli [OPTIONS] mcp [PROJECT] [--no-autostart]
senzor-cli [OPTIONS] schema
senzor-cli help [COMMAND]
senzor-cli --version<PROJECT> is the path of a project file.
Global options
-v and -h work before or after the command; -V only before it.
| Option | Meaning |
|---|---|
-v, --verbose | Log every published message at debug level. Giving it more than once has the same effect. RUST_LOG overrides it. |
-h, --help | Print help. senzor-cli help <COMMAND> prints the help of one command. |
-V, --version | Print the version, e.g. senzor-cli 1.0.0. |
validate
senzor-cli validate <PROJECT>Loads and validates a project and prints every problem. It resolves the broker settings as run
does: it reads the environment variable named in password_env and the TLS files, relative to the
folder of the project file. It does not connect to the broker.
- Valid: prints
<PROJECT>: OK (<n> devices, <m> instances, MQTT <version>)to stdout and exits with 0. A count of 1 takes the singular, for exampleexamples/headless-ci.toml: OK (1 device, 10 instances, MQTT 5). - Invalid: prints each problem to stderr as
<PROJECT>: <location>: <message>and exits with 2. See Validation errors.
run
senzor-cli run <PROJECT> [--only <ID>[,<ID>…]] [--duration <DURATION>]
[--watch | --mcp-port <PORT> [--mcp-bind <ADDRESS>]]| Option | Meaning |
|---|---|
--only <ID>[,<ID>…] | Device IDs to start instead of those with autostart = true. Separate IDs with commas or repeat the option. Spaces around an ID are ignored. A device ID is the id of a [[devices]] entry, not an instance ID. |
--duration <DURATION> | Stop after this long, e.g. 500ms, 30s, 10m, 1h 30m. The unit is required. Without this option, run runs until Ctrl+C or SIGTERM. |
--watch | Apply each save of the project file while run runs. See --watch. Not together with --mcp-port. |
--mcp-port <PORT> | Also serve MCP over HTTP on this port, so an AI agent can control the run. See --mcp-port. |
--mcp-bind <ADDRESS> | The address MCP listens on, 127.0.0.1 unless set. Any other address, such as 0.0.0.0, needs a token in SENZOR_MCP_TOKEN. Needs --mcp-port. |
run does the following:
- Loads and validates the project like
validate. If there is any problem, it prints the problems and exits with 2 without starting anything. - Selects the devices: those listed in
--only, or, without--only, every device withautostart = true. - Starts every instance of the selected devices. Each one connects to the broker with its own client.
- Waits for Ctrl+C, SIGTERM (on Linux and macOS), or the end of
--duration. With--watch, it applies each save of the project file meanwhile. - Logs
shutting downand stops the instances gracefully: each online instance publishes its availabilityofflinemessage, if its template has availability, and disconnects. After 2 seconds, connections that are still open are dropped. - Exits with 0 if no instance ended Failed. If any did, it prints
error: <n> instances failed: <ids>to stderr and exits with 3.
--mcp-port
With --mcp-port, run also serves the MCP tools over HTTP at
http://<address>:<PORT>/mcp, and logs MCP at http://127.0.0.1:<PORT>/mcp (with the
--mcp-bind address) when it listens. An agent can then edit the project, start, stop and kill
devices, pin values and read the published messages while the run goes on, as with
mcp. The project, with the agent's edits, is not saved unless the agent saves it.
- Without
--only, nothing to start is not an error: the agent can start the devices. - On
127.0.0.1, only requests addressed to127.0.0.1,localhostor::1are answered. WithSENZOR_MCP_TOKENset, they must also sendAuthorization: Bearer <token>. - On any other address, such as
--mcp-bind 0.0.0.0in a container, other machines can connect, soSENZOR_MCP_TOKENis required: without it,runprintserror: --mcp-bind 0.0.0.0 lets other machines connect; set SENZOR_MCP_TOKEN to the token they must sendand exits with 2. Requests without the token get401 Unauthorized. - If the port cannot be used,
runprintserror: cannot listen on <address>: <reason>and exits with 1. - When the run ends, by Ctrl+C, SIGTERM or
--duration, connected agents lose the connection.
SENZOR_MCP_TOKEN=change-me senzor-cli run project.toml --mcp-port 18830 --mcp-bind 0.0.0.0--watch
With --watch, run looks at the project file once a second. When the file's modification time or
size changes, it waits until they have stayed the same for 200 ms, so that a save written in several
steps is read whole, and then reads the file. A missing or empty file counts as a save in progress:
run waits for the next change. A save with the same content as before changes nothing.
If the new content is valid, run compares each instance it runs with the same instance in the new
content, and carries out the difference:
- An instance whose settings changed, but not how it connects, is updated: it takes the new settings at once and stays connected. That covers its template, messages, fields, params, field overrides, interval, QoS, retain, Home Assistant discovery and the project's seed. See Edits that apply at once.
- An instance whose template's availability changed restarts: it stops gracefully and starts again with the new settings, since its Last Will is fixed for the connection.
- An instance that is new starts, for example when a device is added or its
countgoes up. - An instance that is gone stops, for example when a device is removed or its
countgoes down. - An instance whose settings are the same keeps running untouched.
- If anything in
[broker]changed, every instance that keeps running restarts, since each one connects with those settings.
The devices to run are chosen as at the start: the --only devices, or every device with
autostart = true. Turning autostart off for a device stops it; turning it on starts it. An
--only ID that the saved file no longer has is not an error: that device stops, and starts again
once a later save adds it back.
Each applied save logs one INFO line, reloaded <PROJECT>: <changes>. <changes> lists what
happened, separated by ; : broker changed when [broker] changed, then restarted, updated,
started and stopped, each with a list of IDs. A device ID stands for all of the device's
instances; when only some of them change, or they change in different ways, their instance IDs are
listed instead. If nothing that runs changed, it says no running device changed.
2026-11-07T10:15:02.120417Z INFO senzor_cli: reloaded project.toml: restarted office; updated hall; started lab, kitchen-2; stopped doorIf the new content has problems, run keeps running as before. It logs the WARN line
<PROJECT> has problems; the devices keep running as before, then prints each problem to stderr as
validate does, once per save. The next valid save is compared with what runs.
clear-retained
senzor-cli clear-retained <PROJECT> [--only <ID>[,<ID>…]]Deletes the retained messages that the project's instances leave on the broker.
| Option | Meaning |
|---|---|
--only <ID>[,<ID>…] | Device IDs whose topics to clear instead of every device's, with or without autostart. Written as for run. |
clear-retained does the following:
-
Loads and validates the project like
validate. If there is any problem, it prints the problems and exits with 2 without connecting. -
Lists the topics on which the selected devices' instances can leave a retained message:
- the availability topic of each instance, if its template has availability;
- the message topics of each instance, if its template has
retain = true; - the Home Assistant discovery topics of each instance, one per field, if its template has
home_assistant = true(see Home Assistant discovery).
Each topic is listed once, even if several instances publish on it. Topics are rendered as validation renders them: with each field's initial value,
seq0 and the current time. A topic that uses a field,seq,now_msornow_isois cleared only for that one value. -
If there is no such topic, prints
no retained topics to clearto stdout and exits with 0 without connecting. -
Connects once to the broker with the project's broker settings and MQTT version, with the client ID
<client_id_prefix>clear-retainedand no Last Will. It publishes an empty retained message with QoS 1 to each topic, which tells the broker to delete the retained message there. A topic without a retained message is not an error. -
Waits until the broker has acknowledged every message, then disconnects. It waits 10 seconds at most in all.
-
Prints each topic on its own line to stdout, in sorted order, then a summary, and exits with 0:
senzor/kitchen/status senzor/office-0/status senzor/office-1/status cleared 3 retained topics on localhost:1883
If it fails, it prints error: cannot clear retained topics: <reason> to stderr and exits with 1.
<reason> is one of:
- a connection error in the words a failed instance uses, such as
broker refused the connection: NotAuthorizedorI/O: Connection refused (os error 111)(see Logging); no answer from <host>:<port> within 10 s: the broker did not accept the connection in time;the broker at <host>:<port> acknowledged <n> of <m> topics within 10 s;the connection dropped after the broker acknowledged <n> of <m> topics: <error>;- with MQTT 5,
the broker refused to clear <n> of <m> topics: <reasons>, where<reasons>are the broker's MQTT 5 reason codes, for exampleNotAuthorizedwhen its access control does not let the client publish there. With MQTT 3.1.1 a broker cannot report this.
Stop the project before you clear it: a running instance publishes its retained messages again. If
a topic uses a field value, the topics that other values left behind are not cleared; delete them
by hand, for example with mosquitto_pub -r -n -t <topic>.
mcp
senzor-cli mcp [PROJECT] [--no-autostart]Serves the Model Context Protocol on stdin and stdout, so that an AI agent can control the project. The agent's MCP client starts the command; you do not run it in a terminal yourself. The tools are listed in the MCP tools reference.
| Option | Meaning |
|---|---|
[PROJECT] | The project file to open. Without it, mcp starts with an empty project that the agent can fill and save. |
--no-autostart | Leave every device stopped until the agent starts it. Without it, the devices with autostart = true start, as when the GUI opens a project. |
mcp does the following:
- Opens the project. Unlike
run, a project with validation problems opens: its problems go to stderr as<PROJECT>: <location>: <message>, and only the devices with problems are blocked, as in the GUI. A problem in[broker]blocks every device. If the file cannot be read or has a TOML syntax error,mcpprints a warning that names the file and the problem, for examplewarning: cannot read project.toml: No such file or directory (os error 2); starting with an empty project, and starts with an empty project. The agent sees the same reason asopen_erroringet_status. - Starts the devices with
autostart = true, unless--no-autostartis given. - Answers the agent's requests until the agent closes stdin (it disconnected), or until Ctrl+C or SIGTERM.
- Stops the instances gracefully, as
rundoes. If the project has unsaved changes, it printswarning: the project has unsaved changes; they are discarded. - Exits with 0.
stdout carries only MCP messages; the log and warnings go to stderr. -v works as for run.
schema
senzor-cli schemaPrints the JSON Schema of a project file to standard output and exits with 0. It is the same
schema the documentation site publishes at https://senzor.kankaj.cz/schema/project.json, for
an editor without network access, for example
senzor-cli schema > senzor-project.schema.json. See
Editor support.
Exit codes
| Code | Meaning |
|---|---|
0 | validate: the project is valid. run: it stopped after Ctrl+C, SIGTERM or --duration, and no instance ended Failed. clear-retained: the broker acknowledged every topic, or there was none. mcp: the agent disconnected or never completed the MCP handshake, or Ctrl+C or SIGTERM. Also --help and --version. |
1 | The async runtime could not start, run --mcp-port could not listen on its port, or clear-retained could not clear: the broker could not be reached, refused the connection, did not acknowledge every topic in time, or refused to clear a topic. |
2 | Invalid project or invalid arguments: the file cannot be read, the project has problems, an option is invalid, there are no devices to start (without --mcp-port), or --mcp-bind names an address other machines can reach without SENZOR_MCP_TOKEN. |
3 | run: it stopped after Ctrl+C, SIGTERM or --duration, and at least one instance had ended Failed: the broker refused its connection, for example because of a wrong password or because the broker does not support MQTT 5, or the instance hit an internal error. |
An instance that cannot reach the broker keeps retrying until run stops, and then stops normally,
so it does not make run exit with 3. If the broker was never reachable, run exits with 0. To
check that messages arrived, subscribe to the topics on the broker. mcp never exits with 3, even
when instances failed.
Messages
senzor-cli prints these errors to stderr itself. All but the last two are printed before anything
runs.
| Message | Exit code | Cause |
|---|---|---|
error: cannot read <PROJECT>: <reason> | 2 | The file does not exist or cannot be read. |
error: no devices to start (none has autostart = true; use --only) | 2 | run without --only, and no device has autostart on. |
error: --only names no devices | 2 | --only contains only commas or spaces. |
error: unknown device id(s): <ids> | 2 | --only names IDs that are not devices of the project, listed with a comma and a space. |
error: cannot start the async runtime: <reason> | 1 | run, clear-retained or mcp could not create the runtime that drives the MQTT connections. |
error: cannot clear retained topics: <reason> | 1 | clear-retained failed; see clear-retained for the reasons. |
error: <n> instances failed: <ids> | 3 | run stopped, and these instances had ended Failed. The IDs are listed with a comma and a space, sorted as text (dev-10 before dev-2); a count of 1 takes the singular, as in error: 1 instance failed: kitchen. The reason for each is in the log, in its failed: <reason> line. |
Invalid arguments are reported by the argument parser and exit with 2, for example:
error: invalid value '30' for '--duration <DURATION>': time unit needed, for example 30sec or 30msOutput
- stdout gets only the
OKline ofvalidate, the topics and summary ofclear-retained, the MCP messages ofmcp, and the output of--helpand--version. - stderr gets everything else: problems, errors and the log.
Logging
The log goes to stderr, one line per event:
2026-10-15T15:53:14.229242Z INFO senzor_core::engine::state: reconnecting: I/O: Connection refused (os error 111) instance=kitchen attempt=1The time is UTC. Lines about an instance carry instance=<instance ID>. The log is in colour only
when stderr is a terminal and NO_COLOR is unset or empty; otherwise it is plain text. Other libraries, such as
the MQTT client, log only warnings and errors unless RUST_LOG says otherwise.
| Setting | Filter |
|---|---|
| Default | warn,senzor_core=info,senzor_cli=info |
-v | warn,senzor_core=debug,senzor_cli=debug |
RUST_LOG set | The value of RUST_LOG, which replaces the filter above. A value that cannot be parsed is ignored, and the filter above applies. An empty value logs only errors. |
| Level | Message | Meaning |
|---|---|---|
INFO | connecting with MQTT <version> | Once, when run starts: the project's MQTT version. |
INFO | Connecting | The instance started and is connecting to the broker. |
INFO | Online | The broker accepted the connection. |
INFO | reconnecting: <error> with attempt=<n> | The connection failed or dropped. The instance tries again after 1 s, 2 s, 4 s, …, at most 30 s apart. Over TLS with MQTT 5, if the broker closed the connection before accepting it, <error> ends with a hint to try mqtt_version = "3.1.1". |
INFO | Stopped | The instance stopped. |
ERROR | failed: <reason> | The instance stopped because of an error and does not retry. |
WARN | render: <error> | A message could not be rendered. It is skipped and counted as an error. |
WARN | message on `<topic>` is <size> bytes; the limit is <limit> | A message is larger than the limit: 1 MiB (1048576), or less when an MQTT 5 broker announces a smaller maximum packet size. It is skipped and counted as an error. |
WARN | publish: <error> | A message could not be handed to the MQTT client, for example because its queue is full. It is skipped and counted as an error. |
WARN | publish online: <error> | The availability online message could not be handed to the MQTT client. |
INFO | reloaded <PROJECT>: <changes> | run --watch applied a save of the project file. See --watch. |
WARN | <PROJECT> has problems; the devices keep running as before | run --watch found a save with problems, which follow on stderr. |
INFO | shutting down | run or mcp is stopping. |
DEBUG | publish with topic=<topic>, payload=<payload> and, when the message sets MQTT 5 properties, properties=<properties> | One published message. Shown with -v. |
ERROR | MCP client did not initialize: <error> | mcp: the agent closed the connection or sent something else before the MCP handshake. mcp then stops and exits with 0. |
When the broker refuses a connection, the reason is broker refused the connection: <code>. With
MQTT 3.1.1, <code> is one of RefusedProtocolVersion, BadClientId, ServiceUnavailable,
BadUserNamePassword or NotAuthorized. With MQTT 5 it is the broker's MQTT 5 reason code, for
example BadUserNamePassword or NotAuthorized. When an MQTT 5 instance finds that the broker
supports only MQTT 3.1.1, the reason is the broker does not support MQTT 5; set mqtt_version = "3.1.1" in [broker], or choose 3.1.1 under MQTT version in the broker settings.
Environment variables
| Variable | Effect |
|---|---|
RUST_LOG | Replaces the log filter, e.g. RUST_LOG=warn to log only problems. A value that cannot be parsed is ignored; an empty value logs only errors. |
NO_COLOR | Any non-empty value turns off colours in the log, also when stderr is a terminal. |
The name in password_env | The broker password. Needed by validate, run, clear-retained and mcp. |
SENZOR_MCP_TOKEN | The token agents send as Authorization: Bearer <token> to run --mcp-port. Required with an --mcp-bind address other than a loopback address. |