Senzorv1.0.1

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.

OptionMeaning
-v, --verboseLog every published message at debug level. Giving it more than once has the same effect. RUST_LOG overrides it.
-h, --helpPrint help. senzor-cli help <COMMAND> prints the help of one command.
-V, --versionPrint 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 example examples/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>]]
OptionMeaning
--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.
--watchApply 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:

  1. Loads and validates the project like validate. If there is any problem, it prints the problems and exits with 2 without starting anything.
  2. Selects the devices: those listed in --only, or, without --only, every device with autostart = true.
  3. Starts every instance of the selected devices. Each one connects to the broker with its own client.
  4. 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.
  5. Logs shutting down and stops the instances gracefully: each online instance publishes its availability offline message, if its template has availability, and disconnects. After 2 seconds, connections that are still open are dropped.
  6. 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 to 127.0.0.1, localhost or ::1 are answered. With SENZOR_MCP_TOKEN set, they must also send Authorization: Bearer <token>.
  • On any other address, such as --mcp-bind 0.0.0.0 in a container, other machines can connect, so SENZOR_MCP_TOKEN is required: without it, run prints error: --mcp-bind 0.0.0.0 lets other machines connect; set SENZOR_MCP_TOKEN to the token they must send and exits with 2. Requests without the token get 401 Unauthorized.
  • If the port cannot be used, run prints error: 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 count goes up.
  • An instance that is gone stops, for example when a device is removed or its count goes 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 door

If 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.

OptionMeaning
--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:

  1. Loads and validates the project like validate. If there is any problem, it prints the problems and exits with 2 without connecting.

  2. 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, seq 0 and the current time. A topic that uses a field, seq, now_ms or now_iso is cleared only for that one value.

  3. If there is no such topic, prints no retained topics to clear to stdout and exits with 0 without connecting.

  4. Connects once to the broker with the project's broker settings and MQTT version, with the client ID <client_id_prefix>clear-retained and 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.

  5. Waits until the broker has acknowledged every message, then disconnects. It waits 10 seconds at most in all.

  6. 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: NotAuthorized or I/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 example NotAuthorized when 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.

OptionMeaning
[PROJECT]The project file to open. Without it, mcp starts with an empty project that the agent can fill and save.
--no-autostartLeave 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:

  1. 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, mcp prints a warning that names the file and the problem, for example warning: 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 as open_error in get_status.
  2. Starts the devices with autostart = true, unless --no-autostart is given.
  3. Answers the agent's requests until the agent closes stdin (it disconnected), or until Ctrl+C or SIGTERM.
  4. Stops the instances gracefully, as run does. If the project has unsaved changes, it prints warning: the project has unsaved changes; they are discarded.
  5. Exits with 0.

stdout carries only MCP messages; the log and warnings go to stderr. -v works as for run.

schema

senzor-cli schema

Prints 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

CodeMeaning
0validate: 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.
1The 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.
2Invalid 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.
3run: 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.

MessageExit codeCause
error: cannot read <PROJECT>: <reason>2The file does not exist or cannot be read.
error: no devices to start (none has autostart = true; use --only)2run without --only, and no device has autostart on.
error: --only names no devices2--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>1run, clear-retained or mcp could not create the runtime that drives the MQTT connections.
error: cannot clear retained topics: <reason>1clear-retained failed; see clear-retained for the reasons.
error: <n> instances failed: <ids>3run 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 30ms

Output

  • stdout gets only the OK line of validate, the topics and summary of clear-retained, the MCP messages of mcp, and the output of --help and --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=1

The 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.

SettingFilter
Defaultwarn,senzor_core=info,senzor_cli=info
-vwarn,senzor_core=debug,senzor_cli=debug
RUST_LOG setThe 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.
LevelMessageMeaning
INFOconnecting with MQTT <version>Once, when run starts: the project's MQTT version.
INFOConnectingThe instance started and is connecting to the broker.
INFOOnlineThe broker accepted the connection.
INFOreconnecting: <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".
INFOStoppedThe instance stopped.
ERRORfailed: <reason>The instance stopped because of an error and does not retry.
WARNrender: <error>A message could not be rendered. It is skipped and counted as an error.
WARNmessage 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.
WARNpublish: <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.
WARNpublish online: <error>The availability online message could not be handed to the MQTT client.
INFOreloaded <PROJECT>: <changes>run --watch applied a save of the project file. See --watch.
WARN<PROJECT> has problems; the devices keep running as beforerun --watch found a save with problems, which follow on stderr.
INFOshutting downrun or mcp is stopping.
DEBUGpublish with topic=<topic>, payload=<payload> and, when the message sets MQTT 5 properties, properties=<properties>One published message. Shown with -v.
ERRORMCP 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

VariableEffect
RUST_LOGReplaces 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_COLORAny non-empty value turns off colours in the log, also when stderr is a terminal.
The name in password_envThe broker password. Needed by validate, run, clear-retained and mcp.
SENZOR_MCP_TOKENThe token agents send as Authorization: Bearer <token> to run --mcp-port. Required with an --mcp-bind address other than a loopback address.

On this page