Being a newbie, trying to make my way in rules.
I’m trying to make UI rules (OH3), with actions in Ecmascript 2021.
JSScripting addon installed.
So, the task is simple - if hour of the day is 8, do something, else if hour is 18 - do another thing.
As far as I understand, the time.ZonedDateTime.now() has a method of getHour, but I can not make it work. var myHour = time.ZonedDateTime.now();
this one work ok,
but var myHour = time.ZonedDateTime.now().getHour();
leads to an error
Script execution of rule with UID 'test2' failed: org.graalvm.polyglot.PolyglotException: TypeError: (intermediate value).ZonedDateTime.now(...).getHour is not a function
Where does your time object come from, are you using a helper library or have defined it yourself, like - var time = Java.type("java.time.ZonedDateTime");
var myHour = time.now().getHour();
should work for that, I think?
Well, I just take samples from documentation. in that samples they just use: var tt= ZonedDateTime.now(); - and it works OK
So I assumed, that the ZonedDatetime object already exist, and all it’s methods and properties exist as well
UPD: well, your suggestion really works, thank you. so I have to get the ZonedDatetime object to variable, to then have possibility to manipulate it. OK.
SUMMARY:
JS scripting binding anounce most of objects ( like logger, zonedatetime, ephemeris) are imported already. but for now I can not proof it’s ok, as I was receiving constant errors while testing.
NOW, with ‘old’ Ecmascript 5.1 - need to import necessary objects, like:
var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var notifications = Java.type('org.openhab.io.openhabcloud.NotificationAction');
var ephemeris = Java.type("org.openhab.core.model.script.actions.Ephemeris");
var time = Java.type("java.time.ZonedDateTime");
In ECMAScript 2021 there is an attempt to make everything possible be pure JavaScript. So for time based stuff it uses JS-Joda library which mirrors the java.time.ZonedDateTime Object in almost every way which lets us use in in our rules to do stuff like creating Timers and the like.
This is discussed in the docs and there is a link there to the JS-Joda reference docs where all answers about that can be done and how with time can be answered.
When using JS Scripting the Helper Library comes with the add-on and it’s just there. Everything you need to use that Helper Library is imported by default (though that can be turned off if desired). The docs for the JS Scripting includes full documentation for how to create rules and scripts using the built in helper library.
So unlike with Jython or ECMAScript 5.1, JS Scripting is an all-in-one turn-key capability. Install the add-on and you have everything you need by default. So all that complexity and challenges we had to figure out how they are writing rules with what version of what Helper Library isn’t a concern with JS Scripting.
And except in very rare cases, there is no need to Java.type stuff in JS Scripting as most of that is done for you in the embedded helper library.
So in this case
var myHour = time.ZonedDateTime.now().hour()
would be the way to get at the current hour (unfortunately there isn’t a time.now() function exposed since time is just a direct reference to the JS Joda library, but that might be possible to add).
Which documentation? The example in the JS Scripting add-on is
thank you for detailed explanation.
I took the time.ZonedDateTime.now() from jsscripting page you give the link to. this example was working ok. But trying to extend it to time.ZonedDateTime.now().hour() gave me constant errors.
As a newbie, I can not get to the reasons of the issue, so I preferred to remove the jsscripting binding. Now, that I have got positive result at least with ECMAScript 5.1 and having read your remarks, I understand the situation a bit better, thank you,