Using MQTT publish in rules

Hi all,

I am trying to use the MQTT publish action in rules (as described here: https://github.com/openhab/openhab/wiki/Actions#mqtt-action ), but can’t get it to work. The rule looks like this:

rule "Ping djecja"
when
Time cron “0 0/1 * * * ?“
then
postUpdate(Status_gDjecja, OFF)
publish(“server”,”/home/djecja/command/ping”,“PING”)
Thread::sleep(1000)
postUpdate(String_Status_gDjecja, Status_gDjecja.state.toString)
end

Where the “server” in the publish action is defined in openhab.cfg as:
mqtt:server.url=tcp://localhost:1883

Mqtt binding is working, I can send and receive messages to switches using the binding, but I cant publish an MQTT message from the rules.

The log file when the rule fires:

2016-02-22 14:20:00.028 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Ping djecja
java.lang.RuntimeException: The name ‘publish(,,)’ cannot be resolved to an item or type.
at org.openhab.model.script.interpreter.ScriptInterpreter.internalFeatureCallDispatch(ScriptInterpreter.java:67) ~[na:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateAbstractFeatureCall(XbaseInterpreter.java:658) ~[na:na]
at sun.reflect.GeneratedMethodAccessor45.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_95]
at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_95]
at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
.
.
.

Any ideas?

Go to the forum search button and type “mqtt rules publish”, there are a number of hits which may help you.

Good luck.

I already searched for the answer, but could not find anything. There is no single reference that I can find in regard to the publish action for MQTT binding that was introduced in version 1.8. Of course, there are other ways to do what I want, like updating a state of an item, but I want to use the publish action since this is there just for this purpose, and I cant get it to work.

Ahh, okay, didn’t know it was that new.

According to your linked example there shouldn’t be quotation marks, did you try:

publish(server,/home/djecja/command/ping,PING)

?

And I assume you have the org.openhab.action.mqtt-1.8.0.jar in your addons folder …:smiley:

I tried with and without quotation marks, without the marks, the error in log is a bit different:

2016-02-22 13:39:00.084 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Ping djecja
java.lang.RuntimeException: The name ‘server’ cannot be resolved to an item or type.
at org.openhab.model.script.interpreter.ScriptInterpreter.internalFeatureCallDispatch(ScriptInterpreter.java:67) ~[na:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateAbstractFeatureCall(XbaseInterpreter.java:658) ~[na:na]
at sun.reflect.GeneratedMethodAccessor45.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_95]

And yes, I am using the 1.8 version :slight_smile:

Hmm, just a wild guess (because I never used the mqtt action):

Maybe you need both, the binding AND the action in your addons folder?

1 Like

Your wild guess is very good. I did not know until now that I need an addon for actions in rules. Thank you, everything is working now :slightly_smiling:

Great!

For future reference: did you use quotation marks or not?

I did use quotation marks, that is the correct way.