Migrating DSL Rules to JSR223/Javascript with a case example

Fantastic tutorial! I will provide a link to it from the Time of Day DP.

I would recommend users consider using a git clone to pull the library and provide a softlink to the jslib folder under the automation folder. This will allow you to update the library more easily with a git pull. It’s not a strong recommendation, just an alternative to consider.

cd $OH_CONF
git clone git@github.com:lewie/openhab2-javascript.git
ln -s openhab2-javascript/jslib/ $OH_CONF/automation/jsr223/jslib

Then to update:

cd $OH_CONF/openhab2-javascript
git pull

Pick a good location to check it out into that works for your configuration.

Also note that as of this writing the JSR223 helper authors are working to merge the three into a single repo so they can be centrally managed and updated. So the location of the repo for the library may change in the not to distant future.

There is also work in progress to make these helper libraries for all three JSR223 libraries easier to install if not something that gets installed automatically for you. With the efforts of @5iver and others JSR223 is becoming easier and easier to use every day.

Maybe not so self explaining. What is __LINE__?

Just a note on the docs page linked to. That was a first attempt at documenting the Next Gen Rules Engine (NGRE). It is worth mentioning here that the NGRE has two interfaces, a GUI based one and JSR223. As such there are some very key areas where they differ. For example, GUI created Rules will be saved in JSONDB but JSR223 is saved in .js files. Also, the rule parts of the code (JSRule, triggers, etc.) get defined through the GUI so the only JS code you would write will be for Action Scripts or Conditional Scripts. But the code in those Scripts will look just like the code presented in the execute:function.

You can read more about the differences and similarities between the GUI based Rules and JSR223 at Experimental Next-Gen Rules Engine Documentation 1 of : Introduction in the “How NGRE Is Related to JSR223” section.

As we move forward with a replacement for PaperUI, the GUI based way of defining Rules will likely become the one used by most non-technical users. But because both are executing on the same rules engine, it shouldn’t be hard to migrate to all text based rules should the user desire.

Some more information about working with Date Time Objects of various types in JS see Experimental Next-Gen Rules Engine Documentation 4 of : Writing Scripts in the DateTimeType section. It’s not complete but it should provide enough information for users to figure out how to get to a DateTimeType you can post to a DateTime Item.

Two important things to note is one can use the Java OffsetDateTime class or even good old Joda DateTime if you need to, though when OH goes to a new version of Java, the Joda DateTime may no longer be supported.

Obviously, I would encourage everyone to use native JavaScript stuff as much as possible in JS Rules, but be aware that you have full access to all the core Java classes as well, just like in Rule DSL.

should these be else if? Once you find a match you don’t need to do the remaining comparisons. By using only if every case is checked.

Do you really need the subtractions in the comparisons? Couldn’t you just say, for example:

if(now > morning_start && now < day_start)

That is checking to see if now is between morning_start and day_start which I think is equivalent to what you are doing with the subtractions.

Now, another idea is instead of following my suggestion of using else if statements, we can use multiple if statements and simplify the conditions.

    if(now > night_start)     curr = "BED"; // The first condition must be the first time period after midnight
    if(now > morning_start)   curr = "MORNING";
    if(now > day_start)       curr = "DAY";
    if(now > afternoon_start) curr = "AFTERNOON";
    if(now > evening_start)   curr = "NIGHT";

With the above, it will check each time in turn. So when it’s after day_start and before afternoon_start, curr will become “BED”, then “MORNING”, then “DAY” and stay there because the conditions for afternoon_start and evening_start will evaluate to false.

Great job. I hope this becomes one of the most popular tutorials on the forum as users start to migrate more and more to JSR223 Rules. Thanks for posting!

1 Like