Wait/sleep in PaperUI experimental rule engine rule - how?


does anyone knows how to perform wait operation in PaperUI experimental rule engine rule (some Thread::sleep(1000) equivalent)?

I see that there is option to run script ECMAScript but I don’t know how to perform this operation in this language.


The graphical rule editor in PaperUI is incomplete. I think time related actions are one of the missing areas.
It’s not going to be completed now, very different plans are afoot for OH3.

I’m trying to say in a kind way that you’re wasting your time with it. :smiley:

The rule engine itself is fine, and can be used with a variety of non-graphical languages. Jython is encouraged at present.

If you must have graphical, node red issupported.

The vast majority of pre-existing example rules here are in OH1/OH2 “native” DSL, which will continue to be supported going on into OH3.

I’m suggesting you choose another way to begin with rules.

The is no sleep in JS (ECMAScript 5.1). If you install Jython, you will also have the option to use a script action using it. Then you can…

from time import sleep


I recommend not using the UI rule editor in Paper UI and just write your rules in scripts. Currently, the easiest way to install Jython is…

But you can also install it manually…


While there is no sleep in JavaScript, you can call into Java classes. See Experimental Next-Gen Rules Engine Documentation 5 of : Actions, the section right under createTimer.

Often creating a Timer might be a better idea as well.

Also in javascript you can use a function with a delay. The only dependcie is the helper library.

var onTimerFunc = function(arg){
   // this code will run after delay
   // ...

// delay in ms
// arg, single argument (for multiple arguments you can use an array or object
// onTimerFunc, function that is called after delay
var delay = 10000 // 10000ms
var arg = 'ON'    // or ['22','323','17'], ...

var onTimer = setTimeout(onTimerFunc, delay, arg)

@5iver I’m using the old helper libraries with OH 2.4 and it works well. Is this not possible with the newer helper libraries?

I’m not sure what you are referring to by “old helper libraries”, but I do not see any issue using this with the current helper libraries. However, there is a big difference between an asynchronous timer and sleeping a thread. Scripted automation is running in the JVM and has full access to use Java and any of the public OH classes and interfaces. This means we are not locked into what the scripting languages natively provide. For example, you could pause a script in JS using executeCommandLine to call a Python script that calls sleep! There are lots of possibilities, but there is nothing native in JavaScript (ECMAScript 5.1) to pause a thread.

I meant with old helper libraries, the library provided by lewie. (in the past)

Are the newer helper libraries compatible with OH 2.4? I am not sure. Most of the time I can read jython at all places in the forum, but I prefer javascript.

Yes, but a sleeping thread? I always try to avoid it.

Absolutely! :slightly_smiling_face:

When using the old rule engine, pausing a thread could definitely cause issues, since there were a limited number of threads available, but this is not an issue with the NGRE and scripted automation. In the old rule engine, you could trigger a rule multiple times simultaneously and there could be multiple instances of the rule executing at the same time. The total number of rules executing at a time was limited by the number of threads in the pool. Using sleep held up the thread, so you could end up running out out of them and cause all rules to freeze up while waiting for a thread to become available.

The new rule engine allows for multiple triggers too, and the rules will queue up FIFO, but only one instance of a rule will be executed at a time. So, using a sleep in NGRE rules does not cause issues with using up threads in a pool, but it will prevent another instance of the same rule from executing until the last one has completed. When using Jython, you can easily get past this by spawning new threads from within the rule.

Nice, i will have a look at that.

I would prefer (and i use it) a other approach in javascript:
My entry rule is really short and inside this rule one can set a variable as state.
Now one can call a async function, setTimeout - directly or with a delay - .
This function can be a loop and runs until the state variable has a certain value or the function runs one time. What ever is nessesary.
So i have a really short entry and can direct (nearly without the query and FIFO) react on item state changes.


since OH3 M1 and M2 has been released with this engine as default engine does anything changed when it comes to pausing between commands in GUI engine rules?

No, it’s still the same engine so all of the above should still apply.