Select_next script for light effects

Somehow I assumed there would be a service like light.effect_select_next as there is for input_select.select_next but since there wasn’t, I created the following script which I’m sharing in case anyone else has a use for it.

light_effect_select_next:
  sequence:
  - service: light.turn_on
    data:
      effect: >
        {% set effects = state_attr(light, 'effect_list') %} 
        {% set state = effects[0] if state_attr(light, 'effect') is none else state_attr(light, 'effect') %}
        {{ effects[0] if (effects.index(state)+1 == effects|length) else effects[effects.index(state)+1] }}
    target:
      entity_id: '{{ light }}'
  mode: parallel
  alias: light effect select next

The target light is passed as the variable light when calling the script.

- service: script.light_effect_select_next
  data:
    light: light.office_light

In my case, most of my lights are template lights so I use effects as presets which I find cleaner than scenes. This script lets me cycle through presets.

2 Likes

FYI, if you make a template select, number.select_next comes with it.

template:
- select:
  - name: My light Effect
    unique_id: my_light_effect
    state: "{{ state_attr('light.xyz', 'effect') }}"
    options: "{{ state_attr('light.xyz', 'options') }}"
    select_option:
    - service: light.turn_on
      target:
        entity_id: light.xyz
      data:
        effect: "{{ option }}"
3 Likes

That looks much cleaner and I could have saved a lot of time spent reading jinja documentation, but at least I learned a bit more jinja.

Thanks as alway, Petro
(BTW, I think you meant template select instead of template number)

template:
- select:
  - name: Office Light Effects
    unique_id: office_light_effect
    state: "{{ state_attr('light.office_light', 'effect') }}"
    options: "{{ state_attr('light.office_light', 'effect_list') }}"
    select_option:
    - service: light.turn_on
      target:
        entity_id: light.office_light
      data:
        effect: "{{ option }}"
1 Like

Yes, I meant select :sweat_smile:

@petro I created the template select entities but don’t see select.select_next service. There is a feature request for it though.

interesting, sorry for steering you down the wrong path then. I assumed they shared the same namespace.

1 Like

While the template in theory works, there’s an issue when the light is off which will result in a None value. An error will occur as there is no option for None