Automation with multiple trigger

Hi all,
I am very new to HA and trying to optimize my working automation by using one automation for multiple triggers and targets and not copy it over and over again.

Here is the working example for one instance:

- id: '1649953538970'
  alias: Favoriten Radio
  description: ''
  trigger:
  - platform: state
    entity_id: 
      - input_select.auswahl_radio_buero
      - input_select.auswahl_radio_wz
      - input_select.auswahl_radio_ez
  condition: []
  action:
  - service: squeezebox.call_method
    target:
      entity_id: media_player.buero
    data:
      command: playlist
      parameters: >
        {% if is_state('input_select.auswahl_radio_buero', 'Bayern 3') %}
          ["index", "0"]
        {% elif is_state('input_select.auswahl_radio_buero', 'StarFM') %}
          ["index", "1"]
        {% elif is_state('input_select.auswahl_radio_buero', 'Radio Ton') %}
          ["index", "2"]
        {% elif is_state('input_select.auswahl_radio_buero', 'Antenne') %}
          ["index", "3"]
        {% endif %}
  mode: single

How can I achieve this if there are more input_select.selection_radio_xyz and media_player.xzy instances?

I think extracting the room string of the trigger might work. But unfortunately I don’t know how to do that.

I am very grateful for your tips!

This is more a concept than a working automation, because it is dependent on the name you’ve used and the index of your Input Select vs playlist index…

- id: '1649953538970'
  alias: Favoriten Radio
  description: ''
  trigger:
  - platform: state
    entity_id: 
      - input_select.auswahl_radio_buero
      - input_select.auswahl_radio_wz
      - input_select.auswahl_radio_ez
  condition: []
  action:
  - variables:
      current_room: "{{ trigger.to_state.entity_id.split('_')[-1] }}"
      current_list: > 
        {{ expand(trigger.to_state.entity_id) | map(attribute='attributes.options') | first }}
  - service: squeezebox.call_method
    target:
      entity_id: media_player.{{current_room}}
    data:
      command: playlist
      parameters: >
        ["index", {{ current_list.index(trigger.to_state.state) }}]
  mode: single

If your media player entity id’s aren’t so easily interchangeable with the input selects’ ids, you can use a map to set current_room. For example, if your three media players are media_player.buero, media_player.wohnzimmer, and media_player.esszimer (I’m totally guessing on the room names based on your abbreviations and my very-limited German :laughing: ) you would use the following:

current_room: >
  {% set map = {
   'input_select.auswahl_radio_buero': 'buero',
   'input_select.auswahl_radio_wz': 'wohnzimmer',
   'input_select.auswahl_radio_ez': 'esszimmer'
  } %}
  media_player.{{ map.get(trigger.to_state.entity_id) }}

Hi @Didgeridrew,

your German translation is totally fine! :smiley:

Thanks for your detailed answer, I’ll give it a try!
Do you have a hint for me how to develop and debug such complex (for me) automations?