Using trigger.id as part entity id

I have a 4x4 HDMI matrix which allows me to choose any one of four inputs (Sky, Firestick, DVD or CCTV) and map it to any of four TVs. It is controlled by this interface:
image
which has an automation listening for changes in the input select which then runs the appropriate script to control the HDMI matrix which works fine but it is quite long winded. At the moment I have 4 trigger ids and each one has four if-then statement for example

action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - lounge
        sequence:
          - if:
              - condition: state
                entity_id: input_select.lindy_lounge
                state: Sky
            then:
              - service: shell_command.lounge_to_sky
                data: {}
          - if:
              - condition: state
                entity_id: input_select.lindy_lounge
                state: Firestick
            then:
              - service: shell_command.lounge_to_firestick
                data: {}

I’d like to streamline this to something like:

action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - lounge
        sequence:
              - service: shell_command.{{trigger.id}}_to_{{states('input_select.lindy_{{trigger.id}}')|lower}}
                data: {} 

However I can’t get it to work. Any ideas, please? TIA

I believe you are just missing the quotes.

action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - lounge
        sequence:
              - service: "shell_command.{{trigger.id}}_to_{{states('input_select.lindy_'~trigger.id)|lower}}"
                data: {}

Edit, noticed the second trigger is was a string

"shell_command.{{trigger.id}}_to_{{states('input_select.lindy_' + trigger.id)|lower}"

The template contains a syntax error (already identified by other users). If your goal is to streamline the automation, post it so we can examine it for other potential streamlining opportunities.

Here is the whole automation.

alias: 33. Lindy Matrix
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.lindy_lounge_tv
    id: lounge
  - platform: state
    entity_id:
      - sensor.lindy_kitchen_tv
    id: kitchen
  - platform: state
    entity_id:
      - sensor.lindy_bed3_tv
    id: bed3
  - platform: state
    entity_id:
      - sensor.lindy_bed1_tv
    id: bed1
  - platform: time
    at: "03:00:00"
    id: three
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - lounge
        sequence:
          - if:
              - condition: state
                entity_id: input_select.lindy_lounge
                state: Sky
            then:
              - service: shell_command.lounge_to_sky
                data: {}
          - if:
              - condition: state
                entity_id: input_select.lindy_lounge
                state: Firestick
            then:
              - service: shell_command.lounge_to_firestick
                data: {}
          - if:
              - condition: state
                entity_id: input_select.lindy_lounge
                state: DVD
            then:
              - service: shell_command.lounge_to_dvd
                data: {}
          - if:
              - condition: state
                entity_id: input_select.lindy_lounge
                state: CCTV
            then:
              - service: shell_command.lounge_to_cctv
                data: {}
      - conditions:
          - condition: trigger
            id:
              - kitchen
        sequence:
          - if:
              - condition: state
                entity_id: input_select.lindy_kitchen
                state: Sky
            then:
              - service: shell_command.kitchen_to_sky
                data: {}
          - if:
              - condition: state
                entity_id: input_select.lindy_kitchen
                state: Firestick
            then:
              - service: shell_command.kitchen_to_firestick
                data: {}
          - if:
              - condition: state
                entity_id: input_select.lindy_kitchen
                state: CCTV
            then:
              - service: shell_command.kitchen_to_cctv
                data: {}
          - if:
              - condition: state
                entity_id: input_select.lindy_kitchen
                state: DVD
            then:
              - service: shell_command.kitchen_to_dvd
                data: {}
      - conditions:
          - condition: trigger
            id:
              - bed3
        sequence:
          - if:
              - condition: state
                entity_id: input_select.lindy_bed3
                state: Sky
            then:
              - service: shell_command.bed3_to_sky
                data: {}
          - if:
              - condition: state
                entity_id: input_select.lindy_bed3
                state: Firestick
            then:
              - service: shell_command.bed3_to_firestick
                data: {}
          - if:
              - condition: state
                entity_id: input_select.lindy_bed3
                state: DVD
            then:
              - service: shell_command.bed3_to_dvd
                data: {}
          - if:
              - condition: state
                entity_id: input_select.lindy_bed3
                state: CCTV
            then:
              - service: shell_command.bed3_to_cctv
                data: {}
      - conditions:
          - condition: trigger
            id:
              - bed1
        sequence:
          - if:
              - condition: state
                entity_id: input_select.lindy_bed1
                state: Sky
            then:
              - service: shell_command.bed1_to_sky
                data: {}
          - if:
              - condition: state
                entity_id: input_select.lindy_bed1
                state: Firestick
            then:
              - service: shell_command.bed1_to_firestick
                data: {}
          - if:
              - condition: state
                entity_id: input_select.lindy_bed1
                state: DVD
            then:
              - service: shell_command.bed1_to_dvd
                data: {}
          - if:
              - condition: state
                entity_id: input_select.lindy_bed1
                state: CCTV
            then:
              - service: shell_command.bed1_to_cctv
                data: {}
      - conditions:
          - condition: trigger
            id:
              - three
        sequence:
          - if:
              - condition: not
                conditions:
                  - condition: state
                    entity_id: input_select.lindy_lounge
                    state: Sky
            then:
              - service: shell_command.lounge_to_sky
                data: {}
              - service: input_select.select_option
                metadata: {}
                data:
                  option: Sky
                target:
                  entity_id: input_select.lindy_lounge
          - if:
              - condition: not
                conditions:
                  - condition: state
                    entity_id: input_select.lindy_kitchen
                    state: Sky
            then:
              - service: shell_command.kitchen_to_sky
                data: {}
              - service: input_select.select_option
                metadata: {}
                data:
                  option: Sky
                target:
                  entity_id: input_select.lindy_kitchen
          - if:
              - condition: not
                conditions:
                  - condition: state
                    entity_id: input_select.lindy_bed1
                    state: Sky
            then:
              - service: shell_command.bed1_to_sky
                data: {}
              - service: input_select.select_option
                metadata: {}
                data:
                  option: Sky
                target:
                  entity_id: input_select.lindy_bed1
          - if:
              - condition: not
                conditions:
                  - condition: state
                    entity_id: input_select.lindy_bed3
                    state: Sky
            then:
              - service: shell_command.bed3_to_sky
                data: {}
              - service: input_select.select_option
                metadata: {}
                data:
                  option: Sky
                target:
                  entity_id: input_select.lindy_bed3
          - delay:
              hours: 0
              minutes: 0
              seconds: 10
              milliseconds: 0
          - service: switch.turn_off
            target:
              entity_id:
                - switch.lindy_hdmi_matrix
            data: {}
mode: single

i would agree that doing the "shell_command.{{trigger.id}}_to_{{states('input_select.lindy_' + trigger.id)|lower}" is probably the single biggest way to collapse this code. once you do that, i think it’ll be like 5-10 lines of action code.

Your entity names adhere to a uniform naming scheme and that feature can be leveraged to reduce the automation’s overall size (from over 200 lines to under 50 lines).

The following example is untested so it may contain errors. Let me know if you encounter any and I’ll fix them.

alias: 33. Lindy Matrix
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.lindy_lounge_tv
      - sensor.lindy_kitchen_tv
      - sensor.lindy_bed3_tv
      - sensor.lindy_bed1_tv
  - platform: time
    at: "03:00:00"
condition: []
action:
  - if: "{{ trigger.platform == 'state' }}"
    then:
      - variables:
          room: "{{ trigger.to_state.object_id.split('_')[1] }}"
          mode: "{{ states('input_select.lindy_' ~ room) | lower }}"
      - service: 'shell_command.{{ room }}_to_{{ mode }}'
    else:
      - variables:
          selects:
            - input_select.lindy_lounge
            - input_select.lindy_kitchen
            - input_select.lindy_bed1
            - input_select.lindy_bed3
      - repeat:
          for_each: >
            {{ expand(selects) | selectattr('state', 'ne', 'Sky')
              | map(attribute='entity_id') | list }}
          sequence:
            - variables:
                room: "{{ repeat.item.split('_')[-1] }}" 
            - service: 'shell_command.{{ room }}_to_sky'
            - service: input_select.select_option
              data:
                option: Sky
              target:
                entity_id: 'input_select.lindy_{{ room }}'
      - delay:
          seconds: 10
      - service: switch.turn_off
        target:
          entity_id: switch.lindy_hdmi_matrix
mode: single
1 Like

That’s brilliant, thank you. Also really neat demonstration of some functions that I will be able to re-use. :+1:t3:

1 Like