I’m doing an automation, and I want to know what entity_id caused the automation to trigger:
Here’s an example:
- alias: "Door Meta Sensor"
trigger:
- platform: state
entity_id: binary_sensor.door_window_sensor_158d0001d63b07, binary_sensor.door_window_sensor_158d0001ef77d8, binary_sensor.door_window_sensor_158d0001fa792d
action:
- service: variable.set_variable
data:
variable: door_meta
value_template: '{{ the entity_id from above that just triggered the automation }}'
I could just use an if, elif, elif, else statement for all the sensors, but I’m wondering if there is a cleaner way?
Hmmm I’m still having some problems, see the below example automation.
- alias: "01 First trigger"
trigger:
- platform: state # Door and window
entity_id: binary_sensor.door_window_sensor_158d0001d63b07, binary_sensor.door_window_sensor_158d0001ef77d8, binary_sensor.door_window_sensor_158d0001fa792d, binary_sensor.door_window_sensor_158d0001a94c35, binary_sensor.door_window_sensor_158d0001f9e2e2, binary_sensor.door_window_sensor_158d0001f38b1f
from: 'off'
to: 'on'
- platform: state # Motion
entity_id: binary_sensor.motion_sensor_158d0001e57659, binary_sensor.motion_sensor_158d0001e52dee, binary_sensor.motion_sensor_158d0001dc7526
from: 'off'
to: 'on'
action:
- service: input_text.set_value
data_template:
entity_id: input_text.alarm_trigger_one
value_template: '{{ trigger.to_state.attributes.friendly_name }}'
- service: variable.set_variable
data:
variable: alarm_triggerone
value_template: '{{ trigger.to_state.attributes.friendly_name }}'
- service: notify.html5
data_template:
message: 'Alarm has been tripped by {{ trigger.to_state.attributes.friendly_name }}'
data:
data:
tag: 'notification-about-sensor'
The value_template for the notification works great and shows the correct sensor name … however I’m unable to use value_template to store the triggering sensor name into either a ‘variable’ or ‘input_text’.
I haven’t used the variable component, but I was just browsing what I believe to be the code for that component. If it is, and I read it correctly, then I think you need to do this:
Also, in your original post, you said you wanted the entity_id of the the entity that caused the trigger. If so you should use trigger.entity_id instead of trigger.to_state.attributes.friendly_name.
And lastly, in your more recent post you showed the triggers as:
trigger:
- platform: state # Door and window
entity_id: binary_sensor.door_window_sensor_158d0001d63b07, binary_sensor.door_window_sensor_158d0001ef77d8, binary_sensor.door_window_sensor_158d0001fa792d, binary_sensor.door_window_sensor_158d0001a94c35, binary_sensor.door_window_sensor_158d0001f9e2e2, binary_sensor.door_window_sensor_158d0001f38b1f
from: 'off'
to: 'on'
- platform: state # Motion
entity_id: binary_sensor.motion_sensor_158d0001e57659, binary_sensor.motion_sensor_158d0001e52dee, binary_sensor.motion_sensor_158d0001dc7526
from: 'off'
to: 'on'
I agree with @pnbruckner. {{ trigger.entity_id }} is what you want for the entity id out of a trigger. attribute.friendly_name will give you the name that is shown in the UI. All of those attributes for triggers are in the automation templating documentation. Hopefully this will help you so you aren’t taking a stab in the dark when trying to find crap inside triggers: