Toggling many automations in one call

There is a script which toggles several automations:

    sequence:
      - service: automation.turn_on
        entity_id:
          - automation.a_1
          - automation.a_2
          - automation.a_3
         ...
          - automation.a_xxx

How to define a list of automations using a template?
I made this template:

            {% set AUTOS =  states.automation |
                              selectattr('entity_id', 'search', 'a_') |
                              map(attribute='entity_id') | list -%}
            {{AUTOS}}

It returns this value:

['automation.a_1', 'automation.a_2', 'automation.a_3', 'automation.a_4']

Adding this value to the script does not cause any errors (at least before starting a run):

    sequence:
      - service: automation.turn_on
        entity_id: ['automation.a_1', 'automation.a_2', 'automation.a_3', 'automation.a_4']

But this code causes an error after reloading scripts:

    sequence:
      - service: automation.turn_on
        entity_id: >-
            {% set AUTOS =  states.automation |
                              selectattr('entity_id', 'search', 'a_') |
                              map(attribute='entity_id') | list -%}
            {{AUTOS}}

Error is: invalid key: "OrderedDict([('AUTOS', None)])" in "/config/conf/sys/sys_script_toggle_autos.yaml", line 32, column 0

Is it possible to use templates to specify a list of automations?

Create a group, include all automations in the group and use the group name to switch the group as a whole on and off.

1 Like

Thats what I did. Its faster than anything I ever had in webcore. Sometimes I press the button just to watch them all turn off haha.

1 Like

you need to use β€œdata:” to use a template there.

Like this should work:

    sequence:
      - service: automation.turn_on
        data:
          entity_id: >-
            {% set AUTOS =  states.automation |
                              selectattr('entity_id', 'search', 'a_') |
                              map(attribute='entity_id') | list -%}
            {{AUTOS}}

Finity is right. Use data: (or target:) between service call and entity id.

Thank everyone for support!

That was my final solution:

  1. Create an automation group:
group:

  do_something:
    entities:
      - automation.a_1
      - automation.a_2
      - automation.a_3
  1. Create a switch for toggling the group:
switch:
  - platform: template
    switches:
      do_something:
        value_template: >-
          {% set input_GROUP_AUTOS = 'group.do_something' -%}
          {%- set input_VALUE_TO_SET = 'on' -%}
          {%- set autos = expand(input_GROUP_AUTOS) -%}
          {%- set ns = namespace(AUTOS_STATE='0') -%}
          {%- for auto in autos -%}
            {%- if not is_state(auto.entity_id,input_VALUE_TO_SET) -%}
              {%- set ns.AUTOS_STATE = ns.AUTOS_STATE|int + 1 -%}
            {%- endif -%}
          {%- endfor -%}
          {{ns.AUTOS_STATE|int == 0}}

        turn_on:
          - service: script.sys_toggle_autos
            data:
              input_GROUP_AUTOS: group.do_something
              input_VALUE_TO_SET: 'on'

        turn_off:
          - service: script.sys_toggle_autos
            data:
              input_GROUP_AUTOS: group.do_something
              input_VALUE_TO_SET: 'off'

Why not using a states('group.do_something') as a value_template?
Because I wanted this logic:

  • if ALL automations are ON β†’ then the switch is ON;
  • if any automation is OFF β†’ then the switch is OFF.

Why not just toggling a group.do_something?

          - service: automation.turn_off
            entity_id: group.do_something

Because I wanted an additional check - all automations should be toggled by the moment when the actions are completed.
If anyone find this not necessary - please post your opinion.

  1. The script:
script:

  sys_toggle_automations:
    description: "Toggle group of automations"

    fields:
      input_GROUP_AUTOS:
        example: group.do_something
      input_VALUE_TO_SET:
        example: 'on'

    sequence:
      ### toggle a group
      - service: automation.turn_{{input_VALUE_TO_SET}}
        data:
          entity_id: >-
            {{input_GROUP_AUTOS}}

      ## wait for completion
      - wait_template: >-
          {% set autos = expand(input_GROUP_AUTOS) -%}
          {%- set ns = namespace(AUTOS_STATE='0') -%}
          {%- for auto in autos -%}
            {%- if not is_state(auto.entity_id,input_VALUE_TO_SET) -%}
              {%- set ns.AUTOS_STATE = ns.AUTOS_STATE|int + 1 -%}
            {%- endif -%}
          {%- endfor -%}
          {{ns.AUTOS_STATE|int == 0}}

      ### you may add here sending a notification

    mode: queued
    max: 5