How to pass a variable from a trigger to an action?

I’m trying to write an automation where people coming home will trigger my google home to say who it is that has arrived. I know this could be done easily using conditionals or by writing a separate automation for each person but this seems a little clunky.

I was hoping that I’d be able to have the automation be triggered by one of three people and to be able to pass a variable of the person’s name who has arrived and then pass an interpolated string to google. Check out my code below to see what I’m trying to do in more detail (with some pesudocode thrown in there to show where I want the variable to be set and where I want it to be used).

Thanks in advance!

- alias: "Person Arriving Home"
  trigger:
    - platform: state
      entity_id: person.ed
      to: "home"
      SET_VARIABLE: "ed"
    - platform: state
      entity_id: person.edd
      to: "home"
      SET_VARIABLE: "edd"
    - platform: state
      entity_id: person.eddy
      to: "home"
      SET_VARIABLE: "eddy"
  action:
    service: tts.google_say
    entity_id: media_player.kitchen_speaker
    data:
      message: "{{ PERSON }} has arrived"

In the action, you can use {{ trigger.to_state.entity_id }}. To_state and from_state are both state objects that are set by the trigger.

Search for ‘trigger state object’

2 Likes

Ah nice one, thanks! That opens up a world of possibilities for me. Much appreciated!