Turning each light in particular order

I have 7 spotlights. I want to turn on each spotlight pressing on my esphome button.
Case scenario:

{% set lights = ['light.livingroom_spotlight_1_1','light.livingroom_spotlight_1_2','light.livingroom_spotlight_1_3','light.livingroom_spotlight_1_4','light.livingroom_spotlight_1_5','light.livingroom_spotlight_1_6','light.livingroom_spotlight_1_7'] %}
  • If all lights are turned off in lights group, then turn first light ‘light.livingroom_spotlight_1_1’
  • If only first light is turned on, turn on next light (‘light.livingroom_spotlight_1_2’)
  • If only first and second lights are turned on, then turn on third light (‘light.livingroom_spotlight_1_3’)
    and so on.

If lights are turned on in any other non-linear form (first light turned on, then fourth, then seventh) - turn off all lights, and turn on just first light.

Thanks

This is still new and I haven’t used this myself, but an input_select has a select_next service. Perhaps you can leverage that. It will give you the ability to cycle, but you’ll need more.

or maybe just 30 statements of if-else statements :slight_smile:

what about this?
I always see lights go on in line of how I add them in the service:

            - service: light.turn_on
              data:
                entity_id:
                  - light.livingroom_1
                  - light.livingroom_2
                  - light.livingroom_3
                  - light.livingroom_4
                  - light.livingroom_5
                  - light.livingroom_6

I assumed the OP wants something smarter and maintainable or they wouldn’t have asked.

1 Like

That’s not what the OP means, according to my understanding. It should go: Light 1 is on, then press the button, then 1 and 2 is on, press again, 1 and 2 and 3 is on, etc. and after all are on, the next button press will turn everything off, followed by restarting the process.

then it will be a long if this then that haha… :slight_smile:

So, this automation should do the trick and give you the tools to debug it. You’ll have to add your own trigger and adjust the list of lights in the variables.

automation:
- alias: Turn lights on in order
  trigger:
  ...
  variables:
    lights:
    - light.light1
    - light.light2
    - light.light3
    on_lights: >
      {{ expand(lights) | selectattr('state','eq','on') | map(attribute='entity_id') | list }}
    off_lights: >
      {{ expand(lights) | selectattr('state','eq','off') | map(attribute='entity_id') | list }}
    on_count: >
      {{ on_lights | length }}
    next_light: >
      {%- if lights == off_lights %}
        {{ lights[0] }}
      {%- elif lights[:on_count] == on_lights and on_lights != lights %}
        {{ lights[on_count] }}
      {%- else %}
        off
      {%- endif %}
  action:
  - choose:
    - conditions:
      - condition: template
        value_template: "{{ next_light != 'off' }}"
      sequence:
      - service: light.turn_on
        target:
          entity_id: "{{ next_light }}"
    default:
    - service: light.turn_off
      target:
        entity_id: "{{ on_lights }}"
    - service: light.turn_on
      target:
        entity_id: "{{ lights[0] }}"
4 Likes

That’s neat.