Experimental Next-Gen Rules Engine Documentation 4 of : Writing Scripts

A deduction must be made between PaperUI created rules, which these docs cover, and text based JSR224l3 Rules.

I can confirm that these variables worked for me on 2.5 M1.

Look too the JSR223 docs and the helper libraries for specifics on text based JSR223 Rules.

@volfan6415

You should used event.command in JSR223 rules < for Jython decorators.

The event object is in input, you can get it out using input.get("event").

event = input.get("event");
logInfo("Command: " + event.command);

(I’m not very familiar with JS so the syntax might not be exact, but that’s the general idea

@Crazylvan359

Thank you for this! I had seen that the javascript helper libriaries had a method called getTriggeredData(input)

but i had no idea what to pass to it.

Using this code:

var event = getTriggeredData(input);
logInfo("zach 2 " + event.receivedCommand);

My logs output :

2019-06-29 12:31:20.879 [INFO ] [jsr223.javascript ] - |zach 2 2|

Where the second 2 happens to be the command that was passed in.

For others reading this the object that getTriggeredData(input) passes back has the following attributes which can be accessed:

oldState
newState,
receivedCommand
receivedState
receivedTrigger
itemName

These are not language specific. I’ve gone through the code for all of the existing triggers (each trigger provides different event attributes) and documented them…

https://openhab-scripters.github.io/openhab-helper-libraries/Guides/Event%20Object%20Attributes.html

1 Like

This must be in the Docs. Great and useful post! … Actually, the only way to know how to use scripts within NGR.

It’s not the only way nor even the best way. A lot has changed since I started writing these and the UI is being completely rewritten which is largely why I stopped writing this series. It will be picked back up again soon and it will make it into the docs. There is already a really good start to a beginner;s tutorial you can find here.

A whole lot has changed with the JavaScript libraries too, which you can find out more about at Helper Libraries for openHAB Scripted Automation — openHAB Helper Libraries documentation about how to install and use them.

But if you are using anything but Rules DSL in OH 2.X you are using the NGRE. In OH 3, it’ll be just called the Rule Engine and it will also run Rules DSL.

For the specific task of using scripts within NGRE in papaerUI, I’d say it is the best tutorial :slight_smile: .

I am not using he helper lib as it seems to be changing and I don’t want to rely on it yet.

Although I have a good experience with OH, but it was not easy to find out what APIs are available and what are not (I am still trying to find out how to load the logger :sweat_smile:)

I cover that on the 5th page. Click on the link for “Next Step” above for how to get at the actions.

The are no major changes in the helper libraries. I had future proofed them a long while ago for the upcoming package name changes in OH 3.0. There are a few more things due to some deprecations, but OH 3.0 should be a smooth transition for the helper libraries, especially when using the Jython and helper library add-ons. I have some new features coming out though.

Which language?

Found it … thanks!

Will definitely give it a test once I get used to the original API.

Javascript, I think it is the only available default language in NGRE at the moment.
I also found the logger in the next step.

Jython is very easy to install and use. It’s what I’m currently using, though I’m starting to also move into more heavy JavaScript stuff as I’m writing libraries of reusable rules and want to reach as wide an audience as possible. See [beta testers wanted!] Jython addon w/ helper libraries (requires OH 2.5.x).

1 Like

Yup, I’ve seen this one. Would love to try it later as I am learning python.
Currently, Javascript is more than enough as I have good experience with nodejs.

It’s not really a default language… JavaScript (Nashorn) just happens to be currently included in the JDK. Heads up though, since it has been removed in JDK15, which is due for release in September. When OH is configured for other scripting languages, they will also be available in the ‘execute a given script’ options of Actions and Conditions in the UI rule editor.

Meant to say *by default, less installation wins for me :slight_smile:

I wonder if there is a replacement for it?
Won’t this be a problem for OH3 since it is already deprected in JDK11?

Take a look at Jython, it is not completely by default, but with the Binding ([beta testers wanted!] Jython addon w/ helper libraries (requires OH 2.5.x)), It is also worth considdering, that there seems to be far more activity in the community on Jython.

1 Like

Thank you very much for this nice documentation.

Currently I try to get the function rules.runNow("<rule UID>"); to work. but for me it seams that this is not working :frowning: You mentioned this was also not working for you. Is this still the case on your side? Then there is definetiv a bug.

The above works great for me

Hi @rlkoshak, great stuff, however there’s a part where you suggest this:

You will also notice there are a lot of variables that only exist when the Rule is triggered by certain types of triggers. You can test in a script to see if a variable exists using:

// Both with and without library
if(command == undefined) // we know the Rule wasn't triggered by a command if true

It throws me this when running from designer with Ctrl+R:

ReferenceError: "command" is not defined in <eval>

A lot has gone on and changed in the nearly two years since I’ve written the above. I’m not sure those variables are populated the same way in OH 3 any more, or even if they were in the later versions of OH 2.x.

But you should be able to use

if(event.itemCommand === undefined)

I see, is there a recent version of this doc/tutorial/comparison?
I tried, but even the event is not defined when running from the designer.