Robert Poor <
rdp...@gmail.com> writes:
> Is there a general convention for naming topics? In particular, consider
> these three cases in a device that has a set of relays. Note that the
> relays may not change at the moment that they are asked to:
>
> 1. payload contains a request to set the relays to a given state.
> 2. payload contains the current state of the relays
> 3. payload is empty, message is a request to report the current state of
> the relays
You are running into "While MQTT is a protocol, saying 'use MQTT' is not
a protocol specification, even though people act like it is."
It sounds like you are writing code for both sides, so you can of course
do anything you want.
I would suggest looking into home automation projects and aligning with
what they do. That's useful because their scheme has almost certainly
worked for a lot of people, and because you might want to integrate with
such a program.
You are talking about a set of relays, so one obvious question is if you
want a single logical entity with a bit vector of relays, or a set of
entities each with one relay. If the relays can get hooked up to
different things, vs being intrinsically linked, I would suggest
modeling them as separate entities.
I use Home Assistant (HA), and the concept that matches a relay is
"switch". Note that when reading the HA docs the "switch" is the
logical entity in HA, and it represents the physical entity.
One can create an "MQTT switch" device with homegrown code, or with
things like ESPhome or ESPeasy.
https://www.home-assistant.io/integrations/switch.mqtt/
In my case, I have a switch entity which is not in the same place as the
HA instance. It's good to think about what happens as the control or
the device is restarted and with or without power independently when
designing your control protocol.
Physically, the switch is an ESP8266 with a GPIO hooked to the control
outlet of a power strip with a relay, and there's a light plugged into
the power strip. The ESP8266 runs nodemcu with a bit of hand-written
Lua code. My config* is literally (with real name switched to foo):
switch:
# FOO
- platform: mqtt
name: "Foo main light"
state_topic: "sensor/foo/main/light"
command_topic: "sensor/foo/main/light/set"
#availability_topic: "sensor/foo/main/online"
retain: true
* This is out of date; now one uses top-level mqtt and then switch, but
that's irrelevant to the present discussion.
The device accepts commands on ../light/set and reports state to
../light.
This sets retain so that messages to ../set have the retain flag. That
means that if the ESP8266 powers up or reconnects after the light has
been turned on or off, it will be in the right state. Retained topics
are useful, but you can't tell when the device is gone vs when it just
hasn't said anything in a long time. The ESP8266 sets retain on status
messags.
The ../online topic is ON if the ESP8266 is connected to the broker;
it's not about the relay. The point, were it enabled, is to have the
light control in HA greyed out if the ESP8266 is offline. This is done
by posting ON on connect and setting a lwt to post OFF.
Another thing to look is ESPHome, which is code to do what I'm doing
where you just configure it rather than writing code. It has native HA
integration, not on point here, and MQTT where it also publishes
metadata for autoconfig. What's useful for this discussion is the
topic protocol it uses.
https://esphome.io/components/mqtt.html#mqtt-component-base-configuration
I quote:
state_topic (Optional, string): The topic to publish state updates
to. Defaults to
<TOPIC_PREFIX>/<COMPONENT_TYPE>/<COMPONENT_NAME>/state.
command_topic (Optional, string): The topic to subscribe to for
commands from the remote. Defaults to
<TOPIC_PREFIX>/<COMPONENT_TYPE>/<COMPONENT_NAME>/command.
so this uses foo/state and foo/command, whereas I am using foo and
foo/command. That's close, though.
(There is complexity about availability topics. One can use ON/OFF like
main entities, or online/offline. I prefer to use ON/OFF as I view the
availability_topic as first a binary sensor for "is the device
connected" and secondarily to grey out the child entities of the
device.)
All in all I would suggest you align with ESPHome if there isn't a good
reason to do otherwise. It probably has the biggest mindshare in
defining this admittedly arbitrary convention.