Get current day, hour and minute in Script

Hello, I’m trying to get the current Day of the week, Hour and Minute in a ECMA script 262 edition 11. This is the code:

var n = time.ZonedDateTime.now();
var day = n.dayOfWeek().value(); // Get the day of the week (1,2,3,4,5,6,7)
var hour = n.hour(); // Get the hour of the day
var min = n.minute(); // Get the minute of the day

The rule is triggered every 2 minutes:

The behaviour is very strange, the rule sometime works and sometime gives me this error:

2022-12-01 11:36:00.748 [ERROR] [b.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: TypeError: undefined has no such function “now”
at .:program(:111) ~[?:?]
at org.graalvm.polyglot.Context.eval(Context.java:379) ~[?:?]
at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458) ~[?:?]
at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426) ~[?:?]
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264) ~[java.scripting:?]
at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:51) ~[?:?]
at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:69) ~[?:?]
at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:51) ~[?:?]
at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:69) ~[?:?]
at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0(ScriptActionHandler.java:58) ~[?:?]
at java.util.Optional.ifPresent(Optional.java:183) [?:?]
at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute(ScriptActionHandler.java:55) [bundleFile:?]
at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1181) [bundleFile:?]
at org.openhab.core.automation.internal.RuleEngineImpl.runRule(RuleEngineImpl.java:989) [bundleFile:?]
at org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData.run(TriggerHandlerCallbackImpl.java:89) [bundleFile:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
at java.lang.Thread.run(Thread.java:829) [?:?]
2022-12-01 11:36:00.751 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘Rule_01’ failed: org.graalvm.polyglot.PolyglotException: TypeError: undefined has no such function “now”

Did anyone have the same problem, and can help me? I’m on OH 3.4.0.M3 on docker.
Thanks!

No, I’ve never seen an error like that and it doesn’t make much sense. That should work as written. ZonedDateTime absolutely does have a now function. But it’s saying that time.ZonedDateTime is undefined.

Have you messed with the settings for JS Scripting?

If “Do not use built-in variables” is selected, the helper library will not be imported by default. You’ll have to import that yourself. I suppose that could explain the error.

What happens if you use:

var n = time.toZDT();

That’s essentially equivalent to what you have now only shorter.

Have you ever installed or modified any libraries using npm? What’s the contents of your $OH_USERDATA/automation/js/node_modules folder?

One final guess, have you restarted recently? If not try restarting OH.

Thanks for your reply.

JS Scripting si set to “Use built-in Variables”, and I replaced the

var n = time.ZonedDateTime.now();

with

var n = time.toZDT();

but I have a similar error:

failed: org.graalvm.polyglot.PolyglotException: TypeError: time.toZDT is not a function

I did some additional tests: if I disable and then re-enable the rule, the first time the script is working, giving me the correct values:

"2022-12-03T10:04:00.905+01:00[SYSTEM]"
6
10
4

where 6 is the number of day (Saturday), 10 is the hour and 4 is the minute.

The second time the rule is triggered it gives me the error above.

I’m not a java expert, but it seems that the thread that is running for the time.toZDT() cannot be executed two time or is still running when executed for the second time and gives the error.

This happen also if I force execution of the script with “Run Now” button in the UI, if I disable and re-enable the rule the first time is working and the second time and following times fails. Very difficult to understand this behaviour for me… :slight_smile:

I didn’t install or modified any libraries with npm, and the content of the folder

~/openhab_prod/conf/automation/js/node_modules

is empty.

Are the script lines in the first post the complete script or is there more? If there’s more, can you post the complete script?

That is informative.

Something is happening from one ton of the role to the next. Please post the full rule from the “code” tab so we can see the triggers and everything else.

It’s not a Java or a thread issue but the same rule gets reused each time it runs so stuff done during one run could effect subsequent runs.