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:

service: notify.mobile_app_gsm
data:
  title: test
  message: >
    {{ states.person|selectattr("attributes.user_id", "==",
    trigger.to_state.context.user_id)|map(attribute="attributes.friendly_name")|first
    }}"

gives me : Error rendering data template: UndefinedError: ‘dict object’ has no attribute 'to_state

just a guess… the error “no attribute ‘to_state’” means that trigger variable has no attribute ‘to_state’… so i’m guessing that if this is in an automation, you either manually ran it (ie, the trigger didn’t fire), or the automation was triggered by a trigger that wasn’t a state trigger… and therefore it has no ‘to’ and ‘from’ state… but just a guess since you don’t have the automation posted nor how it happened.

alias: Ruimte1_nfctag
description: ""
trigger:
  - platform: tag
    tag_id: 0b3128d7-a0fc-4f56-a7fa-ad6041ea207c
condition: []
action:
  - service: notify.mobile_app_gsm_*name*
    data:
      title: test
      message: >
        {{ states.person|selectattr("attributes.user_id", "==",
        trigger.to_state.context.user_id)|map(attribute="attributes.friendly_name")|first
        }}"
mode: single

this is my automation… The automation gets triggerd. but I don’t understand the code from te message ( I copied it, so I don’t know what I am doing here)

I have a entity person.name , state unknown. and attributes editable: true
id: name
device_trackers: device_tracker.gsm_X
friendly_name: name

What i really want is…

If person x scanned
Vacuüm settings x
Light x
Etc
If person y scanned
Vacuüm settingsy

Therefore i need to know who scanned

trigger.to_state and trigger.from_state are only defined if the trigger that invoked the automation was a state trigger.

this page goes into detail on that.

it sounds to me like you’re trying to do something quite different than what the original question is asking. and certainly at least different in terms of the context (they’re using state triggers). if that’s true, you should start a new thread and explain exactly what you’re trying to do, include the work you’ve done so far and also might be helpful in sharing your level of knowledge w/ yaml. it looks like you started w/ home assistant 6 years ago? which hints that you may be pretty comfortable with ha automation? but only hints…

1 Like