OH2 Sending MQTT command programmatically/ rule

Hi all,
I want to send an arbritrary MQTT- command to the MQTT-broker from rules.

In OH1 a command could be send within the rules like this

 publish("mymqtt", "INIT", "1")

with MQTT - topic and payload.
How to achieve this in OH2 ?

Thanks for any hint.

Hi, I still have the same challenge. I already bind the ‘mqtt-action’ but this code:
rule "Phone is ringing"
// fboxRinging is a switch item which switches to ON if call is detected
Item fboxRinging_In changed from OFF to ON
logInfo(“Anrufermeldung”, “Generating caller name message…”)

        publish("mosquitto", "openhab/H801_001/W1", "100")


throws me exception 'the method publish is undefined… So I wait also for someone who may help :wink:

Good to hear that I’m not alone. (Or bad … ?)
I found a small workaround, but all in all not very sufficient.

in mqtt-eventbus.cfg


That forwards ANY MQTT command to the broker.

Created a String item:

String INIT {mqtt=">[mymqtt:INIT:command:xxx:yyy]"}

The ‘xxx,yyyy’ must be there, but is ignored. I have no idea how to get around this.

Then in the rules: (in my rules its the startup rule)

INIT.sendCommand(“Starting OpenHab2”)

That gives the message at MOSQITO:

Client … received PUBLISH (d0, q0, r0, m0, ‘INIT’, … (17 bytes))
Starting OpenHab2

Unfortunately the topic is not changeable as its the item’s name.

All- in- all, it is obviuosly not possible to send arbitrary MQTT commands with MQTT topic and payload. A big step backward on OH2 MQTT binding.


indeed, my actual workging simple background is as follows:
in my *.items I declare a string item, which is modified by the .rule in case of changing, e.g.:
String TEST_FB (gTEST) {mqtt=">[mosquitto:openhab/…:command:
So, in my *.rules I write appropriate:
var String FBText
rule "Phone starts ringing OUTGOING"
// fboxRinging_In is a switch item which switches to ON if call is detected
Item fboxRinging_Out changed to ON
FBText= "Klingeling - Ein Ruf geht raus…"
sendCommand( TEST_FB, FBText)
logInfo(“Anrufermeldung”, “Generating caller name message…”)

All the time, when my variable ‘TEST_FB’ is modified by the rule, the mqtt broadcast fires the appropriate topic. This works fine for me, but I also want to know the possibilites and (may be) advantages of the mqtt-action :wink: