Sending MQTT Publish commands from a rule..

3,086 views
Skip to first unread message

allen

unread,
Jun 24, 2015, 11:33:47 AM6/24/15
to ope...@googlegroups.com
Good day,

I would like to publish MQTT messages from a rule.

The following works.

var String message1="mosquitto_pub -t /yb/bp1 -m ABC-123"
executeCommandLine(message1)


Can anyone tell me why this does not work?

var SimpleDateFormat df = new SimpleDateFormat( "YYYY-MM-dd HH:mm:ss" )
var String Timestamp = df.format( new Date() )
var String message1="mosquitto_pub -t yb/bp1 -m " +Timestamp
            executeCommandLine(message1)

The command looks correct on the openhab console but no message is published to MQTT subscribers. I suspect the sting has a character that MQTT does not like.

Also, can anyone suggest how to create a MqttClient and use the MQTT api to publish message from a rule?

Thank you.
-allen

watou

unread,
Jun 24, 2015, 12:54:12 PM6/24/15
to ope...@googlegroups.com
Hi Allen,

A simpler approach might be to use the MQTT binding, create an Item bound to the broker and topic you've setup, and send the item commands or updates from your rule.  The messages will get published that way.  Example (not tested):

1. Set up the MQTT binding per the wiki page.
2. Create an Item in your .items file:

String PublishItem "[%s]" { mqtt=">[yb://bp1:command:*:default]" }

3. Send state updates from rules:

rule "mqtt publish"
when
 
<your trigger>
then
 
PublishItem.sendCommand("your_data")
end

allen

unread,
Jun 24, 2015, 3:21:20 PM6/24/15
to ope...@googlegroups.com
Thank You.. I first tried this approach, but was unsuccessful. Apparently, I did something wrong. Thanks again for your response.

Greg EVA

unread,
Jun 25, 2015, 10:50:02 AM6/25/15
to ope...@googlegroups.com
The MQTT binding (or persistence) approach mentioned is of course best, and what is I guess semi supported in openHAB.

The Wiki page on MQTT is a bit lengthy and dives into the fact that there are multiple ways to do things depending on what your needs are.  Have a read through the main Wiki page, and the MQTT persistence page to see what is best for you.

https://github.com/openhab/openhab/wiki/MQTT-Binding

That said, if you bind an Item to an MQTT broker and topic, you just need to post an update to that item in the rule and it will publish it for you.  A central idea in OpenHAB is to remove the connection part from the "Event Bus" so that you can just deal with items everywhere and not have to question what it is connected to, which type of network, its address, etc.

Greg
Reply all
Reply to author
Forward
0 new messages