Senzorv1.0.1

Running headless

Validate and run a project from the command line with senzor-cli, for example in CI or a container.

senzor-cli runs the same project file as the GUI, without a window. It has four commands: validate checks a project, run connects its devices to the broker and publishes until you stop it, clear-retained deletes the retained messages a run leaves on the broker, and mcp lets an AI agent control the project (see Let an agent drive it). This page shows how to use them; the CLI reference lists every flag and message.

Validate a project

senzor-cli validate examples/quick-start.toml
examples/quick-start.toml: OK (2 devices, 3 instances, MQTT 5)

MQTT 5 is the MQTT version every instance connects with. To run against a broker that supports only MQTT 3.1.1, set mqtt_version = "3.1.1" under [broker] in the project file.

validate reads the file and runs the same checks as the GUI. It also resolves the broker settings: it reads the environment variable named in password_env and the TLS files. Run it with the same environment you will use for run, or it reports a missing variable. It does not connect to the broker.

When something is wrong, validate prints every problem on its own line to stderr, each prefixed with the file name, and exits with code 2:

project.toml: broker: port must not be 0
project.toml: device `office`: unknown template `co-2`
project.toml: device `kitchen`: param `seq` uses a reserved name

A TOML syntax error, or a key Senzor does not know, is reported with its position, and the remaining checks are skipped until you fix it:

typo.toml: line 3, column 1: unknown field `prot`, expected one of `host`, `port`, `client_id_prefix`, `keep_alive`, `username`, `password`, `password_env`, `tls`, `mqtt_version`, `session_expiry`

Validation errors explains each message and how to fix it.

Run a project

senzor-cli run examples/quick-start.toml

run first validates the project like validate does. If there is any problem, it prints the problems, starts nothing and exits with code 2. This is stricter than the GUI, where only the devices with errors are blocked: the CLI runs a project only when the whole file is valid, even if you start only some of its devices.

Which devices start

Without --only, run starts every device with autostart = true, which is the default when a device does not set autostart. If no device has autostart on, run prints error: no devices to start (none has autostart = true; use --only) and exits with code 2.

With --only, run starts exactly the devices you list, whether or not they have autostart on:

senzor-cli run examples/quick-start.toml --only office,kitchen
  • --only takes device IDs: the id of a [[devices]] entry, such as office. Instance IDs such as office-0 are not accepted.
  • Separate IDs with commas, or repeat the flag (--only office --only kitchen). Spaces around an ID are ignored.
  • An ID that is not in the project is an error: error: unknown device id(s): <ids>, exit code 2.
  • An --only that names no device at all, such as --only ,, is an error too: error: --only names no devices.

What happens while it runs

Every instance of the started devices connects to the broker as its own MQTT client and publishes on its interval: the device's interval if it sets one, otherwise the template's. The first message of each instance comes at a random point within its first interval, so instances started together do not publish in lockstep (see Reproducible runs).

If the broker cannot be reached, an instance keeps retrying: it waits 1 s, then 2 s, 4 s and so on, up to 30 s between attempts. You can start senzor-cli before the broker is up. If the broker answers but refuses the connection, for example because of wrong credentials, that instance fails and does not retry; the other instances keep running, and run exits with code 3 when it stops. See Availability and Last Will for the details.

Stop

run stops when one of these happens:

  • You press Ctrl+C.
  • The process receives SIGTERM, on Linux and macOS. This is what docker stop, systemd and a plain kill send.
  • The time given with --duration has passed.

--duration takes a number with a unit, such as 500ms, 30s, 10m or 1h 30m. A number without a unit is rejected.

senzor-cli run examples/quick-start.toml --duration 10m

On stop, senzor-cli logs shutting down and stops every instance gracefully: each online instance publishes its offline message, if its template has availability, and then disconnects cleanly, so the broker does not send its Last Will. Senzor waits up to 2 seconds for the instances to finish, then drops any connection that is still open, and exits with code 0. If any instance had failed, it prints error: <n> instances failed: <ids> instead and exits with code 3.

Apply saves while it runs

With --watch, run keeps an eye on the project file and applies each save without a restart of the whole run:

senzor-cli run examples/quick-start.toml --watch

Edit the file in any editor and save it. Within about a second, run compares the saved project with what runs:

  • a device whose settings changed is updated: its instances take the new settings at once and stay connected;
  • a device whose template's availability changed restarts: its instances stop and start again with the new settings, since the Last Will is fixed for the connection;
  • a device you added starts, and a device you removed stops;
  • a device you did not change keeps running untouched;
  • a change in [broker] restarts every device, since each one connects with those settings.

When you change a device's count, only the instances that come or go start or stop. Which devices run follows the same rule as at the start: the devices in --only, or those with autostart = true. Each save logs one line that says what happened:

2026-11-07T10:15:02.120417Z  INFO senzor_cli: reloaded examples/quick-start.toml: updated office; started lab

If the saved file has a problem, nothing changes: the devices keep running with the last valid project, and run prints the problems the way validate does. Fix them and save again. --watch works with --duration, which still ends the run. The CLI reference has the details.

Clear retained messages

Status messages are retained, and so are the messages of a template with retain = true and the Home Assistant discovery messages of a template with home_assistant = true. The broker keeps them after the run, and every new subscriber receives them first. clear-retained deletes them:

senzor-cli clear-retained examples/quick-start.toml
senzor/kitchen/status
senzor/office-0/status
senzor/office-1/status
cleared 3 retained topics on localhost:1883

It validates the project like run, connects once with the project's broker settings, and publishes an empty retained message to each topic, which tells the broker to delete the retained message there. Stop the project first, or its instances publish their retained messages again. With --only, it clears only the topics of the devices you list, written as for run.

A topic that uses a field value is cleared only for the field's initial value. If the broker cannot be reached or refuses, clear-retained prints error: cannot clear retained topics: <reason> and exits with code 1. The CLI reference lists every case.

Logging

senzor-cli logs to stderr. By default it logs at info level:

  • the MQTT version, once at startup: connecting with MQTT 5;
  • status changes of each instance: Connecting, Online, reconnecting: <error>, Stopped;
  • problems: a warning for every message that cannot be rendered or published, and an error when an instance fails.

This is a short run of one device, kitchen, while the broker is not running (the timestamps are UTC):

2026-10-15T15:53:14.228512Z  INFO senzor_cli: connecting with MQTT 5
2026-10-15T15:53:14.229095Z  INFO senzor_core::engine::state: Connecting instance=kitchen
2026-10-15T15:53:14.229242Z  INFO senzor_core::engine::state: reconnecting: I/O: Connection refused (os error 111) instance=kitchen attempt=1
2026-10-15T15:53:15.230419Z  INFO senzor_core::engine::state: reconnecting: I/O: Connection refused (os error 111) instance=kitchen attempt=2
2026-10-15T15:53:16.729666Z  INFO senzor_cli: shutting down
2026-10-15T15:53:16.729975Z  INFO senzor_core::engine::state: Stopped instance=kitchen

Add -v to log every published message at debug level, with its instance, topic, payload and any MQTT 5 properties:

senzor-cli run examples/quick-start.toml -v

The RUST_LOG environment variable replaces both defaults. For example, RUST_LOG=warn logs only problems.

The log is in colour only when stderr is a terminal, so CI logs, docker logs and files get plain text. Set NO_COLOR=1 to turn the colours off in a terminal too.

Exit codes

CodeMeaning
0validate: the project is valid. run: stopped by Ctrl+C, SIGTERM or --duration, and no instance failed. clear-retained: every topic is cleared.
1The async runtime could not start, run --mcp-port could not listen, or clear-retained could not clear the topics.
2The file cannot be read, the project has problems, the arguments are invalid, there are no devices to start (without --mcp-port), or MCP would be open to other machines without a token.
3run: stopped by Ctrl+C, SIGTERM or --duration, and at least one instance had failed, for example because the broker refused a wrong password.

An instance that cannot reach the broker keeps retrying and does not count as failed: run exits with 0 even if no instance ever reached the broker. To check that messages arrived, subscribe to the topics on the broker.

Let an agent drive it

senzor-cli mcp serves the project to an AI agent over MCP on stdin and stdout, instead of running it on its own. The agent starts the command itself, for example after you register it with Claude Code:

claude mcp add senzor -- senzor-cli mcp examples/quick-start.toml

The agent can then start, stop and kill devices, pin values, read the published messages and edit the project. See Controlling Senzor from an AI agent, and the CLI reference for the command's options.

To let an agent join a run instead, for example one in a container, add --mcp-port. The agent connects over HTTP; on an address other machines can reach, it needs the token you set in SENZOR_MCP_TOKEN:

SENZOR_MCP_TOKEN=change-me senzor-cli run project.toml --mcp-port 18830 --mcp-bind 0.0.0.0

See --mcp-port.

In CI and containers

This project starts ten CO2 sensors from the built-in co2 template against a broker service named mqtt, with the password taken from the environment:

examples/headless-ci.toml
# For CI: `senzor-cli run headless-ci.toml --duration 30s` next to a broker service named "mqtt".
[broker]
host = "mqtt"
port = 1883
username = "ci"
password_env = "MQTT_PASSWORD"

[simulation]
seed = 1

[[devices]]
id = "ci-co2"
template = "co2"
count = 10
autostart = true
export MQTT_PASSWORD="…"   # from your CI secret store
senzor-cli validate headless-ci.toml
senzor-cli run headless-ci.toml --duration 30s
  • The instances are ci-co2-0 to ci-co2-9. They publish to senzor/ci-co2-0/co2 and so on (see Built-in templates).
  • password_env keeps the password out of the file. Set the variable for validate as well as for run.
  • seed = 1 makes the CO2 values repeat from run to run (timestamps still differ). See Reproducible runs.
  • --duration ends the job on its own. Without it, stop senzor-cli with SIGTERM.
  • The broker does not have to be ready first: the instances retry until it is.

In a container, make senzor-cli the container's main process: use the exec form of CMD or ENTRYPOINT, not a shell wrapper, so the SIGTERM from docker stop reaches it. The graceful shutdown takes at most about 2 seconds, well within the grace period docker stop gives.

The CI and Docker recipe shows a complete CI job with a broker service.

On this page