Are there examples of rules with ECMAScript 262 Edition 11?
I’m looking for wiki type of examples that can be easily edited, corrected and extended for everyone’s benefit.
When searching for examples you often find them for older versions and then need to modify them for the new ECMAScript version.
I’m happy to contribute if such a wiki exists or can be setup and is appreciated.
What do you think?
Even better, there are rule templates. Rule Templates - openHAB Community. Why copy/edit when you an install and configure. This is way better than Cargo cult programming - Wikipedia which I’m afraid, based on my years experience here on the forum, a wiki like you propose, would encourage.
There is also the Tutorials and Solutions section of the forum which is intended for such examples. There are a number of ECMAScript 11 examples there already and more would be welcome.
Most of the Design Pattern posts do not have an ECMAScript 11 example because it’s either been implemented as a rule template or it’s been implemented in Announcing the initial release of openhab_rules_tools. With few exceptions, you shouldn’t be implementing the Design Patterns yourself any longer.
Hi @rlkoshak, thanks for your quick reply.
I don’t think a structured wiki page with well explained examples would encourage meaningless copy/pasting.
The problem with having examples in the forum is, that they are not kept up to date and are harder to locate than on a structured wiki.
I agree, such examples should not implement existing functionality like from openhab_rules_tools but use it.
The tests of openhab_rules_tools look a bit like what I was looking for. I’ll check them out in more details.
It has in the past in my experience.
Based on my experiences with the documentation for OH 1, a wiki has the same problem.
If you want to give it a try you are welcome to. Ask and I can convert a forum posting to a wiki for you (it’s one of my powers as a moderator). It should go in the Tutorials and Solutions section of the forum and be sure to tag it with
JS, and other variations so it can be found by tag. In my experience it’s not worked very well in the past but maybe this time will be different.
Oh, those are really poorly written and they have antipatterns that people should not follow when writing rules. They make good examples for how to instantiate and use the openhab-rules-tools classes but their use of timers, sleeps, and other weird structures are not suitable for use in rules. Better would be to go through the JS Scripting readmes and generate examples for each major feature. More complicated examples probably should be a rule template.
Many thanks for your detailed reply @rlkoshak.
My intention is to make it easier for users to create rules (including myself). The wiki idea was just something I thought could be helpful.
I personally have multiple rules running and they work well. But it took me quite some time to set them up and I feel guidance/documentation to create them could be improved.
I have a list of rules in mind for which I think it would be great to make it easy for users to use them. Possibly I could implement some as Rule Templates.
I tested the “Thing Status Reporting” template and have it calling a
ECMAScript (ECMAScript 262 Edition 11) based rule (all my rules are written in). According to the description can I read a parameter with
context.getAttribute("things"). But I didn’t yet manage to read it because
context is not defined. Do I need to import it from somewhere?
That’s how to do it for Nashorn JS (i.e. ECMAScript 5.1). For The newer JS Scripting add-on, the values are just injected. Instead of
this.things or just
Although, now that I see that
things is a poor choice for a variable name because it overwrites a variable injected from the helper library.
I’m not going to change it for the 3.4 version of the template
but need to change it for the 4.0 template. In the 4.0 version of the template it uses
thing and doesn’t send all the Things on each call because it now triggers on Thing status changes instead of polling so it doesn’t have to.
But in your case, I recommend going into the script action of the rule and change the name from
things to something else so that the
things from the helper library doesn’t get overwritten.
Thank you, that was the missing piece to get it to run. I suggest to add this hint to the rule template description.
The reason I haven’t and probably won’t is that then I’d have to figure out and add it for all then rules languages. The 3.4 rule template is written in Nashorn JS so that’s what I provided in the description. The 4.0 rule template is written in GraalVM JS so that’s what I provided in its description. I don’t know how it works in Rules DSL, jRuby, Jython, Groovy, Java Scripting, etc.