mqttAction in js in OH3

As I struggled to transform DSL mqtt actions to javascript in OH3, I thought I’d publish my solution:

var Actions = Java.type("org.openhab.core.model.script.actions.Things");
var mqttActions = Actions.getActions("mqtt","mqtt:broker:yourBrokerID");
mqttActions.publishMQTT("yourtopic", "hurray!");

It is much easier than that! For Thing actions, use the actions object, which is available in the default script scope

// no import needed
actions.get("mqtt", "mqtt:broker:yourBrokerID").publishMQTT("yourtopic", "hurray!");

There are a lot more examples here…

BTW, this is the same for OH2.

3 Likes

actions.get(“mqtt”, “mqtt:broker:yourBrokerID”).publishMQTT(“yourtopic”, “hurray!”);

@5iver

This is the way I currently do it too (OH2), and it works. I use Visual Studio Code with the openHAB plugin - is there a reason why action is red underlined by VSC as being an error in the code? Presumably VSC doesn’t know that actions is in the default script scope?

I’m not aware of any work that has been done in the VSC OH extension for the new rule engine, scripted automation, or any of the scripting languages, but @Confectrician may have more information.

Nope. NOthing done for this yet.

We have started some issues and discussion, but since the scripted rules support was (and still is) under heavy changes there were no efforts yet.

I am also still very unlucky with the current solution for openHAB language recognition, which holds me back from investing time in this currently.