Sleep in Openahb 3.0.0->Rules->Action->execute a given script

Hi,
I am trying to sleep for some seconds in my script. I tried the following script:

var outdoor_temp, bedroom_temp, a_at_home, b_at_home;


outdoor_temp = itemRegistry.getItem('weather_OutdoorTemperature').getState();
bedroom_temp = itemRegistry.getItem('bedroom_temperature').getState();
a_at_home = itemRegistry.getItem('a_Online').getState();
b_at_home = itemRegistry.getItem('b_Online').getState();
if ((outdoor_temp < 70 || bedroom_temp < 70) && (b_at_home || a_at_home)) {
  events.sendCommand('bedroom_hvac_mode', '1');
  Thread::sleep(1000);
  events.sendCommand('bedroom_heat_temp', '70');
} else if ((outdoor_temp > 76 || bedroom_temp > 76) && (b_at_home || a_at_home)) {
  events.sendCommand('bedroom_hvac_mode', '2');
  Thread::sleep(1000);
  events.sendCommand('bedroom_cool_temp', '76');
} else if (outdoor_temp < 35) {
  events.sendCommand('bedroom_hvac_mode', '1');
  Thread::sleep(1000);
  events.sendCommand('bedroom_heat_temp', '50');
} else {
  events.sendCommand('bedroom_hvac_mode', '0');
}

Unfortunately, the Thread::sleep(1000); does not work here.

I am using the script in Rules engine in Openhab 3. There is an action named execute a given script.

Do you have any suggestion to use here to sleep one second between the two commands?

This is JavaScript so accessing some things are necessarily a little different from Rules DSL.

java.lang.Thread.sleep(1000);

Note, while long running rules like this are not the same sort of problem as they were in OH 2.5 rules DSL, if this rule is likely to be triggered several times that means those triggers will queue up and this rule may be constantly running and rerunning without being able to catch up. At least it won’t crash your other rules though.

I’ve a Python library that makes this sort of rule a little easier. I still need to port it to JavaScript but it would look something like

if(outdoor_temp < 70 || bedroom_temp < 70) && (b_at_home || a_at_home) {
    events.sendCommand('bedroom_hvac_mode', '1');
    defer('bedroom_heat_temp', '70', '1s');
}

That library handles creation of a Timer to send the command to the Item later. I’ve a bunch of similar utilities like that at GitHub - rkoshak/openhab-rules-tools: Library functions, classes, and examples to reuse in the development of new Rules.. My goal is to make it so no one needs to mess with sleeps or Timers directly.