Trying to use sleep for delay

OH3 on RPi 4:

I am trying to use a delay in a script to turn a switch off 4 hours after turning it on. When I try and put it in this script I get an error that implicit map keys need to be followed by map values.

triggers:
  - id: "1"
    configuration:
      time: 05:00
    type: timer.TimeOfDayTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      itemName: SwitchBatteryChargers_Switch
      command: ON
      Thread::sleep(14400)
    type: core.ItemCommandAction
  - inputs: {}
    id: "3"
    configuration:
      itemName: SwitchBatteryChargers_Switch
      command: OFF
    type: core.ItemCommandAction

You can use the expire metadata in the item and set the off for 4 hours. That way you don’t need to program any timers etc.

Here is an example:

Yes I’ve used that before but I’m using the expire timer on my items in a rule to alert me if something is offline so I need to use a timer or delay.

There are a couple of things going on here:

  1. The error refers to the fact that yaml uses : between the keys and the values (key: value). That means that you cannot have a value with a colon in it that is not escaped in some way (usually it has been incorporated into a string). So the yaml parser sees command: ON Thread::sleep(14400) as two different improperly formatted keys.
  2. The bigger issue is that you cannot use Thread::sleep in this way. The item action for the rules will only accept recognized OH commands for its command key and Thread::sleep is not a command fit to send to an item it is a line of script. So, if you want to use Thread::sleep you have to use the run script action instead of the item action option when setting up your rule. That script would then have to contain lines to send the first command to the item, sleep for the required time, and lastly send the second command to the item.

Depending on your use case, there are probably several other options that are better than a 4 hour Thread::sleep. Many users have similar setups and if you search the forums for timers or the new item expire metadata you will find many examples and guides on how to do this another way.

1 Like

Thanks JustinG, I just used a timer.