What I want to achieve:
I want to create a button which triggers a push notification to Companion App. In this notification’s message I would like to add a “signature” - what user clicked that button.
What I have currently:
I have created a button which calls a service on tap.
tap_action:
action: call-service
service: notify.mobile_app_phone_john
target: {}
data:
message: >-
Some important message - {{
states.person|map(attribute="attributes.friendly_name")|first }}
title: Important note
I have tested it using my account and it looked to work. It is adding my name to the message.
What’s the issue:
The issue with this is that when I logged in as another user to test this out, I still received name from my admin account, not from the one I pushed the button.
My question
Can I read user’s name and insert it in notification’s message? If so, how should I update my service call?
Please let me know if I should add some more info to this summary.
Thank you for your time (in advance)
I was able to find a good solution to my issue. I have created a script with variables which adds signature to my message. This script is then called when button is pressed.
Note: In script scope I was able to use context.user_id. This is not available when creating service call in button’s config.
Note: I even created a variable to mobile phone which is used to send notification to.
Button config (only tap_action part):
tap_action:
action: call-service
service: script.notify_with_signature
target: {}
data:
var_url: /lovelace/dashboard-name # Open this dashboard when clicked
var_clickAction: /lovelace/dashboard-name # Open this dashboard when clicked
var_title: Notification title here
var_message_before: This message was pushed by
var_message_after: . Have a great day!
var_tag: some-tag-to-keep-it-organized
var_mobile_app: mobile_app_phone_john
This way I can create multiple notifications using this one script.
Note: In newer Home Assistant install you can add fields to your script. It makes passing variables to script more robust.