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

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.

Maybe you need to describe what you mean by ā€œDesignerā€. If you mean the old old old IDE for openHAB called openHAB Designer, that hasnā€™t worked nor has it been maintained in years. It never was updated to fully support Rules DSL in OH 2.0, let alone the JSR223 languages.

Also, it never let you run the rules independently.

If you are talking about MainUI, event only exists if you trigger the rule from an Item event. If you are running it by hand I donā€™t know if there is a good way to tell. Maybe you can test to see if this.event === undefined.

Thanks, Iā€™m on OH3 latest milestone, and weā€™re talking about the main UI / rules / edit script.
Finally the this.event did the trick, thanks again for the help.