Can I create a button that triggers an automation based on who pressed the button?

I would like to allow lock access based on presence but I can’t see how to get the user that triggered a button in the interface to check if they are near the lock. Is something like that possible?

1 Like

Use {{ context.user_id }} in a script that you call with a button, that id will be the id of the user that called the script.

2 Likes

You can use the new trigger ids here as well

@ludeeus
Can you tell me as how to get the user’s friendly_name from context.user_id if I want to use it in a script that I call with a button. Thanks

Edit: My intention is to use it in the service notify.alexa_media in a voice announcement.

FYI I saw your reply in this thread but I did not understand how to get the button entity id on love-lace UI that triggered the script.

There may be a better method, but this seems to work:

service: notify.alexa_media_living_room_dot
data:
  message: >-
    {{ expand(states.person) | selectattr('attributes.user_id', 'eq', context.user_id)
    | map(attribute='attributes.friendly_name')|first }} pressed a button.
  data:
    type: tts
1 Like

Thank you so much let me try that out.

Edit:
It worked. Thanks again.

{% set x = context.user_id %} 
{{ expand(states.person) | selectattr('attributes.user_id', 'eq', x) | map(attribute='attributes.friendly_name')|first }}