How to get the mobile name from device_id from the event mobile_app_notification_cleared?

Here is my scenario.
My script sends in parallel a notification to 2 mobiles.
Then the notification gets swiped (cleared) one one device.
The event mobile_app_notification_cleared is then sent to HA.
I want to know which phone swiped the notification. I do not want to know which user, but which phone. The user may be logged in on both phones.

Here is an example of the event that is received.
How do I get from there to the name of the mobile?

event_type: mobile_app_notification_cleared
data:
  message: blablabla
  server_id: "2"
  webhook_id: 12345678890abcdefghijklmopqrstubvwxyz1234567890
  device_id: 1244567890abcdefgh
origin: REMOTE
time_fired: "2025-02-24T20:14:38.425183+00:00"
context:
  id: 01JMWSHVASH7WHPXCGN2ERVRVW
  parent_id: null
  user_id: 12345567890123456123456789012345

If you are listening for the event in the same script, you would use a Wait for trigger then use the wait variable to access the data:

{{ device_attr(wait.trigger.event.data.device_id, ā€œnameā€) }}

There doesnā€™t seem to be a way to get the device name from mobile device ID that is available.

To test your suggestion, I tested this dummy script:

sequence:
  - action: notify.mobile_app_myphone
    metadata: {}
    data:
      message: test123
  - wait_for_trigger:
      - trigger: event
        event_type: mobile_app_notification_cleared
        event_data: {}
  - action: notify.mobile_app_myphone
    metadata: {}
    data:
      message: "{{ device_attr(wait.trigger.event.data.device_id, 'name') }}"
alias: ZZZZZZZZZZZ
description: ""

But it doesnā€™t work. Thereā€™s an error trying to find the name:

Erreur : string value is None for dictionary value @ data['message']

In fact, I could have been more clear. What I really want to do, is wait for a trigger where a specific phone will clear the notification.

So is there a way to programmatically find the device_id I am looking for in the wait for trigger event, or should I necessarily establish manually a mapping in advance?

It looks like you will need to set up a mapping of your ownā€¦ I canā€™t find any way to get it directly from the device ID provided in mobile app events.

You may want set it up as a script of itā€™s own so you have a single place you can query if you plan on using the data in other scripts and automations.

The following script example is from @petro, the full discussion can be found in the discord link above:

alias: Resolved Device ID
fields:
 device_id:
   description: The mobile device id passed from the automation trigger 
   example: d818035824cf47ea
variables:
 devices:
   'd818035824cf47ea': Josh
   '009a0965d73ee8f7': MJ
 resolved_device_id: >
   {{ {'value': devices.get(device_id, 'unknown')} }}        
sequence:
 - stop: End
   response_variable: resolved_device_id

So your test script would become:

sequence:
  - action: notify.mobile_app_myphone
    metadata: {}
    data:
      message: test123
  - alias: Wait for notification to be cleared 
    timeout: "00:02:00"
    wait_for_trigger:
      - trigger: event
        event_type: mobile_app_notification_cleared
        event_data: {}
  - condition: template
    value_template: "{{ wait.trigger is not none }}"
  - action: script.resolved_device_id
    data:
      device_id: "{{wait.trigger.event.data.device_id}}"
    response_variable: mobile_name
  - action: notify.mobile_app_myphone
    metadata: {}
    data:
      message: "{{ mobile_name.value }}"
alias: ZZZZZZZZZZZ
description: ""

IIRC, the cleared event is only posted when the message is cleared without being opened, so you might want to keep that in mind if you find you arenā€™t getting as many triggers as you expect.

1 Like

In fact the cleared event is sent regardless of ā€œopeningā€ the notification or not.

I just found a solution for my problem. Yours is interesting, but in my particular use case (which I did not specify, I am a bad person), it is a script that is called twice in parallel. The script sends the exact same actionable notification to 2 phones. It is not the notify command that is directly called in parallel.

This allows the script to ā€œpingā€ the phone with the confirmation option and get the device_id once the notification is sent.

This way, when the notification is cleared, each of the 2 instances of the script will be able to discriminate if it is for its own instance of the notification or not, and then continue with appropriate actions for the one that has been swiped, but continue waiting for the other one.

Hereā€™s the test script for the ā€œpingā€ thing:

sequence:
  - action: notify.mobile_app_myphone
    metadata: {}
    data:
      message: test123
      data:
        confirmation: true
        actions:
          - action: action1
            title: Done
          - action: action2
            title: Cancel
  - wait_for_trigger:
      - trigger: event
        event_type: mobile_app_notification_received
        event_data: {}
    enabled: true
  - variables:
      this_device: "{{ wait.trigger.event.data.device_id }}"
  - wait_for_trigger:
      - trigger: event
        event_type: mobile_app_notification_cleared
        event_data:
          device_id: "{{ this_device }}"
  - action: notify.mobile_app_myphone
    metadata: {}
    data:
      message: "{{ this_device }}"
alias: ZZZZZZZZZZZ
description: ""

Incidentally, I get the link between ā€œmyphoneā€ and the device_id, on the fly, without any pre-mapping.

Mh. What I thought was a solution is not in fact a working one. Thatā€™s because the wait for trigger event mobile_app_notification_cleared will not discriminate which phone it comes from.
And Iā€™m certainly guilty of the XY problem here.

In case someone has a similar issue:
My goal was to have a script that is run twice, from a single ā€˜in parallelā€™ command. This script will send an actionable notification to 2 phones. If one of them clicks on ā€œDoneā€ or ā€œCancelā€, it will be done or cancel for both. But if one of them dismisses the notification, the dismissal will be just for that specific phone.

The way to do it is to use the tag option and define it as being phone specific, while defining the ā€œDoneā€ and ā€œCancelā€ actions in a non phone specific manner.

Hereā€™s a model of the code.

sequence:
  - variables:
      action_1: "{{ 'done' ~ context.id }}"
      action_2: "{{ 'cancelled' ~ context.id }}"
      tag: "{{ myphone ~ context.id }}"
  - action: notify.mobile_app_{{ myphone }}
    data:
      message: test123
      data:
        tag: "{{ tag }}"
        actions:
          - action: "{{ action_1 }}"
            title: Done
          - action: "{{ action_2 }}"
            title: Cancel
    alias: Send notification
  - wait_for_trigger:
      - alias: goal_completed
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_1 }}"
        id: goal_completed
        trigger: event
      - alias: goal_cancelled
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_2 }}"
        id: goal_cancelled
        trigger: event
      - alias: Notification dismissed
        event_type: mobile_app_notification_cleared
        id: dimissed
        event_data:
          tag: "{{ tag }}"
        trigger: event
alias: zzzzdemo
description: ""
fields:
  myphone:
    selector:
      select:
        options:
          - John
          - Johnny
          - Jonas
    name: Which phone?