Actionable Notification with multiple options to choose from

Hello,

I am not sure of this is the correct place for my question but it is regarding actionable notifications send to the mobile app so here I go.
What I want to try to achieve is to be able to get an actionable notification with multiple options to choose from and based on my choise a certain service is called or other action is taken.
The notification is not a probem and I can get multiple buttons.
But when in Actions I configure the action type to be “wait for trigger” and I define also a trigger id, it is not possible to select those trigger id’s when I come into the Action part where I first select choose and then I want to set a condition “Triggered by”. Home Assistan just says “No triggers have an ID set. You can set an ID using the trigger menu button.”
Below is the yaml code of a simple test automation which gives the error that it has no trigger id’s found.

description: ''
trigger:
  - platform: sun
    event: sunrise
    offset: 0
condition: []
action:
  - service: notify.mobile_app_s21_stef
    data:
      message: Test yes or no
      data:
        actions:
          - action: 'YES'
            title: 'Yes'
          - action: 'NO'
            title: 'No'
  - wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: 'YES'
        id: 'Yes'
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: 'NO'
        id: 'No'
    timeout: '00:01:00'
    continue_on_timeout: false
  - choose:
      - conditions:
          - condition: trigger
            id: ''
        sequence: []
    default: []
mode: single

type or paste code here

While you don’t have to use variables to modify your actions like shown below, it is a good habit to get into so you don’t have wait trigger errors in cases where more than one actionable notification is waiting.

description: ''
trigger:
  - platform: sun
    event: sunrise
    offset: 0
condition: []
action:
  - alias: Set up variables for the actions
    variables:
      action_yes: '{{ "YES_" ~ context.id }}'
      action_no: '{{ "NO_" ~ context.id }}'
  - alias: Ask to stop the Echo
  - service: notify.mobile_app_s21_stef
    data:
      message: Test yes or no
      data:
        actions:
          - action: '{{action_yes}}'
            title: 'Yes'
          - action: '{{action_no}}'
            title: 'No'
  - alias: Wait for a response
    timeout: '00:01:00'
    continue_on_timeout: false    
    wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: '{{ action_yes }}'
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: '{{ action_no }}'
  - alias: Perform the action
    choose:
      - alias: Do something on YES
        conditions:
          - condition: template
            value_template: '{{ wait.trigger.event.data.action == action_yes }}'
        sequence:
          - DO SOMETHING
      - alias: Do something else on NO
        conditions:
          - condition: template
            value_template: '{{ wait.trigger.event.data.action == action_no }}'
        sequence:
          - DO SOMETHING ELSE
    default: {}
mode: single

While your example did not continue on timeout, if you did need something to happen if no one responds to the notification before timeout you can put those actions in the default section of the Choose action.

1 Like

You can’t use the trigger type condition there. That type of condition only looks at the triggers to the automation itself and checks that the id provided matches the one that started this automation. Your wait trigger ID is available though, it’s set in the wait variable. You just have to use a template type condition to check against it like so:

- choose:
    - conditions: "{{ wait.trigger.id == 'Yes' }}"
      sequence: []
2 Likes

Yes ! Thank you that works perfecty.
Ah now I can create some fun automations with presence detection :grinning_face_with_smiling_eyes:
Thanks a lot !

@Didgeridrew that’s an interesting approach, thanks for sharing!
However, my use case scenario involves actionable water pumps.
I’m wondering, in order to have more than two actions, i.e. let’s say I wanna automate various ‘on’ times (my chosen actions are defined as turn on-wait-turn off), depending on soil status:

  • action_pump1_30sec
  • action_pump1_1min
  • action_pump2_30sec
  • action_pump2_1min

then I guess I’d just have to define 4 variables instead of 2, then define 4 corresponding data actions in the notification service, then have 4 matching wait_for_trigger’s and then 4 “choose” actions, right?

(I guess basically my question is: is it possible to have, like, 4 actionable buttons in a notification?)

Depends on the your device type. From the docs:

Android allows 3 actions.
iOS allows around 10 actions. Any more and the system UI for actions begins having scrolling issues.

I don’t know the limit on html5 notifications, doc doesn’t seem to say.

Alternatively you can use a single action with type REPLY. Then when the action is clicked it shows a prompt and asks the user to type in a value. Unfortunately its just a textbox though, not a dropdown so you’ll have to remember the correct values to type. I don’t believe you can have two separate reply actions (one for pump 1 and one for pump 2)

1 Like

Another option to make this work:

  1. Define all of your triggers up top in the main trigger section. This includes the wait-triggers that you will also set in the “wait for a trigger” action. Make sure you give them the same event id and action name in both places. (You need to use event id on all the triggers to make this work).
  2. In the “Choose” action, set the condition to be “Triggered by ID” (second from the bottom) for all of your choices. Pick the corresponding trigger for each action.
  3. Then, at the very bottom of the script, change the mode from single to parallel. In single mode, the wait-triggers only fire off the wait-actions. In parallel mode it will fire off the wait-actions and the choose-actions.

To set event ids on triggers: in the visual editor, at the top right of each trigger, click on the 3-dot menu and select Edit ID. Alternatively, in the yaml editor, add “id: <ID_NAME>” at the same indent as platform.

Edited to add how to set event id, since that did not appear obvious to OP based on context.

Is it possible to have one of the notification buttons launch a custom popup that would have more buttons? :slight_smile: (And then a “back” button :slight_smile: )

You might as well just use the reply option so you can enter any text and have that sent back to the server