We make use of Actionable Notifications for a handful of alerts that require attention. In one scenario, (via a window/door sensor) we each see an Actionable Notification on our mobile devices advising that the mail was delivered. This notification contains a single Action allowing one of us to tap on it to set the mail as “Checked” in HA (via boolean). When one of us taps on this Action, the original notification is cleared via automation and a new notification is sent to our devices advising that the mail was checked to prevent multiple visits to the mailbox by each of us.
This is working well, however, if I am the one who checked the mail I would like for everyone else except for me to see the “Mail was checked.” notification - since I’m the person who tapped the Action I’m already fully aware. This would save me the extra swipe-away of the notification.
I searched the forums and came up with many answers advising how to get the user_id of the user who activated a trigger. Unfortunately, the solutions I found which stated to use trigger.to_state.context.user_id
did not seem to apply to event type triggers.
Using the Automation’s Show Trace feature, my output was something similar to this:
this:
entity_id: automation.action_mailbox_actions_event_handler
state: 'on'
attributes:
last_triggered: '2022-05-08T04:17:23.528988+00:00'
mode: single
current: 0
id: '1647420696426'
friendly_name: 'Action: Mailbox Actions Event Handler'
last_changed: '2022-05-08T04:17:08.814302+00:00'
last_updated: '2022-05-08T04:17:23.583657+00:00'
context:
id: 0180a1e2e42069c1a1eb87e38e2cbe8e
parent_id: 0180a1e242069cf75f8d96bdcdbb5cdf
user_id: null
**trigger**:
id: '0'
idx: '0'
platform: event
**event**:
event_type: mobile_app_notification_action
data:
tag: mail_not_checked
title: Mail Delivered
message: Mail needs to be checked.
action_1_title: Set Mail Checked
action_1_key: set_mail_checked
action: set_mail_checked
device_id: 3af1b42069e44214
origin: REMOTE
time_fired: '2022-05-08T04:55:18.782886+00:00'
**context**:
id: 0180a2059dfe42069929bc57a37f7eed
parent_id: null
**user_id**: ***[u]c784646142069412930c070176c9b19c[/u]***
description: event 'mobile_app_notification_action'
I’ve highlighted the areas of interest using asterisks. So it would seem that, according to Show Trace output, mobile action event type triggers’ user_id info could be acquired using trigger.event.context.user_id
, right? Well… not quite, but we are close!
Using dev tools to listen to event type mobile_app_notification_action
and then tapping the Action on my mobile notification revealed the following:
{
"event_type": "mobile_app_notification_action",
"data": {
"tag": "mail_not_checked",
"title": "Mail Delivered",
"message": "Mail needs to be checked.",
"action_1_title": "Set Mail Checked",
"action_1_key": "set_mail_checked",
"action": "set_mail_checked",
"device_id": "3af1b42069e44214"
},
"origin": "REMOTE",
"time_fired": "2022-05-08T04:55:18.782886+00:00",
"context": **{**
"id": "0180a2059dfe42069929bc57a37f7eed",
"parent_id": null,
"user_id": "c784646142069412930c070176c9b19c"
**}**
}
Great! This helps us along. Notice the braces above around the data contained within context - this tells us that context
is a Python data type of Dictionary and gives us the info we need to craft a template to obtain the values within.
To get the user_id from this event, such as for the purpose of using as an Automation Template Condition in my scenario, it looks like this:
{{ trigger.event.context["user_id"] == "c784646142069412930c070176c9b19c" }}
You can obtain your desired comparison user_id by navigating to Settings → People → Users, then clicking on the desired username.