Do a part of a script only if a condition is true

Hi,
I’m trying to to setup a script that goes through a sequence to launch a movie on my Apple TV. This is using harmony. The only problem is that I have a delay of 19 secs to let the Apple TV power on, sometimes my Apple TV is already on though and I don’t want to wait the time as it can just start going strait away. Can I make an automation that skips that delay in the script if the activity ‘Watch Apple TV’ is powered on but leave the delay if it is on another activity or powered off?

Thank you your help would be appreciated.

You could use a value template for the duration of your delay. 0 sec if the apple tv is on, 19 if it’s off.

Could you please provide me the code.(I’ve only just started doing this, sorry)

No actually you should use the wait-template function.

Something like:

- wait_template: "{{ is_state('sensor.apple_tv', 'on') }}"

Thank you. So I just add this in the script instead of the delay?

Correct, replacing sensor.apple_tv with whatever you want to wait to turn on.

The script will pause until the statement is true, or the time out is reached.