Question regarding platform state trigger

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?

Thanks!

I use this:

{{ trigger.to_state.attributes.friendly_name }}

e.g.

  - service: notify.all_ios_devices
    data_template:
      message: "The {{ trigger.to_state.attributes.friendly_name }} Smoke Alarm has TRIGGERED!!!"
      data:
        push:
          badge: 0
          sound: "SmokeDetected.wav"
          category: "silencealarm"
        action_data:
          entity_id: group.alarm_outputs
1 Like

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

ok i got it working for input_text

- service: input_text.set_value
  data_template:
    entity_id: input_text.alarm_trigger_one
    value: '{{ trigger.to_state.attributes.friendly_name }}'

but not for set variable component

- service: variable.set_variable
  data_template:
    variable: alarm_triggerone
    value: '{{ trigger.to_state.attributes.friendly_name }}'
1 Like

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:

- service: variable.set_variable
  data:
    variable: alarm_triggerone
    value_template: '{{ trigger.to_state.attributes.friendly_name }}'

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'

You could put them all in one trigger like this:

  trigger:
    - platform: state
      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
        - binary_sensor.motion_sensor_158d0001e57659
        - binary_sensor.motion_sensor_158d0001e52dee
        - binary_sensor.motion_sensor_158d0001dc7526
      to: 'on'

But this is more of a style suggestion than anything.

2 Likes

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:

1 Like