Work with "Triggered by" in automations

How can i determine (in an automation) what triggered e.g. a Light to turn on?
In the Logbook i can see e.g. “turned on by userxyz/automationxyz”

If i could set up an automation “trigger: light1 turned on // condition: turned on = empty / turned on = not by userxyz/automationxyz” i could assume that its been turned on by the regular Wallswitch, and set it to 100% brightness.

I played around with Things like:

alias: _Test with Trigger IDs
trigger:
  - platform: state
    entity_id: light.color_temperature_light_5
condition: []
action:
  - service: notify.notify
    data:
      message: >
        EntityID: {{ trigger.entity_id }} Platform: {{ trigger.platform }}
        ContextID: {{ trigger.context_id }} ParentID: {{ trigger.parent_id }} 
mode: single

But couldnt identify helpful data.

How can i work with what/who triggered an event in automations?

This has come up before and I believe the answer was along the lines of:

if triggered by ‘null’, then it was wall switch / not HA, else it was HA / whatever the value is in ‘triggered by’.

That would do the Trick! But what is the corresponding Variable i should check?
By now i tried to find it out by Trial and Error with the Automation above and those Variables via Action/Notify (All with + without “.state” at the End):
{{ trigger.platform }}
{{ trigger.event }}
{{ trigger.entity_id }}
{{ trigger.from_state }}
{{ trigger.to_state }}
{{ trigger.context_id }}
{{ trigger.user_id }}
{{ trigger.parent_id }}
{{ trigger.event.data }}
{{ trigger.event.event_type }}

All threw out either nothing suitable, or nothing at all :-p

1 Like

Partially solved, yess!
Via trigger.to_state.context.user_id i can determine if its manually switched via ui. Thanks!

But i cant tell if its triggered by automation or wall-switched, both return “None”.
How can i differentiate between those two cases?

If context.id is not null and context.user_id and context.parent_id are null then it’s (very likely) the physical device that was responsible for the trigger.

Unfortunatly no Success - Switching the Device via an Automation, or Physically turning on, lead to the same (not distinguishable) Results:

context.user_id: None
context.parent_id: None
context.id: Value

Any other way to tell if it was physically powered/turned on?

Based on my own testing, this is combination of values that characterize each action:

Action id parent_id user_id
Physical Not Null Null Null
Automation Not Null Not Null Null
UI Not Null Null Not Null

A more comprehensive list of actions can be found here.

If you are saying that your testing shows that Physical and UI produce identical combinations then, unfortunately for you, there’s no available means of distinguishing between the two.

8 Likes

First of all: Thank you so much for your Effort!
Second: I got it working yeeha - nearly almost! :slight_smile:

One Problem, better said strange Behavior, i still dont get: For testing Purpose i hooked up a really basic Automation (At Time xx.xx toggle Device).
That appeared to be physical switching (ID != None, ParentID == None, UserID == None)
But with my already set up, more complex, Automations, they can be correctly identified.
WTF.
Sure, i can and will ignore that, as it doesnt matter by now.
Just to let you (and maybe others with similar Problems) know what caused the irritation (and, in fact, not correct Interpretation).

In Case somebody want to use my “Debugging-Automation”:
It reacts to State Changes for a single Entity. For Action it has those three Options (Automation, Hardswitched, UI), and throws out a Notification with a Human Readable Result.

alias: Test - what caused the State-Change?
description: ''
trigger:
  - platform: state
    entity_id: light.extended_color_light_2
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ trigger.to_state.context.parent_id == none }}'
          - condition: template
            value_template: '{{ trigger.to_state.context.user_id == none }}'
          - condition: template
            value_template: '{{ trigger.to_state.context.id != none }}'
        sequence:
          - service: notify.notify
            data:
              title: Likely Physical switched
              message: 'ParentID: None || UserID: None || ID: CONTENT'
      - conditions:
          - condition: template
            value_template: '{{ trigger.to_state.context.parent_id != none }}'
          - condition: template
            value_template: '{{ trigger.to_state.context.user_id == none }}'
          - condition: template
            value_template: '{{ trigger.to_state.context.id != none }}'
        sequence:
          - service: notify.notify
            data:
              title: Likely Automation switched
              message: 'ParentID: CONTENT || UserID: None || ID: CONTENT'
      - conditions:
          - condition: template
            value_template: '{{ trigger.to_state.context.parent_id == none }}'
          - condition: template
            value_template: '{{ trigger.to_state.context.user_id != none }}'
          - condition: template
            value_template: '{{ trigger.to_state.context.id != none }}'
        sequence:
          - service: notify.notify
            data:
              title: Likely UI switched
              message: 'ParentID: None || UserID: CONTENT || ID: CONTENT'
    default: []
mode: single

Last: Thanks again for Patience and Effort, really appreciate it! I just found out i still need to learn way more, as i clearly wouldnt have made it on my own. Great you helping guys are around, i hope soon i can give the community something back!

9 Likes

I have a follow up question on this. Is there a way to specify which device triggered the automation? I have some users who use multiple devices. Other than creating different users for each device, is there a way to do this?

1 Like

I may have at least some insight into your “strange behavior” - it ties to the type of trigger event in the parent - please see this post in another thread on the same topic - would love to know if anyone has any new insights or ideas.

I have a switch light on UI:

image

And i always got in Node Red:

image

It sounds impossible to get the user who trigger the event from HA in Node Red as well.

I found several topics in the forum but no one give me the answer for this as well :frowning:

I’ve been using this method and recently looked at it again for a new use case.

I found an ambiguous case. It doesn’t happen under normal conditions, but perhaps still worth pointing this out.

If you manually run an automation via the UI (i.e. it’s not triggered automatically), then it will look the same as for the physical case.

Another thing I wanted to point out is that one must be careful which context one uses, since this.context and context won’t yield the same results.

2 Likes

This method doesn’t work (anymore). I wrote some automations and for triggerd by automation and device the variables a defined the same.

Show your config. There are different ways of achieving this.

Here is condition section:

- condition: template
  value_template: "{{ input_allow_device_trigger and trigger.to_state.context.id != none and trigger.to_state.context.parent_id == none and trigger.to_state.context.user_id == none }}"
- condition: template
  value_template: "{{ input_allow_automation_trigger and trigger.to_state.context.id != none and trigger.to_state.context.parent_id != none and trigger.to_state.context.user_id == none }}"
- condition: template
  value_template: "{{ input_allow_ui_trigger and trigger.to_state.context.id != none and trigger.to_state.context.parent_id == none and trigger.to_state.context.user_id != none }}"

I did log the variables using a automation and let an automation toggles a light by time trigger. There is a bug with timer and template triggers. The context is not set correctly. Most of my blueprints and automations are using template triggers.

There is even an fix for this but isn’t merged.

1 Like