How to find out entity that triggered an automation

Hi,
I have a piece of automation set up that is triggered by multiple switches:

- alias: "IR remote commands"
  trigger:
    platform: state
    entity_id: switch.mqtt_switch
    to: "on"
  action:
    service: mqtt.publish
    data:
      topic: "viktak/spiti/livingroomir/cmnd"
      payload: "something"

I understand instead of payload I can use payload_template, but the documentation is not very verbose on that…
So my question is how do I go about creating a template that sends the entity_id of the switch that triggered this action.

Thanks in advance for any pointers!

v

This gives the friendly name (if set) or entity id if there is no friendly name.

  action:
    service: mqtt.publish
    data: 
      topic: "viktak/spiti/livingroomir/cmnd"
      payload_template: "{{trigger.to_state.attributes.name}}"

This template always gives the entity id:

"{{ trigger.entity_id }}"

Cool, I’ll try this in the afternoon, will let you know.

Is there some documentation on this? Like what other properties/attributes are available so that I don’t have to do it by trial and error?

There’s a bit here:

Still not working…

This works as expected:

  action:
    service: mqtt.publish
    data:
      topic: "viktak/spiti/livingroomir/cmnd"
      payload: "{{ trigger.entity_id }}"

When I swap payload to payload_template, it does not work:

  action:
    service: mqtt.publish
    data:
      topic: "viktak/spiti/livingroomir/cmnd"
      payload_template: "{{ trigger.entity_id }}"

Looking at the logs, I can see that the automation gets triggered:


4:08 PM
IR remote commands has been triggered
4:08 PM
MQTT Switch turned on

However, there is no activity on mqtt.

So use that?

Sorry, I may not have been very clear: when I use only payload, then it works as expected, ie. it sends the string in the double quotes, not the actual entity id.

Try this then

action:
  service: mqtt.publish
  data_template:
    topic: "viktak/spiti/livingroomir/cmnd"
    payload: "{{ trigger.entity_id }}"
1 Like

Yes, this seems to be working.

Thanks for sticking with me!!!

1 Like