franks
(Frank Seidinger)
February 12, 2021, 10:55am
3
Here are two more examples on how to wait and block in a script. One example with java.lang.Thread.sleep()
and one example with a timer and a timer and a java.util.concurrent.CompletableFuture
.
JSR223 JavaScript
TODO… I believe JavaScript lacks a sleep command so the looping timer approach would work for it.
The ECMA Script 5.1 engine used in OH3 does not support setTimeout or Promise. Both of which would give you the possibility to do this natively with javascript.
But you can use the same approach as with sleep or timers. Here are my examples:
'use strict';
var ZonedDateTime = Java.type("java.time.ZonedDateTime");
var ChronoUnit = Java.type("java.time.temporal.ChronoUnit");
va…
Pandemic21:
I’ve spent hours on this and finally figured it out, so I thought I’d post it here in case anybody else runs into the same issue. After a lot of Googling I found out that OpenHAB doesn’t support the current version of Javascript, which means you can’t do the suggestion in the top StackOverflow post here . Also, setTimeout doesn’t work for some reason, so that’s also a no go.
Yeah, I found that out, too and noted it in this thread:
I’m using openHAB 3.0 with docker on a linux host OS.
I wanted to wait in an ECMA script rule some time and used the standard approach of promises and setTimeout to implement that. Unfortunately Promise seems not to be part of the javascript environment.
To illustrate I created a rule with a simple ECMA script:
var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.core.model.script.PromiseTest")
function sleep(sleepTime) {
return new Promise(
function(resolve, reject…
I hope, that OH3 will eventually update to a more recent ECMA Script version.