New openHAB3 rules Tutorial

Tags: #<Tag:0x00007fc8faa26118> #<Tag:0x00007fc8faa25fd8>

Perhaps I just can’t see the forest for the trees, but is there a tutorial somewhere on how to use the new rules with scripts? I never used scripted rules except some JS-transformations, so I don’t seem to get, how I can use variables like channels or variables in jython or JS - and how I start the rules and whatnot… :wink:

I’m lost and need some intro to using UI-based OH3 script rules which not only change items’ states but use complex logic to achieve something like shutters or the likes! :wink:

To be honest we’re a bit behind with those docs.
I am trying to collect the informations where they are available.
Probably there is already some content in the documentation area.

Currently we are finishing the beginner tutorials which are getting moved from the documentation area too.

1 Like

But you and all others are invited to start with some tutorial/docs page while exploring it and contribute that to the docs. :smiley:

Thanks for the heads up! :wink:
If I can fight my way through, I’m happy to contribute! I’ll give it a try this afternoon…

1 Like

If github is the problem don’t mind to start here in the docs area with a wiki post. :slight_smile:
Copy/paste at a later state to get it in the website is done quick and easy.

There is a good deal of content around but not much in the Getting Started Tutorial yet.

Note that the above is all focused on creating rules through the UI. If you will be writing rules in text files the Helper Libraries docs, in particular the But How Do I… page is great. Note that I don’t think the Helper Libraries are ready for OH 3 yet.

There are Python versions of all of the Design Pattern posts (those not written by me have them in a reply) and I’m in the process of rewriting them using JavaScript UI based rules as well (TimeOfDay and Debounce are the only two I’ve posted so far). You can see what I’ve written and reuse the code itself from https://github.com/rkoshak/openhab-rules-tools. The Python versions depend on the Helper Libraries so are OH 2.5 only for now. The JabaScript libraries do not but are coded assuming OH 3 so are not backwards compatible. Rules (like Time of Day and Debounce) are posted as YAML and can be pasted into a UI rule’s code tab.

2 Likes

Are the scripts in the github repository already compatible with openhab3 ?

There is an open PR for Python that appears to work. I don’t know if there is an equivalent PR for JavaScript.

Ok cool, thx. Python were sufficient for me. seams to be this one

A last (maybe stupid) question :slight_smile:
When I want to write my own rules where is the entry point for the documention? An example:
The “Example rules for a first impression” example.

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

where can I find TypeUID definition (timer.GenericCronTrigger) for GenericCronTrigger and the regarding configuration object, the Syntax “cronExpression” string itself and its parameter, for example when I want to create a channel trigger, without deep diving into the Java Source code.

API parameter for the Configuration constructor is just a map/Hashtable in case of the timer.GenericCronTrigger two strings for key and value. Where I have to look for the correct parameters…

Update: Found this is defined in the trigger handler. Is this the only way to determine those parameters?

That stuff is why I strongly recommend using the Helper Library. Then defining a rule is as simple as:

from core.rules import rule
from core.triggers import when

@rule("My rule name")
@when("Item MyItem changed")
def myFunction(event):
    # your rule's code goes here

If you don’t want to use the Helper Libraries, than at least look at the library code for how it does various things like this.

https://openhab-scripters.github.io/openhab-helper-libraries/index.html

Hopefully the helper-scripts gets soon the oh3 compatibility…Are there plans to integrate such a “lowcode” API directly into openhab?

There is already a PR that makes it work with OH 3.

I believe the plan is to make it available as an add-on.

There is also plans to (just in the discussion stage at this point) to have marketplace where add-ons, rules libraries, and widgets could be distributed. But that’s long term.

1 Like

With the pull request the helper scripts are working :slight_smile: Thank you

git clone https://github.com/openhab-scripters/openhab-helper-libraries
cd openhab-helper-libraries/
git fetch origin pull/376/head:oh3comp
git branch -a
git checkout oh3comp
cd Core
cp -r automation/* /opt/openhab-fresh/conf/automation/

@Race666 Your 3 git commands in the middle can be condensed into a single command:

git checkout oh3-patch