8

I am new to MQTT (and home automation in general, I am much more in the systems and dev side), flashed a WiFi switch (Sonoff Basic), connected it to an instance of Mosquitto and Home Assistant and so far everything works fine.

When monitoring the Mosquitto bus, I see all kind of messages, such as

tele/hass1/LWT Online
tele/home/room1/switch1/LWT Online
cmnd/home/room1/switch1/POWER OFF

I recognize home/room1/switch1 which I defined on my WiFi switch, and the switch then sent some topics prefixed by cmnd (command? that would be surprising as nobody manipulated the switch) and tele (telemetry?). tele/hass1/... is generated by Home Assistant.

Are there any standards or commonly accepted practices for the prefixes?

The MQTT documentation explains how topics are formatted but does not introduce any structure (except for topics beginning with $) so I guess that, best case, it would rather be a best practice (or practice full stop).

  • 1
    You do score points for not starting your topics with a leading /, this is just wrong as it adds a extra null segment to the topic tree which is not needed and makes it slightly harder to use wildcards properly. – hardillb Mar 16 '18 at 20:34
7

In general, no — there aren't any standards for topic naming beyond the MQTT specifications.

There are plenty of opinions about how you should construct your MQTT topics, and not a lot of fixed rules. While this is a bit unsettling when you'd like to know exactly what the best practice is, the lack of strict rules does mean you get a lot of flexibility with an MQTT broker.

As you're using Home Assistant, this narrows things down a bit, but more specifically, the topics you're looking at are specific to your Sonoff switch. The API is described in this wiki:

  • Message: cmnd

    • Issued by: devices shown on the right hand side

    • Intent: control the Sonoff; set configuration; ask for status

  • Message: stat

    • Issued by: the Sonoffs on the left hand side

    • Intent: report back status or configuration message

  • Message: tele

    • Issued by: some Sonoffs (like temperature measuring devices)

    • Intent: report unsolicited telemetry info at periodic intervals

The diagram referred to is here, although it is best viewed in the wiki page linked above.

In general, any hierarchy used will be manufacturer or system specific; Sonoff devices will generally follow a documented MQTT topic structure, and other manufacturers might use something different. Not all manufacturers will document their products well (or at all!) — so take care when buying products.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.