Using user's name in notification message with button's service call

Hi,

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.

Script config:

alias: notify_with_signature
sequence:
  - service: notify.{{var_mobile_app}}
    data:
      title: "{{var_title}}"
      message: >-
        {{ var_message_before }}{% set x = context.user_id %} {{
        expand(states.person) | selectattr('attributes.user_id', 'eq', x) |
        map(attribute='attributes.friendly_name')|first }}{{ var_message_after
        }}
      data:
        tag: "{{ var_tag }}"
        url: "{{ var_url }}"
        clickAction: "{{ var_clickAction }}"
mode: single

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.

I hope it will help others!

1 Like

use an id: in your automation. This tags the triggering event and allows you to create separate actions for each.

Below is an automation I use. When received it checks if it was sent from iphone1 or iphon2 and based on the device opens a specific garage door.

sourceDeviceID is used to identify which phone triggered the action and an “id”, Iosgarage1 or Iosgarage2, are used to identify the trigger source.

The actions have condition to fire only if triggered by specified “id”.

Yours is better for multiple but if you only need to account for a few variables, names in your case, mine may be simpler.

alias: ACTION_Carplay
description: ""
trigger:
  - platform: event
    event_type: ios.action_fired
    event_data:
      actionName: Garage
      sourceDeviceID: iphone2
    id: Iosgarage2
  - platform: event
    event_type: ios.action_fired
    event_data:
      actionName: Garage
      sourceDeviceID: iphone1
    id: Iosgarage1

condition: []
action:
  - if:
      - condition: trigger
         id:
          - Iosgarage2
    then:
      - service: cover.toggle
        target:
          entity_id: cover.ratgo01_f9ee3d_door
        data: {}
    else:
      - if:
          - condition: trigger
            id:
              - Iosgarage1
        then:
          - service: cover.toggle
            data: {}
            target:
              entity_id: cover.ratgo02_1beb12_door
mode: single

1 Like