How should I use delays in rules?

Hi All,
I’m a newcomer into the OpenHab community, I feel greate because I have made big progresses in my understanding of the key concepts of the 3.0 version but I got stuck with a very basic step creating my first automation rule… I dont know how to add a delay (timer) in between 2 actions.

The automation I’m trying to create is as follows:
Trigger = When an input phrase from Alexa is detected 2 actions should be triggered:
Action 1 = TurnOn a TV
Action 2 = Switch to Netflix

The Rule works well if the TV is already ON (Action 1 is executed but no changes because is already ON and Action 2 is executed successfully because it switch to Netflix)

The Rule Fails if the TV is OFF (Action 1 is executed correctly and TV turns ON but Action 2 fails because it is sent immediately after Action 1 is triggered and TV is still not ready to receive the command, consequently the action is executed but not “heard” by the TV)

I assume that adding a 5 Secs delay between Action 1 and Action 2 will solve the problem… but I cant find how to add this delay.

Seems like using a timer will solve it but I dont know how or where to code it, I will really appreciate if you can help me to solve this issue.

Please remember I’m using OH3 which I think to understand is using the Next Generation Rule Engine.

Here is the code of the Automation Rule:

triggers:

  • id: “1”
    configuration:
    itemName: EchoLivingRoom_LastVoiceCommand
    state: party mode
    type: core.ItemStateChangeTrigger
    conditions: []
    actions:
  • inputs: {}
    id: “3”
    configuration:
    itemName: LivingRoomTV_Power
    command: ON
    type: core.ItemCommandAction
  • inputs: {}
    id: “2”
    configuration:
    itemName: LivingRoomTV_Application
    state: Netflix
    type: core.ItemStateUpdateAction

Some extra info of my Learning&Test setup

  • Platform information:
    • Hardware: x64/16Gb/160Gb
    • OS: Windows 10
    • Java Runtime Environment: ZULU 11 (the recommended on)
    • openHAB version: 3.0

Thank You in advance :wink:

You’ll have to use a Script Action. In JavaScript it’ll look something like

events.sendCommand("LivingRoomTV_Power", "ON");
java.lang.Thread.sleep(5000);
events.sendCommand("LivingRoomTV_Application", "Netflix");

In Rules DSL it’ll look something like:

LivingRoomTV_Power.sendCommand(ON)
Thread::sleep(5000)
LivingRoomTV_Application.sendCommand("Netflix")

NOTE: You may see many warnings against using Thread sleep in posts on the forum. Those are referencing older versions of OH, not OH 3. They are not that much of a problem in OH 3.

3 Likes

Thanks a lot Rich!
So te entire action should be scripted and thanks for the code!
Kind regards,
Pablo

Hello again Rich,
Sorry I have to bother you again but I was trying to make the script work and Icant.
In JavaScrpti the TV ON/OFF and the sleep command works well, but the Application Change command to Netflix dosent. I been trying all possible combinations before posting again and I couldnt make it work. Added other options like Mute, Volume, etc and all work, but the Application Change dosent. Application Change works from the toggle in the Item.

The script in Rules DSL dosent work at all for me.

Im attaching some screenshots of the scripts and the Item and thing configurations.
I’m doing something wrong?
Thanks,
Pablo

I can’t really do anything with just the screen shot of the Item.

And for everything else except Items, the text from the Code tag is way more useful than a screen shot.

Many thanks @rlkoshak this is usefull also in my case.
I made this rule for delay on off a light, I need the contact work like a botton instead of switch :

image

p.c. @contex22 ==> for openwebnet command.

Which what is “the contact”? If it is an Item, what kind of Item?
Give examples of what you want to happen.