WTH: There is no "Triggered by User" condition is automations

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(?).

9 Likes

(Petro: thanks for linking my WTH to this one.)

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!

Not forgetting the NFC-tag triggered by ‘user’.

1 Like

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.

I want to have a different action when the switch turns off without any “triggered by” value

I am trying to get an NFC tag to trigger unique conditional actions using user_id and the choose action. I am using the following template:

{{'trigger.event.context.user_id' != 'AAAAAAAAAAAAAAA'}}

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”:

context:
  id: XXXXXXXXXXXXXXXXXXX
  parent_id: aaaaaaaaaaaaaaaaa
  user_id: null

Is there a way to reference the parent instead of the changed variable? How do I make this work?

{{ trigger.event.context.user_id != 'AAAAAAAAAAAAA' }}

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.

These are the changed vars for seen in the trace:

trigger:
  id: '0'
  idx: '0'
  alias: null
  platform: tag
  event:
    event_type: tag_scanned
    data:
      tag_id: A9
      name: Disarm Alarm
      device_id: Ae
    origin: LOCAL
    time_fired: '2024-01-13T15:52:41.868518+00:00'
    context:
      id: Ac
      parent_id: null
      user_id: AAAAA

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.

I want one NFC tag to scan and dependent on who scans it enter and disable the alarm (with a custom disarm code).

I tried defining a variable and it outputs “user_id: ‘null’” before it grabs it.

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.

Your condition should just be

- condition: template
  value_template: "{{ trigger.event.context.user_id == 'AAAAA' }}"

You can put it in a variable and see what the results are under the variables tab in the automation trace too.

variables:
  user_id: "{{ trigger.event.context.user_id }}"
  configured: AAAAA
  is_user: "{{ user_id == configured }}"

Then look at the values of everything in the trace.

I changed the template to:

  - 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.

1 Like

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.

Did you try to scan a NFC tag? Where is it telling you this?

The black out corresponds to the user_id of the user found in settings>people>users. This is the view from entities:

last_tag

Clicking on the cog I get this, but everything works as it should:

last_tag2

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

Apologies, I was trying to set this up as a template sensor using a helper but then realised it needed to go in configuration.yaml and all sorted!