Change entity id dynamically in action section, based on which trigger caused the automation to start

Hi,

The goal is simple. I have this automation that works for one speaker.

  • If there is music playing, I want the led of the speaker to turn on.
  • If there is no music playing (paused), I want the led of the speaker to turn off.

But I want my automation to by dynamic, so that I can add a speaker as trigger, and I don’t have to change something in the action section. I basically need the entity_id to change dynamically, based on the trigger, that started the automation.

The entity id friendly name looks something like this switch.sonos_speaker_janis_status_light

alias: test 2 sonos
description: ""
trigger:
  - platform: device
    device_id: e76d83fa8264f912474a90924f8b2eeb
    domain: media_player
    entity_id: media_player.sonos_speaker_janis
    type: playing
    id: media-playing
  - platform: device
    device_id: e76d83fa8264f912474a90924f8b2eeb
    domain: media_player
    entity_id: media_player.sonos_speaker_janis
    type: paused
    id: media-paused
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - media-playing
        sequence:
          - type: turn_on
            device_id: "works with anything, but cannot be empty, hmm"
            entity_id: 4e22a910adc55b0bd96df837bccd4aa3
            domain: switch
      - conditions:
          - condition: trigger
            id:
              - media-paused
        sequence:
          - type: turn_off
            device_id: "works with anything, but cannot be empty, hmm"
            entity_id: 4e22a910adc55b0bd96df837bccd4aa3
            domain: switch
mode: single

Any ideas?

Thanks for your help
Janis

I think what you’re after is trigger variables: Automation Trigger - Home Assistant

Specifically, {{ trigger.entity_id }}

Yes, I was also looking at this. But I think I found the problem.

I’m not able to use variables ( {{ }} ) for entity_id in a sequence.

When I try to same the yaml, I get the following error message

Message malformed: Entity {{'switch.sonos_speaker_janis_status_light'}} is neither a valid entity ID nor a valid UUID for dictionary value @ data['entity_id']
sequence:
          - type: turn_off
            device_id: "works with anything, but cannot be empty, hmm"
            #entity_id: 4e22a910adc55b0bd96df837bccd4aa3                # works
            #entity_id: switch.sonos_speaker_janis_status_light         # works
            entity_id: "{{'switch.sonos_speaker_janis_status_light'}}"  # does not work
            domain: switch

If you call the service “Switch: Turn Off” instead of using a Device action, it should work with this formatting:

choose:
  - conditions: []
    sequence:
      - service: switch.turn_off
        data: {}
        target:
          entity_id: "{{trigger.entity_id}}"
1 Like
alias: example
trigger:
  - platform: state
    entity_id:
      - media_player.sonos_speaker_janis
    to:
      - playing
      - paused
condition: []
action:
  - service: "switch.turn_{{ 'on' if trigger.to_state.state == 'playing' else 'off' }}"
    target:
      entity_id: "switch.{{ trigger.to_state.object_id }}_status_light"
mode: queued

When you get more Sonos speakers, you simply need to add them to the State Trigger’s entity_id option.

trigger:
  - platform: state
    entity_id:
      - media_player.sonos_speaker_janis
      - media_player.sonos_speaker_kitchen
      - media_player.sonos_speaker_bedroom
      - media_player.etc
    to:
      - playing
      - paused
1 Like

Awesome, thanks for the tipp! That worked!

alias: test 2 sonos
description: ""
trigger:
  - platform: device
    device_id: e76d83fa8264f912474a90924f8b2eeb
    domain: media_player
    entity_id: media_player.sonos_speaker_janis
    type: playing
    id: media-playing
  - platform: device
    device_id: e76d83fa8264f912474a90924f8b2eeb
    domain: media_player
    entity_id: media_player.sonos_speaker_janis
    type: paused
    id: media-paused
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - media-playing
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.{{trigger.entity_id.split('.')[1]}}_status_light
      - conditions:
          - condition: trigger
            id:
              - media-paused
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.{{trigger.entity_id.split('.')[1]}}_status_light
mode: single

Wow! That is even cleaner, thank you so much. This was the solution I was looking for!

1 Like