Send actionable notification including user name

Hi. I’ve tried many things for hours on end, and I am about to give up… unless some kind soul can help me…

Stripping down my requirements to the most basic part that I am having trouble with:

How can I send an actionable notification to a mobile phone that includes information about who generated that notification?

I can get notifications to send, that’s easy, and I can send them from several places (UI buttons, automations, scripts, etc). But all the ways I tried to get the user name into the notification text have failed.

I want to send a notification that says something like “User X is asking permission for something”, and can I do that?

Thanks in advance!

{% set p = states.person |
    selectattr('attributes.user_id', 'eq', trigger.to_state.context.user_id) |
    list %} {{ p[0].attributes.friendly_name if p | count == 1 else ''  |
    replace("None", "") }}
service: notify.notify
data:
  title: Centralina Allarme
  message: >-
    🔓 Sistema Disarmato {% set p = states.person |
    selectattr('attributes.user_id', 'eq', trigger.to_state.context.user_id) |
    list %} {{ p[0].attributes.friendly_name if p | count == 1 else ''  |
    replace("None", "") }} |
    {{now().strftime('%H:%M:%S')}}-{{now().strftime('%d/%m/%Y')}}
  data:
    ttl: 0
    priority: high
    actions:
      - action: CLOSE
        title: Segna come letto

Thanks… where do I put that into?

I assume it goes in automations.yaml, inside an “action” element of an automation.

Does it require any special tricks for it to accept and interpret the Jinja stuff?

Another question: in your case, where are you calling that from? I am calling it from a tap_action in a button-card. I have a feeling that the place where it is being called from might make a difference.

Put on your action automation.
Jinja???

immagine

I believe you can also call it from tap_action, but I use it inside an atomation

1 Like

Maybe you can create an automation that starts from the button you want

Jinja is the templating system that interprets those {{ }} blocks.

I see where you’re putting it, it’s like I thought, I just explained it differently because I am editing the automations.yaml directly.

I am also putting it in an automation, but my question is, how is that automation triggered in your case?

in my case when you turn off the alarm from alarm control panel.
Create an automation that starts when you click a button and put code it into actions

I do have it calling an automation from a tap_action, but I get this error

2023-03-31 22:19:07.149 ERROR (MainThread) [homeassistant.components.automation.ask] 
ask: Error executing script. Error for call_service at pos 1: 
Error rendering data template: UndefinedError: 'dict object' has no attribute 'to_state'
2023-03-31 22:19:07.150 ERROR (MainThread) [homeassistant.components.automation.ask] 
Error while executing automation automation.ask: Error rendering data template: 
UndefinedError: 'dict object' has no attribute 'to_state'

your automation is wrong.
Create from web page ui
or:

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - button.button1
condition: []
action:
  - service: notify.notify
    data:
      title: Button pressed
      message: >-
        Button pressed by {% set p = states.person |
        selectattr('attributes.user_id', 'eq', trigger.to_state.context.user_id)
        | list %} {{ p[0].attributes.friendly_name if p | count == 1 else ''  |
        replace("None", "") }} |
        {{now().strftime('%H:%M:%S')}}-{{now().strftime('%d/%m/%Y')}}
      data:
        ttl: 0
        priority: high
        actions:
          - action: CLOSE
            title: Sign read


1 Like

Sorry I don’t use tap_action

Ok, so I want to try and do it exactly like you do it, until I an get it to work. Then I can start slowly changing parts to fit my needs.

I suspect that my problem is actually in the trigger part, because I’m using tap_action, I don’t have a context.

So I want to try and use your trigger section. A really basic question, though… I am at a point where I am really confused with all this. Where do you define button.button1? What kind of entity is it?

Thank you for your patience helping me out :tada:

Never mind, I got it! Yoo-hoo!!!

I had to create an input_button helper. Then add this card to a dashboard:

      - type: button
        entity: input_button.ask
        name: Ask
        show_name: true
        show_icon: true
        icon: mdi:account-question

And my automations.yaml has this

- alias: ask
  id: '1678712538888'
  description: ''
  mode: single
  trigger:
    - platform: state
      entity_id:
        - input_button.ask
  condition: []
  action:
  - service: notify.mobile_app_oneplus_pgr
    data:
      title: Centralina Allarme
      message: >-
        🔓 Sistema Disarmato {% set p = states.person |
        selectattr('attributes.user_id', 'eq', trigger.to_state.context.user_id) |
        list %} {{ p[0].attributes.friendly_name if p | count == 1 else ''  |
        replace("None", "") }} |
        {{now().strftime('%H:%M:%S')}}-{{now().strftime('%d/%m/%Y')}}

I (finally!) got a nice little message with the user name in it. Now I can modify everything to fit my needs, and add the actionable notification buttons, etc.

Thanks for your kind help, without you I’d still be hitting my head against the wall…

And here is the lesson for the Internet regarding getting user names in Home assistant automations:

You need a context. And that context has to be in the trigger of the automations.

You might think your automation doesn’t need a trigger, because you can launch it perfectly well from somewhere, such as a tap_action or hold_action… nope. That won’t work. Just build your system a bit differently, so that the automation includes the trigger explicitly. I suggest an input_button helper.

another way

Notify name:

service: notify.notify
data:
  title: NFC TAG
  message: >-
    Test NFC attivato da {% set person = states.person|selectattr('attributes.user_id','eq',trigger.event.context.user_id)|first%}{{ person.attributes.friendly_name }}
  data:
    ttl: 0
    priority: high
    actions:
      - action: CLOSE
        title: Segna come letto

If Davide pass:

condition: template
value_template: >-
  {% set person = states.person|selectattr('attributes.user_id','eq',trigger.event.context.user_id)|first%}
  {{ 'Davide' in person.attributes.friendly_name }}
enabled: true

Device name:

service: notify.notify
data:
  title: TEST device
  message: >-
    {% set dev_name =  expand(device_entities(trigger.event.data.device_id)) | selectattr('domain', 'eq', 'device_tracker')  | map(attribute='name') | join  %} Test - device: {{ dev_name }}
  data:
    ttl: 0
    priority: high
    actions:
      - action: CLOSE
        title: Segna come letto