MQTT
Devices can publish their readings to an MQTT broker, and Sensora reads them from there. This page shows how to set that up and says what is protected and what is not.
MQTT needs two steps outside the dashboard
In version 0.1.27 the dashboard does not offer MQTT. New integration creates HTTP webhooks only, and Connect a device offers HTTP only. Also, the port of the bundled broker is closed to your network.
To use MQTT you create the integration with the API and open the broker port yourself. Both steps are on this page. After that, the integration's page and its inbox work in the dashboard like those of a webhook.
How it works
- The stack includes a broker, NanoMQ. Sensora connects to it as a client and only listens. It never publishes.
- An MQTT integration has a topic pattern with the placeholder
{device_uid}in one level, for examplesensors/{device_uid}/up. - When a message arrives, the text in that topic level is the device id. Sensora looks for a device whose External ID or
device_uidis equal to it, records the message in the integration's inbox, and stores the readings.
The Sensora Agent firmware and the Arduino library do not use MQTT. They send over HTTP.
Set it up
Create an MQTT integration. Pick a name of lowercase letters, digits and dashes, and replace
192.168.1.20with the address of the computer that runs Sensora.bashcurl -X POST http://localhost:8090/api/integrations \ -H 'Content-Type: application/json' \ -d '{"name":"mqtt-sensors","transport":"mqtt","platform":"custom","config":{"topic_pattern":"sensors/{device_uid}/up","broker_url":"mqtt://192.168.1.20:1883"}}'Sensora starts to listen on the new pattern within 30 seconds. Wait that long before you publish. A message that is published earlier is lost.
broker_urlis only a note for you. It is shown on the integration's page, and Sensora does not connect to it. If you leave it out, the page showsmqtts://broker:8883, which is not a real address.If an integration with this name exists, the request replaces its settings. The demo data already has an integration named
mqtt-main.Create a device that belongs to the integration:
bashcurl -X POST http://localhost:8090/api/devices \ -H 'Content-Type: application/json' \ -d '{"name":"tank-level-01","integration":"mqtt-sensors"}'json{"device_uid":"dev_70b275ab","secret":"sec_345b3bc2a9f02b016146ff3eedd388d6fea56daad9b1f5f4"}The topic uses the
device_uid. The secret is not used on MQTT.Publish a test message from inside the broker container. This works before you open the port:
bashdocker compose -f deploy/docker-compose.yml exec broker \ nanomq_cli pub -h 127.0.0.1 -p 1883 -q 1 \ -t 'sensors/dev_70b275ab/up' \ -m '{"level_cm":132.5,"temperature":18.2}'Check the result. Open Integrations in the Developer group of the sidebar and open
mqtt-sensors. The inbox shows the delivery as Processed with2 datapoints → tank-level-01, and the device shows Online.Open the broker port so that your devices can connect. See the next section.
Open the broker port
In the default stack only the web port (8090) is open on the host. The broker listens on port 1883 inside Docker, where only Sensora reaches it.
An open broker port has no protection
The bundled broker has no user names, no passwords and no TLS. Anyone who can reach port 1883 can publish readings as any device and can read what all devices publish. Open the port only on a private network that you trust. For anything else, use your own broker.
Add the two ports lines to the broker service in deploy/docker-compose.yml:
broker:
image: emqx/nanomq:latest
restart: unless-stopped
ports:
- "1883:1883"Then apply the change:
docker compose -f deploy/docker-compose.yml up -dDevices now connect to port 1883 of the computer that runs Sensora, without a user name, and publish to their topic.
Topic and payload
| Part | Rule |
|---|---|
| Topic | Must have the same number of levels as the pattern. With sensors/{device_uid}/up, the topic sensors/dev_70b275ab/up matches and sensors/dev_70b275ab/up/extra does not. |
| Device id | The whole topic level at the place of {device_uid}. It must be equal to the External ID or the device_uid of a device. |
| Payload | One JSON object. |
| Values | The same rules as HTTP. Without a template, every top-level number and boolean is stored. With a template, the fields it lists are read from their source paths. See What becomes a metric. |
| Time | The time at which Sensora received the message. A recorded_at field is not used on MQTT. |
| QoS | Sensora subscribes with QoS 1. A message that the broker delivers a second time within 60 seconds is not stored twice. |
What happens when a message does not fit:
| Case | Inbox status | What to do |
|---|---|---|
| No device has the id in the topic | Unmatched | Create the device with that id as External ID, then replay the delivery. See The inbox. |
| The payload is not JSON, or has nothing to store | Failed | Fix the device or the template, then replay. |
Devices with their own topic layout
You choose the topic pattern, so devices with a fixed layout can publish without changes. The template library has templates for these:
| Device | Topic pattern | External ID of the device |
|---|---|---|
| Shelly Plus H&T, Plus Plug S, Pro 3EM | {device_uid}/events/rpc | The Shelly device id |
| Tasmota | tele/{device_uid}/SENSOR | The Tasmota topic name |
Use your own broker
A broker that you run yourself can have user names, access rules and TLS. Set its address in deploy/.env and run the up command again:
MQTT_BROKER_URL=mqtt://192.168.1.30:1883
MQTT_USERNAME=sensora
MQTT_PASSWORD=choose-a-password| Variable | Meaning | Default in the stack |
|---|---|---|
MQTT_ENABLED | Set to false to turn MQTT off | true |
MQTT_BROKER_URL | The broker that Sensora connects to | mqtt://broker:1883 |
MQTT_USERNAME, MQTT_PASSWORD | The login that Sensora uses on that broker | none |
On your broker, give each device its own login and allow it to publish only to its own topic. Give the Sensora login permission to subscribe to the topic patterns of your integrations. Then the broker decides who may publish as which device. Sensora itself does not check who sent an MQTT message.
Sensora connects to one broker. All MQTT integrations listen on it.
Related pages
- Webhooks: the inbox, replay and auto-provision work the same way for MQTT integrations.
- Templates: read nested JSON such as
state.temperature. - Configuration: all variables.
