Doubts about calling actions from OH 3.0 rules

To test an addon that I’m developing, targets OH 3.0, and has a trigger channel I created a rule that listens for the trigger event.

The rule was created from the UI and fires correctly. But I have problems with the script part:
My script is:

I have two problems with it:

  • I don’t know how to log. If I uncomment the first line it fails as logDebug isn’t defined
  • I don’t know how to call an action from the addon. What I want is to call the setQueryParameters action from the same thing that fired the event.

For the setQueryParameters part I tried also:

actions.getThingHandler().setQueryParameters(map)
actions.thingHandler.queryParameters = map 

I tried to follow the documentation on https://www.openhab.org/docs/configuration/jsr223.html#javascript.
Is it valid for OH 3.0? Can I find any samples of rules for OH3.0 that calls an addon defined action?

Notes:

  • I’m using one of last daily builds of OH 3.0 to test that
  • Preliminary documentation from the addon that I’m developing (to have more context on the rule)

In JavaScript you need to import the loggers. See Logging — openHAB Helper Libraries documentation and Actions — openHAB Helper Libraries documentation for how to do that with the Helper Libraries and Experimental Next-Gen Rules Engine Documentation 5 of : Actions to see how to get at them logging actions without the Helper Library.

See the MQTT example at Actions — openHAB Helper Libraries documentation for and excample. It would look like

actions.get("mqtt", "mqtt:systemBroker:embedded-mqtt-broker").publishMQTT("test/system/started", "true")

where the first argument to get is the binding, the second argument is the Thing ID, and then you call the method of the argument with the proper arguments.

To my knowledge this has not changes in OH 3, but I don’t know if the Helper Libraries are ported to OH 3 or not.

Thank you very much @rlkoshak

I didn’t know about the openhab-scripters page.
After following installation instructions for Javascript I’ve been able to import the logger and all worked correctly.

The action parts required some more time because I had some error in the action definition, but after putting all the annotations it worked correctly.

One doubt I have, is about the static methods for the addon actions. I hadn’t defined any and it works. The statics are only needed for compatibility with old rule engine / scripts?

I can’t answer that. A developer will have to answer that.

you don’t even need an explicit Java.type in this case with Nashorn (you might prefer including it for clarity though), so this works:.

var logger = org.slf4j.LoggerFactory.getLogger("org.openhab.model.script.Rules");
logger.info("hey!");

For debugging (temporary!) purposes you can also simply use something like print(actions);, this will output something in the console (not in the log) like:

org.openhab.core.automation.module.script.internal.defaultscope.ScriptThingActions@638aa36

Looking at the code of this class in openhab-core’s code, we can gather it has indeed a get method requiring a scope and a thingUID - that’s how I figure out what’s accessible in the default scope:

1 Like