How to do If and else in script?

Good day everyone.
How can I use if and else in scrips? Using template service seems to be not enough.
Trying to make a script which would check if I woke up. Here is a part of algorithm which I have troubles with:

-check motion sensor 1:

If it is Off (for 10 min or more) - play a sound on media player(short one), Wait when motion will get On and then turn off media player. Timeout - 30 seconds. Go to the next step.

Else - go to the next step.

Actually, there is the same thing going over and over again. Just in case, here is full part of script which checks if I am awake

-alarm clock. Start the timer

-delay 30 seconds

-check motion sensor 1:
If it is Off (for 10 min or more) - play a sound on media player(short one), Wait when motion will get On and then turn off media player. Timeout - 30 seconds. Go to the next step
Else - go to the next step.

-delay 5 minutes

-check motion sensor 2:
If it is Off (for 15 min or more) - play a sound on media player (long one). Wait when motion will get On and then turn off media player. Timeout - 5 minutes. Go to the next step.
Else - go to the next step.

-timer is up

-check motion sensor 3:
If it is Off (for 6 minutes or more) - go to the next step
Else - stop the script

-check motion sensor 2:
If it is Off (for 6 minutes or more) - go to the next step
Else - stop the script

-check motion sensor 4:
If it is Off (for 6 minutes or more) - go to the next step
Else - stop the script

-check motion sensor 1:
If it is Off (for 6 minutes or more) - play a sound on a media player. Wait when motion2 will get On and then turn off media player. Then stop the script.
Else - stop the script

A condition check in a script stops the script if it returns false. That means you’d have something like:

  1. Start timer
  2. Delay 30 seconds
  3. Call script #2 (checks motion sensor and …)
  4. Delay 5 minutes
  5. Call script #3 (checks motion sensor and …)
  6. Waits for timer to finish
  7. Checks motion sensor 3
  8. Checks motion sensor 2
  9. Checks motion sensor 4
  10. Calls script #4 (checks motion sensor and …)
1 Like

That was easier than I thought. First tests are good, it works. Thanks!

Hi, one small question here. I used delay at sixth point instead of timer finish. Now I have a timer and I am thinking witch way would be the best to go

  1. Use an automation witch would trigger on event (timer is over)

  2. Use wait template

I would like to use wait template because I have an automation which turns off this script if I leave home. But I do not know how to use event in wait template.

A delay says wait exactly this long and then continue.

A wait template says wait until this condition is true (you can’t test for events, but you could have another automation that triggers on the event, and sets the state of a boolean, say, and use the state of that).

A timer can be pause, reset, or left to run until it completes, and then the completion of the timer starts another automation (assuming you’ve set that up).

In my view:

  • delay is ideal when you want a fixed time between two actions
  • wait template is ideal when you want to wait until a condition is true (for instance, I use it for a notification to turn off an extractor after humidity drops to the right level)
  • timers are great where you want a delay, but with more flexibility
1 Like