Use case: i want items to report their state to a single MQTT topic, and update their state when receiving commands from other clients. For example, a bulb which sends its current state every 60 seconds, but with 4 people who are able to send commands to that topic to change the color.
Let’s say the topic is: bulbs/01/color
and i have CLIENT_A
, CLIENT_B
, CLIENT_C
, and CLIENT_D
. OpenHAB is CLIENT_A
.
In the MQTT binding, i have a generic MQTT thing (“MQTT Transmitter”) which connects to a remote Mosquitto MQTT broker. Inside that there is a channel (“Bulb 01 Color Signal”) which follows a linked Item bulb_01_color
.
MQTT State Topic: bulbs/01/color/state
MQTT Command Topic: bulbs/01/color/command
In the overview, it says:
Let’s assume there is an MQTT capable light bulb.
It has a unique id amongst all light bulbs, say “device123”. The manufacturer decided to accept new brightness values on “device123/brightness/set”. In openHAB we call that a command topic.
And now assume that we have a mobile phone (or openHAB itself) and we register with the MQTT broker, and want to retrieve the current brightness value. The manufacturer specified that this value can be found on “device123/brightness”. In openHAB we call that a state topic."
Easy enough. The device subscribe to new brightness values on /set
and publishes values on /brightness
.
The State topic guidance says
“An MQTT topic that this thing will subscribe to, to receive the state. This can be left empty, the channel will be state-less command-only channel.”
So the State topic is being subscribed to, i.e. receives INBOUND messages.
The Command topic guidance says
“An MQTT topic that this thing will send a command to. If not set, this will be a read-only switch.”
The Command topic is a publisher, i.e. sends OUTBOUND messages.
This seems to be the opposite of the way it’s set up in the MQTT thing.
When OpenHAB (CLIENT_A
) makes a change through a command (e.g. ON/OFF), it will publish ON
to the Command topic (i.e. “here is a command received through PaperUI”). I need the bulb to report ON
to the State topic immediately afterwards.
Then, CLIENT_B
, (or C, or D) a totally different command line client (say mosquitto_pub) wants to send a change. If you send it to the Command topic, it doesn’t update. If you send it to the State topic, it doesn’t update either.
So following on from that, if you tick isCommand
and publish the update to the State topic, it will change the bulb’s state. It acts on messages it subscribes to (i.e. treats them as a “write” command).
The guidance says:
“If the received MQTT value should not only update the state of linked items, but command them, enable this option.”
What’s the difference between updating a linked item, and commanding it? Does updating the OpenHAB item’s state send a command to the physical device?
Here’s the problem: i don’t want to use 2 topics. I’d like one single sync topic: bulbs/01/color
. Ticking isCommand
places the entire thing into an infinite loop. When the item is given a command (e.g. changing it in PaperUI), it will publish it to the topic, and the MQTT subscriber binding (state topic) will interpret it as a command, thereby generating another publish message as a command, which the State topic will treat as a command, which then generates another command, and so on. You have an infinite loop where subscribing and publishing gets messed up. It doesn’t seem to differentiate between clients.
The terminology is incredibly confusing here.
I need OpenHAB to:
- subscribe to incoming commands on an MQTT topic (e.g. from external clients)
- publish all internal commands received on an MQTT topic (e.g. from PaperUI)
- publish all outgoing state changes to a topic, regardless of how they were triggered.
How is this supposed to work with isCommand
? Is there a way to do this without duplicating channels in the MQTT Thing?