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?
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.
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?
- 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.
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