Sending with a rule a MQTT message with publishMQTT in OH3.4.4

  • Platform information:
    • Hardware: VMWare on synology
    • OS: debian 12
    • openHAB version: 3.4.4
  • Issue of the topic:
    I want to publish periodically shellies/command (“announce”)

My rule looks like this:

var Actions = Java.type(“org.openhab.core.model.script.actions.Things”);
var mqttActions = Actions.getActions(“mqtt”,“mqtt:broker:c6a43xxxxx”);
mqttActions.publishMQTT(“/shellies/command”, “announce”);

I get following error in the log.file, when I run it manually…

2023-07-16 19:32:37.964 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘b12442f47f’ failed: TypeError: null has no such function “publishMQTT” in at line number 3

I tried also below …

actions.get(“mqtt”, “mqtt:broker:c6a436xxx”).publishMQTT(“test/test”, “hurray!”);

and got this error in log…

2023-07-16 19:39:01.764 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘b1244xxxxx’ failed: TypeError: null has no such function “publishMQTT” in at line number 5

Could anybody please give me a hint? Do I need to import PublishMQTT or install it somehow?

BTW… I have installed following under bindings “other”

  • mqtt.actions
  • mqtt.bindings
  • mqtt.event bus subsription
  • mqtt.online status

As the error stats NULL has no such function… do I need to declare actions first somehow?

I am following this tutorial… btw…
https://openhab-scripters.github.io/openhab-helper-libraries/Guides/Actions.html

I found the solution to my problem! I need to copy the MQTT-ID from the MQTT-THING and not from the bridge!

==> I hope this helps somebody… the code I wrote is correct and works.