I have OpenHab2 running in an Ubuntu VM. The embedded broker is working well; I’m monitoring all sorts of different things from various topics.
What I’d like to do now is perform an MQTT publish from a switch but I can’t seem to get the syntax correct.
From default.items Switch Lounge_AC_Switch {mqtt=">[mqtt:systemBroker:embedded-mqtt-broker:/loungeaccommand:command:ON:ON],>[mqtt:systemBroker:embedded-mqtt-broker:/loungeaccommand:command:OFF:OFF]"}
From default.sitemap Switch item=Lounge_AC_Switch label="Lounge AC Control"
The log shows:
Item ‘Lounge_AC_Switch’ received command ON
Lounge_AC_Switch changed from OFF to ON
However, no MQTT publish takes place; I’m using MQTT Explorer on a separate machine on the same network and can see all the MQTT messages taking place via the embedded broker, but this one never appears.
I’ve tried creating a channel in my Generic MQTT thing via PaperUI.
I’m missing a step I think, but I can’t find it.
Edit: I’ve tried changing the settings in default.items, where the IP address shown is the local address on the machine OH2 is running on. I’ve also tried 127.0.0.1.
I do not have an mqtt.cfg file because a) I was under the impression the embedded broker didn’t need one and b) everything is working fine. 5 or so remote devices are publishing and subscribing to various topics and the embedded broker is the only one I have, which is working great. This is the first time I’ve wanted OH2 to publish something to it’s own embedded broker in response to a switch in Basic UI.
This is syntax for MQTT version 1.x binding. Do you have that installed? If you are using MQTT version 2 binding (things and channels) for your other Items, you probably want to change this.
My MQTT topic above (loungeaccommand) is an outgoing channel.
The syntax in the manual is only described for Rules. Do I use the same format in my items file? Or do I have to use a rule (for example, if switch state changes to on, trigger the MQTT send?)
That would make sense (I’ve got a test email send switch set up along those lines).
I’ll test it.
I got the impression this was done in the items file, as per my post, maybe because I only found examples of the v1 MQTT binding.
rule "Lounge AC On"
when
Item Lounge_AC_Switch changed to ON
then
val mqttActions = getActions("mqtt","mqtt:systemBroker:embedded-mqtt-broker")
mqttActions.publishMQTT("loungeaccontrol","ON")
end
rule "Lounge AC Off"
when
Item Lounge_AC_Switch changed to OFF
then
val mqttActions = getActions("mqtt","mqtt:systemBroker:embedded-mqtt-broker")
mqttActions.publishMQTT("loungeaccontrol","OFF")
end
I think this is the correct and most effective approach - any feedback would be welcome - thanks in advance for any guidance. I believe the MQTT Things and Channels page could do with a comment that the v2 Binding requires this approach and that the previous v1 way, of placing MQTT commands in items files, will not work. Could be I’m wrong, but I’ve forked and will propose an edit. I’m sure someone will fix it if that’s not the case.
I just did a simple test on my setup. I created a generic mqtt2 thing. Than I added an on off switch channel with a test topic as a command topic and no state topic. I linked this channel to a switch item. Than I added a listener on the topic in nodered and I can see the on off values beeing published everytime I switch the item no problem. I don’t see why you would need the rules for this.
Best regards Johannes