Notification including the user who triggered the device

Maybe it is my wording, maybe it is an entity I never used… but I cannot find a way to notify me which user turned a specific device on.
The use case: I have a siren and I would like to get the user’s friendly name when it is starting through the companion app. I found a topic which is similar to my need but I don’t think that is what I need. I can find the events in the logs (even with user name), but I don’t have a clue how to access that info.

Any clues please?

in the triggered automation, if it came from a user, the trigger to_state will have the user_id in trigger.to_state.context.user_id

however it’s not the friendly name. it’s the identifier. so you’ll have to translate it. but at least you got the info :slight_smile:

that id is the same as what you find in the user_id attribute of each person.* entity.

Thank you for your quick response. I have to admit: I don’t even understand you :grimacing:

in the triggered automation, if it came from a user, the trigger to_state will have the user_id in trigger.to_state.context.user_id

Where do I have to look “in the triggered automation”? If the user switches on the siren via the dashboard or a widget, the only traces I find are in the logs.

no worries. can’t always tell what someone’s familiarity is with it all…

create an automation that also listens to the entity change. so for example i did this:

description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.testbool1
condition: []
action:
  - service: script.debugmsg
    metadata: {}
    data:
      debugtrace: test
      title: user in automation
      message: "{{ trigger.to_state.context.user_id }}"

that automation put the device id into my debug message. so do that for whatever entity that you’ve wired up to the button. when the button is hit, it’ll flip you entity and also call your script with the info you want.

let’s start there…

Assuming your user is associated with a person entity, to get the friendly user name:

message: "{{ states.person|selectattr('attributes.user_id', '==', trigger.to_state.context.user_id)|map(attribute='attributes.friendly_name')|first }}"

Thank you both for pointing me in the right direction. The yaml looks the following:

name: Notification Siren
description: ""
trigger:
  - platform: state
    entity_id:
      - siren.sirene
condition: []
action:
  - service: script.debugmsg
    metadata: {}
    data:
      debugtrace: test
      title: user in automation
      message: "{{ states.person|selectattr("attributes.user_id", "==", trigger.to_state.context.user_id)|map(attribute="attributes.friendly_name")|first }}"

However, I cannot even save it. This error appears:

Message malformed: extra keys not allowed @ data['name']

your " 's are not doing what you think they are in this:

 "{{ states.person|selectattr("attributes.user_id", "==", trigger.to_state.context.user_id)|map(attribute="attributes.friendly_name")|first }}"

the outer quotes you intend to quote the whole thing. but in reality they are gettung unquoted at ("

try this:

name: Notification Siren
description: ""
trigger:
  - platform: state
    entity_id:
      - siren.sirene
condition: []
action:
  - service: script.debugmsg
    metadata: {}
    data:
      debugtrace: test
      title: user in automation
      message: |
        {{ states.person|selectattr("attributes.user_id", "==", trigger.to_state.context.user_id)|map(attribute="attributes.friendly_name")|first }}"

you can also do things like us ’ instead of " for the outer (or vice versa) to help the parser understand what matches what. but i tend to just like doing it that way above.

1 Like

For future reference, the value of user_id isn’t always present. It depends on who or what executed the automation.

There’s a table in the following post that shows how a combination of three properties of the context object can be used to determine who/what triggered the automation.

Wow, thank you so much. I had to delete the first line though, I got the same error message again. But without it I could save it. Let’s see… I will not turn on the siren now to test :rofl: