Alert component + iOS Actionable Notifications. This seems convoluted

I’ve been trying to get this to work tonight and I’m at a point where this feels entirely too convoluted to be correct. Here’s what I’m trying to accomplish:

  • Alert triggered
  • Notification sent
  • Action slected
  • Stuff happens

I’ve gotten a test setup with a simple input_boolean that triggers an alert, the alert successfully gets delivered to my phone, but the events never trigger the automation that acts on the event. The order of operations within Home Assistant (from my understanding), is as follows:

  • Create item to be monitored
  • Create alert that monitors item
  • Create notification group that can be accessed by alert component
  • Create iOS notification actions
  • Create automation to act upon ios.notification_action_fired events

Again, this feels really convoluted, and I’m either grossly missing something or so far off that someone can hopefully point out my stupidity.

Here’s the test yaml that I’ve gotten so far.

---
### Alert Test ###

input_boolean:
  alert_test:
    name: Alert Test
    icon: mdi:alert-circle-outline

alert:
  alert_test:
    name: Alert Test
    entity_id: input_boolean.alert_test
    state: "on"
    repeat: 1
    notifiers:
      # - mobile_app_ians_iphone
      - alert_test

notify:
  - id: alert_test
    name: Alert Test
    platform: group
    services:
      - service: mobile_app_ians_iphone
        data:
          title: Alert Test
          message: This is an alert test
          data:
            push:
              category: alert_test

ios:
  push:
    categories:
      - name: Alert Test
        identifier: 'alert_test'
        actions:
          - identifier: 'alert_test_yes'
            title: "Yes"
            activationMode: 'background'
            authenticationRequired: true
            destructive: true
            behavior: 'default'
          - identifier: 'alert_test_no'
            title: "No"
            activationMode: 'background'
            authenticationRequired: true
            destructive: false
            behavior: 'default'

automation:
  - id: test_alert_test
    alias: "Test - Alert - Test"
    trigger:
      platform: event
      event_type: ios.notification_action_fired
      event_data:
        actionName:
          - alert_test_yes
          - alert_test_no
    action:
      - choose:
        - conditions: >
            "{{ trigger.data.actionName == "alert_test_yes" }}"
          sequence:
            - service: input_boolean.turn_off
              target:
                entity_id: input_boolean.alert_test
        - conditions: >
            "{{ trigger.data.actionName == "alert_test_no" }}"
          sequence:
            - service: input_boolean.turn_on
              target:
                entity_id: input_boolean.alert_test

What am I missing here?

Have a look here. For one, your categories aren’t named as required.

There’s an ‘e’ at the end. You may remove it or change your code at notifiers:

No clue why I removed the title fields. I believe that’s what you were referencing. I know a lot of people seem to use upper case for the action identifiers, but the documentation does specify that lower case is valid.

And this is why I needed a sanity check. It was getting late and I was running out of steam. The weird part is that the actionable notification worked previously. I must have made that mistake at some point.

1 Like

Okay, so I’m to the point where the automation isn’t triggering. If I go into the dev tools and listen for the ios.notification_action_fired I do see the event from when I select a notification action.

{
    "event_type": "ios.notification_action_fired",
    "data": {
        "actionName": "ALERT_TEST_YES",
        "categoryName": "alert_test",
        "sourceDeviceID": "ians_iphone",
        "sourceDeviceName": "Ian’s iPhone",
        "sourceDevicePermanentID": "xxx"
    },
    "origin": "REMOTE",
    "time_fired": "2021-04-14T14:55:53.196408+00:00",
    "context": {
        "id": "xxx",
        "parent_id": null,
        "user_id": "xxx"
    }
}

One thing I notice is that within the trigger for the automation is formatted as

    trigger:
      platform: event
      event_type: ios.notification_action_fired
      event_data:
        actionName:

whereas the JSON event has the actionName field nested within a data object. I’ve made the condition template "{{ trigger.data.actionName == "alert_test_yes" }}", but it does me no good if I can’t get the automation to trigger.

One possibility is that I am trying to trigger on an array of different actionName values rather than creating individual triggers for each, but I’m trying to consolidate as much as possible. As I’ve said before, it already seems really convoluted that I’ve had to create 5 separate components for this task.

I haven’t seen lists given to actionName until now. Have you come across this somewhere? I can understand that you might’ve deduced this as a possibility (it intuitively makes sense). Genuinely curious.

Since you say that you see the event under the dev tools: How sure are you it’s not triggering vs one of the choose sequences not executing? Perhaps for testing purposes, just have one simple action that creates a persistent notification with a message or the data to confirm whether it’s the trigger or the choose conditions (or some other mechanism you fancy, such as an input_text or writing to the system log).

If you eventually get this to work maybe bear in mind that it may only be working as a side effect and that it may break in the future.

Haven’t seen it before either. It does make sense, but it might also be the issue.

Checking the last triggered time for the automation entity itself. I triggered it manually last night, and that’s the only time it’s been triggered.

Ideally, I’d like to turn all of this into a blueprint that I could share with the community since so many people get hung up on actionable notifications, but I’m worried that I might have to to go down the route of creating individual automations for each notification action.

I’m looking at breaking the notification service itself off into a separate script that I can reuse. Carlo has a great example in his repo.

What happens is you remove this section? Does the trigger fire?

Removing the actionName values does trigger the event, but now it’s getting caught on the choose block. Here’s what I got in the trigger data.

Executed: April 14, 2021, 10:26:38 AM
trigger:
  platform: event
  event:
    event_type: ios.notification_action_fired
    data:
      actionName: ALERT_TEST_YES
      categoryName: alert_test
      sourceDeviceID: ians_iphone
      sourceDeviceName: Ian’s iPhone
      sourceDevicePermanentID: xxx
    origin: REMOTE
    time_fired: '2021-04-14T16:26:38.590070+00:00'
    context:
      id: xxx
      parent_id: null
      user_id: xxx
  description: event 'ios.notification_action_fired'
  id: '0'

Something that stands out is that the actionName is still converted to upper case. I changed my condition template to "{{ trigger.event.data.actionName | lower == "alert_test_yes" }}", but it still isn’t hitting one of the choose conditions. I changed it to upper case in both the identifier and template, and it’s still not hitting.

I changed the automation to the following… and it works.

automation:
  - id: test_alert_test
    alias: "Test - Alert - Test"
    trigger:
      platform: event
      event_type: ios.notification_action_fired
      event_data:
        actionName: 'ALERT_TEST_YES'
    action:
      - service: input_boolean.turn_off
        target:
          entity_id: input_boolean.alert_test

So, I’m thinking that I might have to do an automation block for every ios notification action…

Okay, I figured out you can trigger on the ios push category name. Now I’ve just got to figure out the templates for picking out the correct action names.

automation:
  - id: test_alert_test
    alias: "Test - Alert - Test"
    trigger:
      platform: event
      event_type: ios.notification_action_fired
      event_data:
        categoryName: alert_test

Current template is
"{{ trigger.event.data.actionName == "ALERT_TEST_YES" }}"
Looking at the documentation, it should match, but maybe I’ve got my string comparison operator wrong.

This is good to know.

I can’t see anything wrong myself. These are guesses but perhaps the data is still JSON, or the casing changed (e.g. to action_name).

As an alternative, you can pass your own data along. I know it’s redundant since the actionName is already there but until you can figure out how to get to it.

With the push notification:

  action:
    - service: notify.mobile_xxx
      data:
        title: "Some Title"
        message: "Some message."
        data:
          push:
            category: "some_category"
          action_data:
            some_key: "some_value"

When receiving the event.

  action:
    - service: some_service.some_action
      data:
        some_param: "{{ trigger.event.data.action_data.some_key }}"

I did a quick test and it worked.

ios:
  push:
    categories:
      - name: Test
        identifier: 'test'
        actions:
          - identifier: 'TEST'
            title: 'Test Action'
- alias: "Test actionName"
  trigger:
    - platform: event
      event_type: ios.notification_action_fired
  action:
    - service: persistent_notification.create
      data:
        title: Test actionName
        message: >-
          {{ trigger.event.data.actionName == "TEST" }}

Screenshot 2021-04-14 at 21.36.55