Linking a switch to an MQTT publish

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

Switch Lounge_AC_Switch {mqtt=">[mqtt:],>[mqtt:]"}

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.

Any advice much appreciated, thanks.

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.

Thanks for replying!

I don’t actually know which binding I’m using. I followed instructions to get the embedded broker going, then added the binding I found first.

Under Bindings it’s just listed as MQTT Binding.

Is there a way to tell?

Or is the fact I have a Generic MQTT thing with lots of channels (I add one for every new topic I need) indicative of version 2?

If it’s version 2, are you able to point me in the direction of the correct syntax to use?

Thanks again.


For this use, you have to create your outgoing channel instead of finding an incoming one.

1 Like

Thanks again.

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.

Working now!


Switch Lounge_AC_Switch "Lounge AC Control"


Switch item=Lounge_AC_Switch


rule "Lounge AC On"
  Item Lounge_AC_Switch changed to ON
  val mqttActions = getActions("mqtt","mqtt:systemBroker:embedded-mqtt-broker")

rule "Lounge AC Off"
  Item Lounge_AC_Switch changed to OFF
  val mqttActions = getActions("mqtt","mqtt:systemBroker:embedded-mqtt-broker")

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



My topic is a text value, but since you can specify the value ‘sent’ by the switch I didn’t think that mattered.

I’ll test again using your method to see if that works.