How to find out, who triggered a script?

Hi there,
so I came at the point, where I would like to make an announcement in my house. So the idea is, that you press a button on the dashboard, type in the announcement and then all speakers in the house speak this announcement. This is actually working pretty easily via scripts and fields.

Now, I also would like to play the announcement using the TTS message on the phones of my family members. But the person, who triggered the announcement, does not need to hear this again. So I need to find out in my script, who actually triggered the announcement / the script. I looked into Trigger Variables already, but the trigger is not defined in a script.

Any ideas, how I can solve this?

Thank you so much,

Tobias

I found something:
You can make use of

{{ context.user_id }}

What you end is getting the UUID of the user (stored in the user_id attribute). Since I only have 3 people in my family, I then do just an if statement:

{% if state_attr("person.tobias", "user_id") == context.user_id %}

There must be some nicer way by using selectattr, but I did not got this working.

So, topic solved!

1 Like

oh this is interesting. Nice little find there. I’d like to utilize something similar.

E.g. Send notification to the user that triggered the automation.

Just sifting through the documentation now.

Wait, so where can I add {{ trigger.to_state.context.user_id }} in an automation.

Hi Mike,

you can’t really make use of it in an automation, because the automation is triggered without a user. However, I came across an article, where someone used the browser_mod extension to find out, who last interacted with HA and assumed this as the “active” user (which should work up to some extend).

But if you trigger a script directly from the dashboard, you have that variable set.

1 Like
1 Like

Thanks for this. Will read it through