Toggle all lights automation

This might have been answered before, but I’ve not been able to find anything.

I’ve got most lights (most are Philips Hue) in my house connected to Home Assistant and I’m looking for a way to turn off all lights by using a light switch next to my entrance door. I’ve already mounted a Wall Switch Module so in Hue I can add a rule turning off all lights, but as I have multiple bridges I can only make use of that solution for turning off some lights. Also it does not work towards other lights I have which are connected to Home Assistant. Also the Hue will only allow you to turn off everything and afterwards turn on everything which is not optimal. I would prefer to be able to easily turn off all lights and when pushing the button next time it would go back to the same state. So if I had only one room with lights on when leaving I would turn off that light and when pushing the light switch the next time it would go back to the same state - meaning just turning that room back on.

Anyone solved this? I could group all lights fairly easily into on group, but I assume I would need a way to store the state of each and every light and that I have not found a way to do…

1 Like
          - service: light.turn_off
            data_template:
              entity_id: |
                {% set lights = states.light
                  | selectattr('state', 'eq', 'on')
                  | rejectattr('entity_id', 'search', 'light_outside_front')
                  | map(attribute='entity_id') | list %}
                {{ expand(lights)| map(attribute='entity_id')|join(',') }}
            alias: "Light: Turn all off except outdoor front"

but yeah, in order to recall you’de have to store the list somewhere. I guess you could write it into a scene, and then call the scene on next push…
Copying it into a helper would also be possible :wink:

1 Like

I’m using the following script:


# VERWENDUNG / = usage:
#
#   - service: script.auto_taster_lampen
#     data:
#       ignore:
#         - light.couch_licht


script:

  auto_taster_lampen:
    alias: Auto-Taster-Lampen
    description: |-
      Momentary Switch für Lampen
      ——> packages/skripte/auto_taster_lampen.yaml
    icon: mdi:lightbulb-auto-outline
    mode: single
    max_exceeded: silent

    variables:
      ignore: []
      lights: |-
        {{- states.light
        |reject('search', 'is_hue_group|deconz|entity_id')
        |map(attribute='entity_id')
        |reject('in', ignore)
        |select('is_state', 'on')
        |list -}}

    sequence:

    - if:
      - "{{ lights|count > 0 }}"
      then:
      - service: scene.create
        data:
          scene_id: snapshot_lights
          snapshot_entities: "{{ lights }}"

      - service: homeassistant.turn_off
        target:
          entity_id: "{{ lights }}"
      else:
      - service: scene.turn_on
        target:
          entity_id: scene.snapshot_lights

Please note that a snapshot scene won’t survive a HA restart, so if you’re away from home and your HA instance accidentally restarts, the snapshot is gone (but this is a thing I can live with because my main focus is to turn off the lights when I’m leaving home :slight_smile: )

2 Likes