Senzorv1.0.1

Editing templates

Change a template's settings, messages and fields, and check the result in the live preview.

Open the Templates tab in the left panel and click a template under Project to edit it. Add template creates a working template called template, with one message on senzor/{{ device_id }}/value and one random_walk field named value, and selects it.

The template editor showing office-air with its preview

The editor has three sections, Settings, Messages and Fields. Click a section's title, or the arrow before it, to fold it away; click again to open it. When the detail panel is at least 900 points wide, the preview sits in a panel to the right of the form, where it stays in view while you scroll; in a narrower window it is the last section, under Fields. Drag the panel's edge to change its width.

Every change goes into the project as you make it, and Senzor validates the project again. A problem with one value shows in red under that value's box, which gets a red outline; other problems with the template appear in red under the heading. Running instances take a change at once, except a change to the availability, which waits until you restart them (Editing while running).

Duplicate template copies the template, with its messages, fields and all its other settings, right after the original, and selects the copy; Duplicate in the template's right-click menu in the Templates tab does the same. The copy's ID is <id>-copy, or <id>-copy-2, <id>-copy-3 and so on when that is taken. Devices keep using the original.

Delete template, or the Delete key while the template is selected, removes the template from the project without asking; Ctrl+Z brings it back. Devices that still use it then report an unknown template in the Problems tab.

Settings

LabelWhat it sets
IDThe template's ID, which devices refer to.
IntervalHow often instances publish, as a duration such as 10s, 500ms or 1h.
QoSThe QoS of the data messages: 0, 1 or 2.
Retainretain data messages sets the retain flag on the data messages.
Home Assistantannounce over MQTT Discovery makes each instance appear in Home Assistant, with one entity per field. Below it, the editor says Every field is announced., or names the fields it cannot find in a payload. See Home Assistant discovery.
Availabilityonline/offline topic with Last Will turns on the availability topic and its payloads.

Renaming a template

Type the new ID into the ID box. Nothing changes while you type; the new ID applies when you press Enter or leave the box. If the ID cannot be used, the reason appears in red next to the box, for example when it is empty, already taken, or the ID of a built-in template. Devices that use the template follow the rename, so they keep working.

Durations

The Interval box takes a duration like 10s, 500ms or 1h. Text that is not a duration stays in the box with the hint use e.g. 10s, 500ms, 1h, and the interval keeps its previous value until you correct it.

Availability

Ticking online/offline topic with Last Will adds three boxes, filled in with a starting point:

  • Topic: senzor/{{ device_id }}/status
  • Online payload: online
  • Offline payload: offline

A fourth box, Will delay, is empty (none) by default. With MQTT 5, a duration there, in whole seconds, makes the broker wait that long before it publishes the Last Will; it needs a broker Session expiry at least as long. Availability and Last Will explains when each payload is sent.

Messages

Each message is a group named Message 1, Message 2 and so on, with a Topic box, a multi-line Payload box and a Remove button. Add message adds a message on senzor/{{ device_id }}/value with the payload {{ value }}. Change the payload to use your own fields: a template without a field named value cannot render it. Topics and payloads are templates; the template variables reference lists what you can use in them.

Writing topics and payloads

The Topic and Payload boxes, and the availability Topic, colour what you write: {{ … }} and {% … %} blocks in the accent colour, and JSON strings, numbers and punctuation in colours of their own, so a missing quote or brace stands out.

The { } button after a box's name lists the variables that box can use: device_id, index and template_id; in a message also seq, now_ms, now_iso and the template's fields; and the params of the project's devices that use the template. Hover over one for what it holds, and click it to insert it as {{ name }} where the cursor was, or at the end. The availability topic is rendered once, when an instance starts, so its list has no fields and no time.

While you type a name inside {{, the box lists the variables that start with what you typed, under the box after Tab:. Press Tab to take the first one, or click one; Senzor completes the name and adds }} unless the block is already closed.

Encoding

Under Payload, Encoding says how the rendered payload is published: text, the default, sends it as it is; hex and base64 read it as bytes written in hex digits or in base64; CBOR and protobuf read it as JSON and send it in that binary format. See Binary payloads.

With protobuf, two more boxes appear:

  • Proto file: the .proto file that defines the message type, such as proto/sensor.proto. A relative path starts at the folder of the project file; while the project has no file, at the folder Senzor was started in.
  • Message type: the fully qualified type, such as sensors.v1.Reading.

Their problems show under them, such as message 1: encoding protobuf needs a proto file. Choosing another encoding clears both boxes.

MQTT 5 properties

Under each message, MQTT 5 properties opens three more settings. The section starts open when the message sets any of them.

  • Message expiry: how long the broker may keep the message for subscribers that have not received it yet: whole seconds and at least 1s, like 5m. Empty (shown as none) means no expiry. Text that is not a duration shows the hint use e.g. 10s, 1m, 1h.
  • Content type: sent as written, for example application/json. Empty means none.
  • User properties: name and value pairs. Add user property adds a row named name; Remove property removes one. A new name applies when you press Enter or leave its box. An empty name, or one the message already has, shows the reason in red while you type, and the box goes back to the old name when you leave it. The values are templates with the same variables as the payload, so {{ room }} sends the device's room param.

With MQTT version set to 3.1.1 in the broker settings, these settings and Will delay are disabled with the note needs MQTT 5; the broker settings use MQTT 3.1.1 (inside the opened MQTT 5 properties section, and next to Will delay). While a message still sets any property, its whole section stays editable so you can clear it, and Will delay stays editable while it has a value. Until you clear them, the Problems tab lists them and the template's devices cannot start.

Fields

Each field has a name box, a generator picker, a Remove button, Name and Unit, and the generator's parameters. Add field adds a random_walk field named field (start 50, min 0, max 100, step 1).

Field names work like the template ID: a rename applies when you press Enter or leave the box. Devices that override the field keep their overrides under the new name.

Name is a readable name for the field, such as CO2, and Unit the unit of its values, such as ppm, °C or %. The instance view shows them; they do not change what instances publish. Leave a box empty for none: the instance view then shows the field's key and the value alone. They are saved as name and unit in the field's table (project file reference).

The generator picker offers constant, random_walk, sine, uniform, sequence and script. Choosing another generator replaces the field's parameters with that generator's defaults; a new script field adds 1 to its value on every tick. The editor shows these parameters:

GeneratorParameters
constantvalue, noise
random_walkstart, min, max, step, noise
sinecenter, amplitude, period, phase, noise
uniformmin, max, noise
sequencevalues, loop, noise
scriptstart, noise, and script below
  • period and phase are durations, like Interval.
  • noise is a checkbox. Ticking it adds noise of 0.1, which you can then change.
  • values takes a comma-separated list such as open, closed. Each item is read as a number if it can be, then as true or false, and otherwise as text. Empty items are skipped.

What each parameter does is in the generators reference.

A script field has a start box, the value the script sees as value on the first tick, which takes a number, true, false or text like value does, and a script code box under the row for its Rhai script. A script that does not compile is listed in the Problems tab with its line and column.

Subscriptions

The Subscriptions section lists the topics each instance subscribes to, and the script that runs for every message that arrives; see Scripts and commands. Each subscription has:

  • Topic: the topic filter, which may use + and #, with the same variables as the availability topic, such as {{ device_id }}; the {} button inserts one.
  • QoS: 0, 1 or 2, the QoS the instance subscribes with.
  • Script: the Rhai code run for each message.

Add subscription adds one on senzor/{{ device_id }}/set whose script keeps the last command in state.last_command; change both to what your consumer sends. Remove next to Subscription 1 removes it. Problems with a topic, its QoS or its script show in red under the box they are about.

Typed values

The value of a constant field has a type selector next to its box: text, number or bool. Typing never changes the type, so 007 typed into a text value stays the text "007" and does not become the number 7.

  • A number value only accepts a number. Anything else stays in the box with the hint use a number, e.g. 42 or 22.5, and the field keeps its previous value.
  • A bool value only accepts true or false (hint use true or false).
  • A text value keeps exactly what you type, spaces included.

To change the type, pick it in the selector. Types that the text in the box does not fit are disabled; hover over one to see why. Device params use the same selector (Editing devices).

Preview

The Preview, beside the form or at the bottom (see above), renders the template's topics and payloads as you edit, before you save. It renders them as the first instance that uses the template would, with that instance's params and field overrides, and says so: Rendered as office-0 with its params and sample values. In the showcase project, the state topic of office-air renders as b/office/office-0/state.

The sample value of each field is its starting value: the constant value, the random_walk start, the sine center, the uniform min, the first sequence value, or the script start.

If no valid device uses the template, the preview says No valid device uses this template, so params are not available. This is also the case while the template itself has problems, since its devices cannot run then. A template that uses params then shows an error for the missing param.

The preview shows the availability topic (as availability: …) and each message's topic and payload, followed by the MQTT 5 properties the message sets, such as content type: application/json, message expiry: 300 s and user property: room = office. A message that fails to render shows its error in red, such as message 1: ….

For a message with a binary encoding, the preview shows the rendered text, then its size, such as 5 bytes, and the bytes it encodes to as hex, 16 bytes a line after their offset. When the text does not encode, the reason shows in red instead, such as not encoded as hex: odd number of hex digits (5).

When a rendered payload starts with { or [ but is not valid JSON, the preview warns under it in amber: looks like JSON but is not valid JSON, followed by where the JSON breaks, such as a comma before }. A message whose Content type names JSON, such as application/json, gets the same check for any payload, and so does a message with the encoding CBOR or protobuf, which must be JSON: encoding CBOR needs JSON, but the payload is not valid JSON. The warning is not a problem: the device still runs, so you can publish broken JSON on purpose to test how your consumer copes.

Built-in templates

Click a template under Built-in to look at it. Built-in templates are read-only: the view shows the template's settings greyed out, with its preview, and the note Built-in templates are read-only. Copy one into the project to change it.

To change one, click Copy into project…. The dialog asks for a New template ID and suggests one, such as co2-copy (or co2-copy-2 when that is taken). Click Copy, and the copy appears under Project and opens in the editor. Devices that use the built-in keep using it; switch their Template to the copy in the device editor. A project template cannot reuse the ID of a built-in template.

The built-in templates reference lists what each one publishes.

On this page