Need help in creating wait_for_trigger using for loop

Hi,

I am trying to create wait_for_trigger for all the values in an input_select. Code I am trying is

wait_for_trigger: >
  {% for action_title in state_attr('input_select.testinputselect','options') %}
      - platform: event
        event_type: mobile_app_notification_action
        event_data: 
          action: {{ action_title }}
  {% endfor %} 

I also tried using

wait_for_trigger: >
  {% set events = namespace(data = []) %}
  {% for action_title in state_attr('input_select.testinputselect','options') %}
    {% set event = {
      "platform": "event",
      "event_type": "mobile_app_notification_action",
      "event_data" : {"action" : action_title}
      }
    %}
    {% set events.data = events.data + [event] %}
  {% endfor %} 
  {{ events.data }}

But when i am trying to save this i get error

Message malformed: expected a dictionary @ data[‘sequence’][1][‘wait_for_trigger’][0]

Actual working code without for loop is

  wait_for_trigger:
  - platform: event
    event_type: mobile_app_notification_action
    event_data:
      action: Test1
  - platform: event
    event_type: mobile_app_notification_action
    event_data:
      action: Test2

I am not sure what I am missing here.

You cannot build YAML with templates. Post your actual automation and its goal so we can avoid XY problems

So I am trying to create a script which sets the value of input_select based on actions selected by user via phone notification.

Full script code is

  - device_id: c5d2ecko3754253d5aoa8zab2ac86e98
    domain: mobile_app
    type: notify
    message: Test Message
    title: Test Title
    data:
      persistent: true
      actions: >
        {% set uris = ["", "", ""] %}  {% set actions = namespace(data = []) %}
        {% for action_title in state_attr('input_select.testinputselect','options') %}
          {% if action_title|length %}
            {% set uri = uris[loop.index - 1] %}
            {% set action = {
                  "action": action_title,
                  "title": action_title
              }
            %}
            {% set actions.data = actions.data + [action] %}
          {% endif %}
        {% endfor %} {{ actions.data }}
  - wait_for_trigger: >
      {% set events = namespace(data = []) %} {% for action_title in
      state_attr('input_select.testinputselect','options') %}
        {% set event = {
          "platform": "event",
          "event_type": "mobile_app_notification_action",
          "event_data" : {"action" : action_title}
          }
        %}
        {% set events.data = events.data + [event] %}
      {% endfor %}  {{ events.data }}
  - service: input_select.select_option
    target:
      entity_id: input_select.testinputselect
    data:
      option: |
        {{ wait.trigger.event.data.action }}

Reference for phone notification : Actionable Notifications | Home Assistant Companion Docs

#1: You cannot, currently, live-build yaml using templates. See also

There are some experiments on that front, but they are not core HA functions.

#2: You cannot use templates in Device actions… switch to a Call service action, as shown in all the examples in the document you linked in your original post.

You can streamline your Wait-for-trigger action by assigning the context ID value to the notify event data alias key… that way you do not need to have individual wait triggers for each action.

sequence:
  - service: notify.mobile_app
    data:
      message: Test Message
      title: Test Title
      data:
        alias: "{{ context.id }}"
        persistent: true
        actions:
          - action: action_1
            title: Title 1
          - action: action_2
            title: Title 2
          - action: action_3
            title: Title 3
  - wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          alias: "{{ context.id }}"
    timeout: "00:05:00"
    continue_on_timeout: false
  - service: input_select.select_option
    target:
      entity_id: input_select.testinputselect
    data:
      option: |
        {{ wait.trigger.event.data.action }}
mode: parallel

I tried using the alias for event_data its not working for me on Android.
Which device you are using to make it work ?

NOTE : I was able to use template in device action. i.e code

  - device_id: c5d2ecko3754253d5aoa8zab2ac86e98
    domain: mobile_app
    type: notify
    message: Test Message
    title: Test Title
    data:
      persistent: true
      actions: >
        {% set uris = ["", "", ""] %}  {% set actions = namespace(data = []) %}
        {% for action_title in state_attr('input_select.testinputselect','options') %}
          {% if action_title|length %}
            {% set uri = uris[loop.index - 1] %}
            {% set action = {
                  "action": action_title,
                  "title": action_title
              }
            %}
            {% set actions.data = actions.data + [action] %}
          {% endif %}
        {% endfor %} {{ actions.data }}

Works perfectly to send notification with notification actions as input. Only problem I am facing is getting the wait_for_trigger work