[SOLVED] Can't get until loop to stop - condition template

Hello there,

I wrote my first automation+script recently. I want to turn some lights on at sunset and make them switch to random colors every seven seconds until I turn them off. The idea behind it is to take advantage of some ambience lights I have.

To achieve this I have this automation :

- id: 'XXX'
  alias: Turn on the lights when the sun is set
  description: ''
  trigger:
  - platform: sun
    event: sunset
  condition: []
  action:
  - service: script.turn_on
    entity_id: script.light_colorful
    data:
      variables:
        light_id: light.light1
  - service: script.turn_on
    entity_id: script.light_colorful
    data:
      variables:
        light_id: light.light2
  mode: single

The script called is this :

light_colorful:
  alias: light_colorful
  description: Set a light to colorful.
  fields:
    light_id:
      description: The id of the light to set to colorful.
      example: light.light1
  variables:
    light: '{{ entity_id }}'
  sequence:
  - repeat:
      until:
      - condition: template
        value_template: "{{ is_state(light, 'off') }}"
      sequence:
      - service: light.turn_on
        data_template:
          entity_id: '{{ light_id }}'
          rgb_color:
          - '{{ (range(0, 255)|random) }}'
          - '{{ (range(0, 255)|random) }}'
          - '{{ (range(0, 255)|random) }}'
          brightness_pct: 100
          transition: 1
      - delay: '7'
  mode: parallel
  max: 10

The problem I have is the “until” condition template that doesn’t trigger. Whenever I turn off one of these lights using the dashboard, it gets turned on again a few seconds after.
I observed the {{ is_state(light, 'off') }} in the developer tools and I can see it return “true” before the light turns on again.

Any help to get that exit condition working would be greatly appreciated :slight_smile:

Why is this light_id instead of light or entity_id?

        data_template:
          entity_id: '{{ light_id }}'

My god, I feel so dumb. I rewrote this script so many times over that I didn’t realise it was in a dirty state.

I removed all the unnecessary stuff (what I know of at least) and it works ! Thank you for making me realise my mistake.

light_colorful:
  alias: light_colorful
  description: Set a light to colorful.
  fields:
    light_id:
      description: The id of the light to set to colorful.
      example: light.bf1106d83003486a8bsqvv
  sequence:
  - repeat:
      until:
      - condition: template
        value_template: '{{ is_state(light_id, ''off'') }}'
      sequence:
      - service: light.turn_on
        data_template:
          entity_id: '{{ light_id }}'
          rgb_color:
          - '{{ (range(0, 255)|random) }}'
          - '{{ (range(0, 255)|random) }}'
          - '{{ (range(0, 255)|random) }}'
          brightness_pct: 100
          transition: 1
      - delay: '7'
  mode: parallel
  max: 10
1 Like

Glad to hear it helped to resolve the issue.

BTW, you can change data_template to data.

data_template was deprecated in favor of data several versions ago. data now works the same way as data_template.

1 Like

Thanks ! I tried it and it works great. That will allow me to work with the UI.