Publishing From Rule to MQTT

Does anyone have a good example?

I can’t get it to work…

reviewed this thread:
https://community.openhab.org/t/using-mqtt-publish-in-rules/7858

Under openHAB 2, have you installed the MQTT action JAR? You ought to then have use of the publish action.

As far as I can remember, publish from rules was not working on openhab 2 when I tried it about a year ago. Have no idea if anything changed since then, but on openhab 1 this is working without problems.

thanks watou and denis -

I do not see the MQTT action under extensions/actions in the paper ui. Is there something I need to do to make it available?

I do have the binding installed and working properly.

If you copy this JAR to your addons folder, and can confirm it works, I will add a PR to make the MQTT action JAR installable from the Paper UI.

…update: submitted https://github.com/openhab/openhab/pull/4499

Copying the JAR file did indeed solve the problem! Thanks!

Alfista, Thank you for publishing this. I am also working on publishing from a rule. Would you be open to sharing the syntax of your rule… to study?

Sure…

rule "MQTT_SLACK"
    when
        Item virtual_sensor_minute changed
    then
        publish("orchestrate","slack/jason","PING")
end

virtual_sensor_minute is just a heartbeat MQTT message I use to keep an eye on the MQTT server, and test rules, etc. I am using MQTTwarn to take the events and publish them to slack.

Hello watou

I’ve made a copy the MQTT bonding and your JAR file into the addons folder.
My mqtt binding work well but I have still the message " JvmIdentifiableElement ‘publish’" in openhab designer and no published post seen on mqtt.

Here my .rules.

'have some ideas for this issue ?


import org.openhab.core.library.types.*
import java.util.Date

rule "Test"
when
Time cron "0 0/1 * * * ?"
then
publish(“mqtt:mymosquitto”,“RF/C/1/4/4”,TimeStamp)
end

Do I have to install to install also the MQTT.persistence ?

The publish action is from the org.openhab.action.mqtt bundle, so you will need that to be installed in your runtime. Even if the Designer complains about it not being present, if the JAR is installed in your runtime, it ought to work.

1 Like

Yes, it work’s even with the Designer error.
Thank’s

1 Like

I have everything working so far. MQTT sensor measurements are displayed in the Basic UI and I can also send MQTT messages via the PublishItem command. But I cannot get a rule to fire a publish command directly which is needed if there is more than one topic I want to publsh to (if i understand correctly).

How can I install this bundle?

My default.rules now contains:

rule "RGBStairs2"
when
  Item stairs_b_mov received update or
  Item stairs_b_mov changed
then
  PublishItem.sendCommand("000#300#0#60")
  publish("mosquitto", "blinds","10")
end

But I get the following errors:
Eclipse Smarthomedesigner says “The method publish(String, String, String) is undefined” and the log-file shows:
[ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘RGBStairs2’: An error occured during the script execution: The name ‘publish(,,)’ cannot be resolved to an item or type.

By the way:
I am running OH2 openhabian on a raspberry Pi1

Does anybody have an idea what I am doing wrong? The PublishItem method works but the publish(string,string,string) does not.

Whay aren’t you using an item with mqtt-binding for outbound messages?
Then you just have to update your item to get send over to mqtt broker…

Exactly. See http://docs.openhab.org/addons/bindings/mqtt1/readme.html#example-outbound-configurations

In the following example you’ll find both versions, items with mqtt binding and direct publishing in rules. Only the MQTT binding and MQTT action installed through Paper UI are needed.

Thank you so much. The problem was indeed that I did not have the MQTT action add-on installed. Now everything works flawlessly!

Thanks, just wanted to confirm both methods work great in OH2.1:
Just a simple practical example for the ones maybe struggling with the exact format

when
  Item Doorbell changed to CLOSED
then
  Light_Corridor.postUpdate("ON")   // updates an item, sending MQTT
  publish("mosquitto", "home/light", "ON") // sends MQTT command
end