[OH3] How to Create Rules in OpenHAB 3 using JavaScript / ECMAScript

Hi
I just started writing blog articles about OpenHAB 3 on my new blog (Smart)²Home.
Here is a link to the blog: (Smart)²Home – Medium
And here is the article How to Create Rules in OpenHAB 3 using JavaScript / ECMAScript:
How to Create Rules in OpenHAB 3 using JavaScript / ECMAScript | by Johannes Schildgen | (Smart)²Home | Dec, 2020 | Medium

If you like more articles, write me topics you are interested in. And you are welcome to contribute and also become an author or editor for that blog on medium.

6 Likes

@yoshi, Excellent intro to working with JavaScript rules - some helpful basics in there which are probably documented, but lost in the detail of some of the more complex examples/posts out there!

Cheers
James

Nice. Pretty good
Why don’t you contribute to the openhab official documentation itself

2 Likes

Thanks for passing. My only quibble is it’s easier to get the state of an item using

items["Item name"]

You only ever really need to pull an item from the registry of it’s a Group Item and you want to get access to it’s members.

Also, event is undefined for any rule trigger that didn’t come from an Item.

It also might be worth mentioning that you can trstt for the existence of a variable using

if(myvar === undefined)

Finally, to save a variable from one run if the rule to the next, save it to this.

this.myTimer = (this.myTimer === undefined) ? null : this.myTimer

You can’t share this with other script actions or script conditions though.

I posted a tutorial for creating and importing libraries as well which would be of interest.

2 Likes

Cool, thank you very much. Storing a variable state from one rule run to another is new to me.

if(event == undefined) did not work. It still says that “event” is not defined.

Your article is very helpful. I will link to it in mine.

That wasn’t a typo. You need three =.

if(event === undefined) {    // this is line 4

still shows “ReferenceError: “event” is not defined in at line number 4”

yes please

Try this.event

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.