How do I make sure one step completes in an automation before the next executes?

UPDATE: When I use Alexa to turn on or off the Wyze bulb, it can take 30 seconds for HA to update its status. When I use Alexa or the Wyze app to change the color of the Wyze bulb, sometimes the new color is not picked up at all by HA. Since there are also status update delays between Alexa and the Wyze app, this isn’t just a problem with the Wyze integration.

I need some help with what may be timing issues in an automation. I’m turning the smart-bulb in a lamp red to indicate a gate has been left open. I want to save it’s state, so that when the gate is closed, the bulb will return to it’s previous color and brightness. The service “scene.create” with the “snapshot_entities” seems like it was made for this. I create the scene and then turn on the bulb specifying red. When the gate gets closed, I call the “scene.apply” service, expecting it will restore the bulb to its original state, but instead it stays red.

When I select the scene the automation in Home Assistant and click “apply”, the light turns red - it appears that my “light.turn_on” sneaks in ahead of the “scene.create”. I am using “mode: single” in the automation, so I know it isn’t due to a re-trigger of the automation overwriting the scene.

A simple solution would be to add a “wait” step with timer, but I’m wondering if there is something better; something that will actually wait for the previous step to complete.

alias: Gate left open monitor
description: ""
trigger:
  - type: opened
    platform: device
    device_id: 5a4a79ff69b6fddb5de5d15bde4af201
    entity_id: binary_sensor.gate_open
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 30
condition: []
action:
  - service: notify.alexa_media_everywhere
    data:
      message: Warning! Warning! Gate left open! Gate left open!
      data:
        type: announce
      target: alexa_media.everywhere
    enabled: true
  - service: notify.mobile_app_victor_s_s23
    data:
      message: Gate left open
  - service: scene.create
    data:
      scene_id: save_living_room_lamp_state
      snapshot_entities: light.book_case_lamp
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
  - service: light.turn_on
    data:
      rgb_color:
        - 255
        - 0
        - 0
    target:
      device_id:
        - d86383b2527bb8332be6b911bbf1def1
  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.gate_open
        to: "off"
        for:
          hours: 0
          minutes: 0
          seconds: 5
      - type: not_opened
        platform: device
        device_id: 5a4a79ff69b6fddb5de5d15bde4af201
        entity_id: binary_sensor.gate_open
        domain: binary_sensor
        for:
          hours: 0
          minutes: 0
          seconds: 30
    continue_on_timeout: true
  - service: notify.mobile_app_victor_s_s23
    data:
      message: Gate has been closed
  - service: scene.turn_on
    data: {}
    target:
      entity_id: scene.save_living_room_lamp_state
  - service: notify.alexa_media_everywhere
    data:
      message: The Gate has been closed.
      data:
        type: announce
      target: alexa_media.everywhere
    enabled: true
mode: single


It’s probably just that the scene create is taking a few too many milliseconds.

Put a short delay between the two services and see how that performs.

  - service: scene.create
    data:
      scene_id: save_living_room_lamp_state
      snapshot_entities: light.book_case_lamp
  - delay: "00:00:02
  - service: light.turn_on
    data:
      rgb_color:
        - 255
        - 0
        - 0
    target:
      device_id:
        - d86383b2527bb8332be6b911bbf1def1
1 Like

Should this have 2 quotes or no quotes??

- delay: "00:00:02
                  ^
            missing quote?

When the gate is closed, you should call the scene.turn_on service for scene.save_living_room_lamp_state.

Review the last example in the following documentation:
Creating scenes on the fly

Yes sorry, that was a typo.

1 Like

I thought you had caught the problem, but it turned out that the GUI automation editor says “Scene: Activate”, but the YAML the GUI builds has the correct service call to “scene.turn_on”

When I wrote “scene.apply”, I was wrong. I appreciate your catching my error, and I will try to write a more accurate description in the future.

  - service: scene.turn_on
    data: {}
    target:
      entity_id: scene.save_living_room_lamp_state

Post the entire automation.

TY, I added the entire automation. Also, I added an update about the Wyze Integration delays.