Repeat until automation was working until a few days ago

I have an automation that uses a repeat until loop to dim a light until the state changes to off. It has been working well for many months. A few nights ago, the automation started failing. When I watch in the light’s app, I see about 8-10 decreases and then the automation stops.
When I watch on the automation screen, I see “action run successfully”.
Unfortunately the light is still on and the state of the light is still “on” so I can’t figure out why the loop has suddenly started exiting before the state of the light changes.

I tried a slightly different loop that fails similarly. Repeat until brightness is less than 1.

Previously successful loop that has started failing

repeat:
  until:
    - condition: state
      entity_id: light.master_bedroom_bed_lightstrip
      state: "off"
  sequence:
    - service: light.turn_on
      data:
        brightness_step_pct: -1
      target:
        entity_id: light.master_bedroom_bed_lightstrip

Another loop that failed (to solve the problem)

repeat:
  sequence:
    - service: light.turn_on
      metadata: {}
      data:
        brightness_step_pct: -1
      target:
        entity_id: light.master_bedroom_bed_lightstrip
  until:
    - condition: numeric_state
      entity_id: sensor.master_bedroom_bed_lightstrip_brightness
      below: 1

doing this i a tight loop i typically meta stable. this is because the call to change the brightness on a light is typically not synchronous (ie, it’ll return before the ligtht actually changes brightness)… and it might or might not queue up your requests. if it does queue up the request, what will it do when you’ve overloaded the queue?

it’s not uncommon for this tight loop approach to work for a while, then when timings change (due to an update, or other things happening) it may suddenly break.

the best way to do this is if your light natively supports transitions…

service: light.turn_on
target:
  entity_id: light.your light
data:
  transition: 25
  brightness: 0

or alternatively use @handcoding 's nifty light fader:

which does the fading for you with all sorts of options and works with lights that do and don’t natively transition.

1 Like

Thanks so much for the shout-out, @armedad!

And @RN-Say, if you were to give my light-fader script a go, and if you were to have any questions about it, feel free to ask and I’ll be happy to chime in!