Templateable wait_for_trigger actions

Hi everyone,

I recently switched from the Nest Hello doorbell to the Unifi Protect G4 Doorbell. I’m trying to replicate Nest’s Quick Responses.

Here’s the flow I’m after:

  • Doorbell rings
  • Actionable notification is sent to our phones
  • We press one of the notification actions
  • The doorbell plays an MP3 recording and shows a text on screen.

To make it as flexible as possible, I decided to create a “mapping” variable that contains the notification action ID, sound it has to play on the doorbell, notification title & icon, and the text that should be displayed on the doorbell:

  variables:
    mapping: 
      {
        "DOORBELL_REPLY_LEAVE_PACKAGE": {
          "doorbell_sound": "pakje-aan-deur.mp3",
          "doorbell_text": "Zet het pakje maar aan de deur. Bedankt!",
          "notif_title": "Zet pakje maar neer",
          "notif_icon": "sfsymbols:shippingbox"
        },
        "DOORBELL_REPLY_WE_ARE_COMING": {
          "doorbell_sound": "we-komen-eraan.mp3",
          "doorbell_text": "We komen eraan! Even geduld...",
          "notif_title": "We komen eraan",
          "notif_icon": "sfsymbols:shippingbox"
        },
        "DOORBELL_REPLY_NOT_HOME": {
          "doorbell_sound": "even-niet-thuis.mp3",
          "doorbell_text": "We zijn niet thuis. Sorry!",
          "notif_title": "We zijn niet thuis",
          "notif_icon": "sfsymbols:shippingbox"
        },
      }

I can then use this mapping variable to generate the actions for the notifications like so:

  action:
    - service: notify.xavier
      data:
        title: "Ding Dong"
        message: "Iemand aan de voordeur. Hoe wil je reageren?"
        data:
          # Attach an image of the camera
          attachment:
            content-type: jpeg
          entity_id: camera.voordeur_high

          # Tag to clear the notification afterwards
          tag: "TAG_DOORBELL_RANG"

          # Possible quick replies
          actions: >
            [
            {%- for actionName, props in mapping.items() -%}
              {
                "action": "{{actionName}}",
                "title": "{{props.notif_title}}",
                "icon": "{{props.notif_icon}}",
              },
            {%- endfor -%}
            ]

This works fine. I now want to use a similar system with wait_for_trigger so that the automation can continue when we acted on the notification. It seems like wait_for_trigger doesn’t accept a similar template:

- wait_for_trigger: >
   [
   {%- for actionName, props in mapping.items() -%}
      { platform: "event", event_type: "ios.notification_action_fired", event_data: { actionName: "    {{actionName}}"},id: "{{actionName}}" },
   {%- endfor %}
   ]

It throws this error:

Invalid config for [automation]: expected a dictionary @ data['action'][1]['wait_for_trigger'][0]. Got None. (See ?, line ?).

Am I missing something? The template renders fine in the developer tools. In my eyes, wait_for_trigger expects an array, which is exactly what I’m generating with the template. I don’t see how the template can generate a None value.

Hard coding the action IDs does work, but I want to avoid that.

    - wait_for_trigger:
        - platform: event
          event_type: ios.notification_action_fired
          event_data:
            actionName: "DOORBELL_REPLY_LEAVE_PACKAGE"
        - platform: event
          event_type: ios.notification_action_fired
          event_data:
            actionName: "DOORBELL_REPLY_WE_ARE_COMING"
        - platform: event
          event_type: ios.notification_action_fired
          event_data:
            actionName: "DOORBELL_REPLY_NOT_HOME"
      timeout: 900  # 15min in seconds
      continue_on_timeout: false

Here’s my full automation for reference:

- id: C7A71EEC-CD42-4B95-BEFF-B8507C206331
  alias: '[📣] Notify when someone is at the door + quick responses'
  mode: restart
  variables:
    mapping: 
      {
        "DOORBELL_REPLY_LEAVE_PACKAGE": {
          "doorbell_sound": "pakje-aan-deur.mp3",
          "doorbell_text": "Zet het pakje maar aan de deur. Bedankt!",
          "notif_title": "Zet pakje maar neer",
          "notif_icon": "sfsymbols:shippingbox"
        },
        "DOORBELL_REPLY_WE_ARE_COMING": {
          "doorbell_sound": "we-komen-eraan.mp3",
          "doorbell_text": "We komen eraan! Even geduld...",
          "notif_title": "We komen eraan",
          "notif_icon": "sfsymbols:shippingbox"
        },
        "DOORBELL_REPLY_NOT_HOME": {
          "doorbell_sound": "even-niet-thuis.mp3",
          "doorbell_text": "We zijn niet thuis. Sorry!",
          "notif_title": "We zijn niet thuis",
          "notif_icon": "sfsymbols:shippingbox"
        },
      }
  trigger:
    - platform: state
      entity_id: binary_sensor.voordeur_doorbell
      from: "off"
      to: "on"
  action:
    - service: notify.xavier
      data:
        title: "Ding Dong"
        message: "Iemand aan de voordeur. Hoe wil je reageren?"
        data:
          # Attach an image of the camera
          attachment:
            content-type: jpeg
          entity_id: camera.voordeur_high

          # Tag to clear the notification afterwards
          tag: "TAG_DOORBELL_RANG"

          # Possible quick replies
          actions: >
            [
            {%- for actionName, props in mapping.items() -%}
              {
                "action": "{{actionName}}",
                "title": "{{props.notif_title}}",
                "icon": "{{props.notif_icon}}",
              },
            {%- endfor -%}
            ]

    # Wait for the response to the notification
    # Unfortunately, I can't use a JINJA2 template here?
    - wait_for_trigger:
        - platform: event
          event_type: ios.notification_action_fired
          event_data:
            actionName: "DOORBELL_REPLY_LEAVE_PACKAGE"
        - platform: event
          event_type: ios.notification_action_fired
          event_data:
            actionName: "DOORBELL_REPLY_WE_ARE_COMING"
        - platform: event
          event_type: ios.notification_action_fired
          event_data:
            actionName: "DOORBELL_REPLY_NOT_HOME"
      timeout: 900  # 15min in seconds
      continue_on_timeout: false

Thanks a lot!

You’re making an assumption that the wait_for_trigger key supports the use of a template. This capability isn’t available everywhere. In fact, it was only recently added to the data key of service calls.

Ah, didn’t know that. So I should open an issue on GitHub to suggest this. Thanks!

Nope. Github is for issues. Feature requests go here:

Right. Should I open a new topic there, or can you move this one? I can rewrite it a bit if needed.