Send a mqtt command


I need to send this command to a mqtt broker


I trye’d to type this code in:


but this is not working.

could it be wirtten in a rule

please be sure, you understand, how MQTT works - I found the Tutorials on hivemq helpful:

After that, you will see, that you have to do this configuration within openHAB2:

  1. add your broker with a name to mqtt.cfg (e.g. replace <broker> with a decent name there)
  2. use that name (NOT <broker>!) in your items-file
  3. make sure, your configuration is correct (watch logfiles closely!)
  4. send the payload right

ad 3/4:
please see openHAB documentation on MQTT:

Item myItem {mqtt="<direction>[<broker>:<topic>:<type>:<transformer>:<regex_filter>], <direction>[<broker>:<topic>:<type>:<transformation>], ..."} 

everything in angle brackets has to be your configuration. (e.g. <broker> should read your name of the broker.

Please feel free to come back, if further problems arise.

Try to avoid a trailing “/” in your topic, this makes the main Level of your topic NULL, which makes it harder to use wildcards and other stuff, see:

1 Like

Additionally here is the wiki thread about running mqqt with/in openHAB.

Maybe we cuold check, if we should add anything to the wiki post, that you have written here,
to improve the wiki again. :slight_smile:

I already have my broker up and running with other items. But this problem is that I can not insert {}][ in to my code, because it thinks it is a part of the code. Not the part I need to sent.

Is there a way to send a MQTT command in a string item or something else?

If you do make a string of it, simply by adding the items as:

sendstring="/“state/”"+":"+ etcetera
and send that. Is that perhaps a possibility?

I do not understand, where do a type this in.
could you type a complete string for a example

I do not know any of yr applications but you could no doubt make such a string in a rule.

So you create an item that you simply switch on or off and then have a rule check the state and send that full string

take a look here:
you can transform your ON / OFF with e.g. JSON or simply with a MAP, and adjust so the payload, your Switches expect.

or you can do like Kees just suggested: make a rule, which just sends the payload if the item changes:

publish(String brokerName, String topic, String message)

1 Like

I tryed to make a string item, it works if I send a clean test message to the string w2811 from a rule or sitemap, but when I insert my code to the rule/sitemap or send it direct to the w2811 string it not work.
My OH2 log tell me there is a problem with my code because I use characters like these " } )( "

so it does not see the difference to the text I’m going to send and the code for the item/sitemaps/rules itself

My code i need to send:


My item:

String          w2811                           "test"                          (test)                                  {mqtt=">[<broker>:/bruh/porch/set:command:*:default]"}

Sitemap code

Switch item=ArduinoGateway mappings=[,{"state":"OFF","color":{"r":255,"g":255,"b":255},"brightness":255,"effect":"confetti"}=OFF,{"state":"ON","color":{"r":255,"g":255,"b":255},"brightness":255,"effect":"confetti"}=ON]

Rule code

rule sendMQTT
  Item w2811 changed

Writing JSON directly in the item configuration won’t work. I’m on my mobile, but you have to use a transformation (see my above link).
Basically you tell the transformation to fire and it will then send the JSON for you. Should work,I think. All the transformation had to do is to insert ON. And I guess OFF works the same way?