Wait_for_trigger event not being seen?

OOOOKay, i think it’s the same issue. Bare with me please. Try this (super band aide)

  - alias: "Snapshot garage and send photo when motion detected"
    trigger:
      - platform: state
        entity_id: binary_sensor.garage_exterior_door
        from: 'off'
        to: 'on'
    action:
      - event: take_snapshot
        event_data:
          entity_id: camera.aarlo_garage
          filename: '/config/www/cameras/garage_latest.jpg'
      - wait_for_trigger:
        - platform: event
          event_type: aarlo_image_updated
        timeout: "00:01:00"
      - service: notify.mobile_app_andrew_phone
        data:
          message: "Motion in garage!"
          data:
            image: !secret garage_image_url

and

  - alias: "Snapshot garage and send photo when motion detected"
    trigger:
      - platform: event
        event_type: take_snapshot
    action:
    - service: aarlo.camera_request_snapshot_to_file
      data:
        entity_id: "{{ trigger.event.data.entity_id }}"
        filename: "{{ trigger.event.data.filename }}"

That worked! Now WTH? :slight_smile:

Yes, so my theory is correct. The event is firing exactly the same time (or slightly before) the wait_template starts. This is because the service is not complete. So we need the 2 services (snapshot and wait_template) to run parallel to eachother. The only way to do this is to separate them. It seems that scripts now block movement forward where we want it to be non-blocking. The only way to do this is to fire it from another automation.

so, in ascii art:

In your automation:

|--- snapshot service ---> |
                           |--- wait_template 60 seconds ---> |
                           ^
                           |
               event happens here

In the separated automation:

|--- snapshot service ---> |
|--- wait_template 60 seconds ---> |
                           ^
                           |
               event happens here

Notice how the event is inside the duration of the wait_template, where as your’s its outside.

You could have also done this without the event like this:

  - alias: "Snapshot garage when motion detected"
    trigger:
      - platform: state
        entity_id: binary_sensor.garage_exterior_door
        from: 'off'
        to: 'on'
    action:
      - service: aarlo.camera_request_snapshot_to_file
        data:
          entity_id: camera.aarlo_garage
          filename: '/config/www/cameras/garage_latest.jpg'
  - alias: "Notify and send photo when motion detected"
    trigger:
      - platform: state
        entity_id: binary_sensor.garage_exterior_door
        from: 'off'
        to: 'on'
    action:
      - wait_for_trigger:
        - platform: event
          event_type: aarlo_image_updated
        timeout: "00:01:00"
        continue_on_timeout: false
      - service: notify.mobile_app_andrew_phone
        data:
          message: "Motion in garage!"
          data:
            image: !secret garage_image_url

Very interesting and nuanced. Thank you for the detailed explanation, that really helps explain the (intended?) behavior.

Would it have made a difference if instead of this:

    action:
      - service: script.take_snapshot
      - wait_for_trigger:
        - platform: event
          event_type: aarlo_image_updated
        timeout: "00:01:00"

it was written like this:

    action:
      - service: script.turn_on
        entity_id: script.take_snapshot
      - wait_for_trigger:
        - platform: event
          event_type: aarlo_image_updated
        timeout: "00:01:00"

The difference is that the first one calls take_snapshot and waits for it to finish whereas the second one doesn’t wait and immediately returns to execute wait_for_trigger.

1 Like

i bet that would work. didn’t know about that

Nor did I until pnbrucker started developing the scripting changes that were implemented in 0.113. Apparently, while modifying the scripting code, he discovered that the difference has existed for awhile. It was properly documented when 0.113 was released.

Waiting for script to complete.

I recall when I learned of it that I immediately reviewed how I called scripts in my system. I always leaned towards using the more compact service: script.whatever (i.e the version that waits for script completion). Fortunately, everywhere that I used it, my expectation was that it should behave like a function call (i.e. inline execution). So, purely by chance, I was using the correct one of the two ways to call a script (for my purposes).


EDIT

BTW, this thread has made me revisit the one automation I have where I used wait_for_trigger. I haven’t had the opportunity to exercise the automation yet but I suspect it will fail for the same reason the OP’s automation did: by the time wait_for_trigger begins listening for the event, the event has already occurred.

1 Like