Automation variables - use array to shorten automation?

Hi all,

I’m trying to move some code from AppDaemon to HA Automations and was wondering whether the following was possible:

description: "Control HVAC"
mode: single
variables:
  room_heating_entities:
    - switch.praktijk_verwarming
    - switch.bureau_verwarming
    - switch.verdieping_1_verwarming
    - switch.waskot_verwarming
    - switch.badkamer_verwarming
    - switch.slaapkamer_1_verwarming
    - switch.slaapkamer_2_verwarming
    - switch.slaapkamer_3_verwarming
    - switch.slaapkamer_4_verwarming
trigger:
  - platform: state
    entity_id: "{{ room_heating_entities }}"
      
condition: []
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: state
                entity_id: input_select.hvac_mode
                state: "Heat"
              - condition: state
                entity_id: "{{ room_heating_entities }}"
                match: any
                state: "on"
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.hvac_heat_pump_heat
      - conditions:
          - condition: and
            conditions:
              - condition: state
                entity_id: input_select.hvac_mode
                state: "Cool"
              - condition: state
                entity_id: "{{ room_heating_entities }}"
                match: any
                state: "on"
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.hvac_heat_pump_cool
      - conditions:
          - condition: state
            entity_id: "{{ room_heating_entities }}"
            state: "off"
        sequence:
          - service: switch.turn_off
            target:
              entity_id: 
                - switch.hvac_heat_pump_heat
                - switch.hvac_heat_pump_cool

I receive the following error:

Message malformed: Entity {{ room_heating_entities }} is neither a valid entity ID nor a valid UUID for dictionary value @ data['action'][0]['choose'][0]['conditions'][0]['conditions'][1]['entity_id']

I’m on the latest and greatest HA 2023.6.1.

State triggers and conditions do not accept templates. You can use template conditions instead of your state conditions, but a state trigger with all the entities listed is the way to trigger your automation on any change of any of the entities.

description: "Control HVAC"
mode: single
trigger:
  - platform: state
    entity_id: 
      - switch.praktijk_verwarming
      - switch.bureau_verwarming
      - switch.verdieping_1_verwarming
      - switch.waskot_verwarming
      - switch.badkamer_verwarming
      - switch.slaapkamer_1_verwarming
      - switch.slaapkamer_2_verwarming
      - switch.slaapkamer_3_verwarming
      - switch.slaapkamer_4_verwarming      
condition: []
action:
- variables:
    room_heating_entities:
      - switch.praktijk_verwarming
      - switch.bureau_verwarming
      - switch.verdieping_1_verwarming
      - switch.waskot_verwarming
      - switch.badkamer_verwarming
      - switch.slaapkamer_1_verwarming
      - switch.slaapkamer_2_verwarming
      - switch.slaapkamer_3_verwarming
      - switch.slaapkamer_4_verwarming
    any_on: "{{ room_heating_entities | select('is_state', 'on') | list | count > 0 }}"
  - choose:
      - conditions:
          - condition: state
            entity_id: input_select.hvac_mode
            state: 
              - "Heat"
              - "Cool"
          - condition: template
            value_template: "{{ any_on }}"
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.hvac_heat_pump_{{ states('input_select.hvac_mode')|lower}}
      - conditions:
          - condition: template
            value_template: "{{ not any_on }}"
        sequence:
          - service: switch.turn_off
            target:
              entity_id: 
                - switch.hvac_heat_pump_heat
                - switch.hvac_heat_pump_cool

Another option to get similar functionality would be to put all your switches in a group.

1 Like

Thanks so much - I like the idea of the group.