Can scripts run asynchronously

I need some help with a script that I can't get right.
It used to turn an LED strip green then wait up to 30 seconds for a trigger (a door opening). Depending on whether the door was opened or if it timed out, it would then run one of two other scripts.
The device is using ESPHome and the LED strip is a light of type esp32_rmt_led_strip

I changed the LED turning green to use an effect instead. The effect has lambda that has a for loop in it that turns the LEDs off one by one for 30 seconds.

The idea is that it's a countdown for the 30s timeout, but it actually seems to run the whole thing before waiting for the trigger, so if I open the door during the effect, the next script doesn't run

Is there a way of running the effect lambda asynchronously and continuing the script, then cancelling it if the trigger happens?

TIA

https://www.home-assistant.io/integrations/script/#waiting-for-a-script-to-complete

There are a multitude of other mechanisms for dealing with events that "may" occur:

  • Timers
  • Wait for event
  • Sleeping/polling for state change.
  • Delay on/off attributes on sensors.
  • Using input booleans as flags and separate automations for the red vs green color change.
  • Scripts with multiple named triggers.

TL;DR - Cancelling an automation is probably not the best solution.

So it seems that the ESPHome light effect is already asynchronous, and it was my script that was not seeing the trigger properly.

I changed this:

  • wait_template: "{{ is_state('binary_sensor.office_door_state', 'on') }}"
    timeout: "00:00:40"

to this:

  • wait_for_trigger:
    • platform: state
      entity_id: binary_sensor.office_door_state
      to: "on"
      timeout: "00:00:40"

and the trigger worked and the script carried on running.