How do I write a script to turn off all lights and switches

I have a method that will tell me if any of the lights are on in my shed and I’d like to be able to turn them all on or off. About half are lights and the other half are switches. Is there an introspective way that I can all the appropriate off service for entity in the array? This is a step in learning to better templatize more of my rooms.

The code below doesn’t work, but it what I have.

toggle_shed:
  alias: "Toggle Shed Lights"
  variables:
    action_type: >
      {% if is_state('light.shed_lights', 'on') %}
        "{{ 'light.turn_off' }}"
      {% else %}
        "{{ 'light.turn_on' }}"
      {% endif %}
  sequence:
    repeat:
      for_each:
        - "shed_north_light"
        - "shed_south_light"
        - "shed_east_light"
        - "shed_west_light"
        - "shed_outside_light"
      sequence:
        - service: "{{ action_type }}"
          target:
            entity_id: "light.{{ repeat.item }}"

just make a light group and call light.toggle.

- service: light.toggle
  target:
    entity_id: light.shed_lights

If you’re dead set on a template, you don’t need a foreach. Just provide an entity_id list.
Secondly, make sure you understand when to use quotes and when not to. Overquotting will cause issues.

toggle_shed:
  alias: "Toggle Shed Lights"
  variables:
    action_type: >
      {% if is_state('light.shed_lights', 'on') %}
        light.turn_off
      {% else %}
        light.turn_on
      {% endif %}
    object_ids: 
      - shed_north_light
      - shed_south_light
      - shed_east_light
      - shed_west_light
      - shed_outside_light
    entity_ids: >
      {% set ns = namespace(items=[]) %}
      {% for o in object_ids %}
        {% set ns.items = ns.items + [ 'light.' ~ o ] %}
      {% endfor %}
      {{ ns.items }}
  sequence:
    - service: "{{ action_type }}"
      target:
        entity_id: "{{ entity_ids }}"

Also, if light.shed_lights has all your lights, you can do this (again if you’re dead set on using templates)

toggle_shed:
  alias: "Toggle Shed Lights"
  variables:
    action_type: >
      {% if is_state('light.shed_lights', 'on') %}
        light.turn_off
      {% else %}
        light.turn_on
      {% endif %}
    entity_ids: >
      {{ expand('light.shed_lights') | map(attribute='entity_id') | list }}
  sequence:
    - service: "{{ action_type }}"
      target:
        entity_id: "{{ entity_ids }}"

event further simplified…

toggle_shed:
  alias: "Toggle Shed Lights"
  sequence:
    - service: >
        light.turn_o{{ 'ff'  if is_state('light.shed_lights', 'on') else 'n' }}
      target:
        entity_id: >
          {{ expand('light.shed_lights') | map(attribute='entity_id') | list }}
3 Likes

I thought I was alone in making a simple project complex… Just make a shed group.

Automation:

service: homeassistant.turn_off
data: {}
target:
  entity_id:
    - group.shed
2 Likes

Thanks for taking the time. This works great!!!

Thanks for answering. This is useful. I’ll look at using groups.