Scripting Rules OH3

I have just found time to do a fresh install from OH 3 coming from OH2.
Now I am investigating the new features.

Currently, I am using in OH2 the rule DSL, now I have seen there exists ECMAScript and Rule DSL.
In the docs I have found that I can write the script with 3 different languages:

  • javascript
  • Jython
  • Groovy

But I don’t find any documentation about this languages for OH.

So what’s the best way now to create scripts in which language?
And can I still create scripts via VSC in javascript/jython or groovy or only inside the UI?

1 Like

There’s no reasonable general answer to this. Anyone has to find out for him/herself.

What about a careful look at the official docs ?
You can also search the forum for ‘helper libraries’.

Anything goes.

So every 3 languages are OH future-proof?
There is no “OpenHab” preferred language?

I have already found this doc, thanks.
But with this doc I can’t create a complete script at all.
This is a small hello world sample.
And as stated on the site “Example rules for a first impression”.
I search for a documentation on methods and so on.

If I use javascript how do I need to construct the script?
How do I get the item state or how can I set it.

I hope you understand what exactly I mean.
A developer documentation from the OH framework.

The same as other frameworks have.

For example:

var sRule = new SimpleRule() {
    execute: function( module, input) {
        print("This is a 'hello world!' from a Javascript rule.");
    }
};

Great, but what objects are “module” or “input” ?
Or what does exactly “SimpleRule” means? Where is the doc of “SimpleRule” class?

Another example:

sRule.setTriggers([
    TriggerBuilder.create()
        .withId("aTimerTrigger")
        .withTypeUID("timer.GenericCronTrigger")
        .withConfiguration(
            new Configuration({
                "cronExpression": "0 * * * * ?"
            })).build()
    ]);

How do I know what the object TriggerBuilder needs?
What exactly is a “new Configuration”?

I think it is know clear what I mean :wink:
A simple API Documentation.

Go read all the docs, search the forum and read more, start trying out little scriptlets, debug them yourself and you’ll learn, step by step.

You don’t expect anyone to do 1:1 consulting on all of this, for free, do you ?

What do you exactly mean?
I don’t want a consulting for free or anything else.

I have only asked if there is an API documentation.
With an API documentation, I can read the docs and can start to write my scripts.
That’s it.

But of course it is possible to search the forum and ask questions in the forum and so on.
This is always needed for advanced things, but for the real basics?

Anyway, thanks for your feedback.

Crystal ball failure here.
Whatever works in OH3 will continue to work in OH3.

1 Like

There is no preferred OH language. They all have advantages and disadvantages. There are also more than just those four languages available and two different (and incompatible) versions of JavaScript. It’s all hopelessly fragmented. The documentation is not uniform. There is really no such thing as future proof.

All interaction between OH and the rules for the JSR223 languages (the three you listed) takes place through Java Classes and Objects. So they pretty much all work the same with the main difference being the syntax of the individual languages. Given that…

https://www.openhab.org/javadoc/latest/org/openhab/core/automation/module/script/rulesupport/shared/simple/simplerule

Which shows execute is inherited from SimpleRuleActionHandler (openHAB Core 3.2.0-SNAPSHOT API)

Which shows that module is Action (openHAB Core 3.2.0-SNAPSHOT API) and input is a java.util.Map<String, Object>.

No matter which of the three languages you are using, you need to create an instance of the Java Class SimpleRule and override the execute method which contains the code that will be executed when the rule is run.

The documentation is there once you realize that it’s all mostly working with Java.

It becomes much easier when using the UI to define rules because most of the effort spent in a text based js or py rule file is spent just defining the rule. In the UI all of that is handled for you and all you have to mess with is that execute method.

https://www.openhab.org/javadoc/latest/org/openhab/core/automation/util/triggerbuilder

https://www.openhab.org/javadoc/latest/org/openhab/core/config/core/configuration

There is no simple API Documentation.

1 Like

Thank you very much for your great and useful answers.