Right now there is no Triggered by User condition is automations. Every user in HA has an entity, so it should be straight forward to make it selectable in the UI.
I use templates right now to map the user_id to the person (how scanned the NFC tag), which is painful when you use it in many automations.
The data is there and this can be done relatively easily.
I’d argue that we also need Triggered by Automation and Triggered by Script, or possibly just Triggered by where you specify the entity: script, scene, automation, etc.
Both triggers would need to accept lists of people/users or automations/scripts/scenes(?).
To be complete I’ll share my background for this WTH:
I’d like to know in my automation whether my light was switched on/off by an automation or by a user pressing a physical button (in this example a Fibaro dimmer, with physical switch attached) and specify different actions based on this. For that I’d like triggers or conditions to get insight in the originator/actor of the action.
It seems like a quick win because in the device history I can already see the originator/actor of an action:
Citaat
Is ingeschakeld getriggerd door automatisering ______TST triggered
10:53:42 - 8 minuten geleden
Is uitgeschakeld
10:51:08 - 10 minuten geleden
Where the first time an automation was the reason for switching on the light, and the second example shows the manual (with physical switch) switching off of the light.
The proposal of ‘Triggered by …’ seems to match my need. This topic gets my vote!
I’m surprised there is no way to use this information in automations yet.
My Hot Tub’s Economy switch, that depends on a third party integration, will turn off on its own randomly. I have an automation that alerts me when this happens when power is expensive so I can decide if I need to turn it on again.
If I could use the “triggered by” information from the logbook, I could automate this better by automatically turning it back on when the state change was not triggered by anything.
This fails as a condition in the choose action, but confirmed that this works in conditions.
The hang-up appears that user_id is a changed variable in the choose action. When tracing, the parent_id references the id where the user_id of interest was, and overwrites user_id with “null”:
You added quotes around the object’s declaration which turned it into a literal string. Meaning that condition would always pass because the literal string t r i g g e r … will never equal A A A A A…
If you want the to compare the actual user_id, the objects delclaration must be unquoted.
I appreciate the response, but I cannot get it to pass the condition.
When I look at the trace the changed vars for trigger it lists my user_id, but the statement is outputting false under conditions with no changed vars under conditions.
I think this is all of naught anyway because even if I were able to get this to pass in the condition, there is no way to differentiate between multiple conditions in that section.
The actions choose function assigns a parent_id (which is the trigger.event.context.id you are currently seeing) to trigger.event.context and also assigns ‘null’ to user_id, so there is no way to use a functioning condition there anyway unless I can reference the original changed variable.
What are you trying to achieve though? The typical way to do this is to have a template sensor to store the user when the tag is scanned, and then you can refer to the state of the template sensor in any conditions that need to know who scanned the tag.
The userid will obviously be empty, because how would Home Assistant know who the user was? The ideal way to do it would be:
template:
- trigger:
- platform: event
event_type: tag_scanned
sensor:
- name: Last Tag
state: "{{ trigger.event.data.tag_id if trigger.event.data.tag_id is defined else 'None' }}"
Now in your automation, you simply use the state of sensor.last_tag in your choose options to decide which user scanned the tag.
- trigger:
- platform: event
event_type: tag_scanned
sensor:
- name: Last Tag
state: "{{ trigger.event.context.user_id if trigger.event.context.user_id is defined else 'None' }}"
This sets the user_id as the state of the sensor and I used the state condition to validate the user_id:
action:
- choose:
- conditions:
- condition: state
entity_id: sensor.last_tag
state: (user_id to validate)
Thank you very much to you and Petro! I found a lot of posts on this with no solution. I have definitely some useful knowledge gained along the way.
Apologies but am I missing something? I’ve created a template sensor but in the preview I get “This template does not listen for any events and will not update automatically.” I’m still relatively new to Home Assistant so might be missing something about how to set it up.
This is what I have pasted into my configuration.yaml:
## Template
template:
# Last User to Scan NFC Tag
- trigger:
- platform: event
event_type: tag_scanned
sensor:
- name: Last Tag
state: "{{ trigger.event.context.user_id if trigger.event.context.user_id is defined else 'None' }}"
# Sensors
- sensor: !include sensors.yaml