"Until" instruction returns false while log seems to say values are correct

Hi,
I’m facing issues with a script. I tried to reduce it so that it will be easier to understand what’s going on so consider undeclared variables as declared in the script scope.

What I’m trying to do is create a script that makes a light transition between a set of colors. I want to detect when the user tries to set a different color on the light so that the script stops when that happens. All logs are fine, meaning that the new target color recovered from the light displays the same, but it doesn’t prevent the loop from exiting when comparing both values in the “until” section.
I’m not sure what’s happening. Is the until instruction code in the same scope as the sequence ? Do I need to do something special to compare both values in the “until” call ?

sequence:
  - repeat:
      sequence:
        - variables:
            # Next color index to change to.
            i_next: >
              {% set idx = 0 %}
              {% if decrement %}
                {% set idx = states(color_tracker) | int - 1 %}
                {% if idx < 0 %}
                  {% set idx = color_count %}
                {% endif %}
              {% else %}
                {% set idx = states(color_tracker) | int + 1 %}
                {% if idx > color_count %}
                  {% set idx = 0 %}
                {% endif %}
              {% endif %}
              {{ idx }}
            color_rgb_next: '{{ color_rgbs[i_next] }}'
  
        - service: light.turn_on
          target:
            entity_id: "{{ lights_to_change }}"
          data:
            rgb_color: "{{ color_rgb_next }}"
            transition: "{{ transition_time }}"
        
        - service: logbook.log
          data:
            name: "Debug (target): "
            message: "{{ color_rgb_next | join(', ') }}"
        
        # The target color might differ from the command. Recover the target color of the light to compare against it later
        - variables:
            color_rgb_next: "{{ state_attr(lights_to_change[0], 'rgb_color') }}"
            
        - service: logbook.log
          data:
            name: "Debug (new target): "
            message: "{{ color_rgb_next | join(', ') }}"
            
        - delay: "{{ transition_time }}"
        
        - service: logbook.log
          data:
            name: "Debug (current state): "
            message: "{{ state_attr(lights_to_change[0], 'rgb_color') }}"
        
        # This displays the same thing as the call above
        - service: logbook.log
          data:
            name: "Debug (current target): "
            message: "{{ color_rgb_next }}"

      # Means the light target color has been changed elsewhere
      until: "{{ not is_state_attr(lights_to_change[0], 'rgb_color', color_rgb_next) }}"
      
  - service: logbook.log
    data:
      name: "Debug (current state after loop): "
      message: "{{ state_attr(lights_to_change[0], 'rgb_color') }}"

Found out a solution using “if” and “stop”.
It seems variable in a loop are not accessible in the until instruction, which is really strange

# break the loop. Until cannot access the local mustExit variable
        - if:
            condition: template
            value_template: "{{ mustExit }}"
          then:
           -  stop: "Stop running the loop"

If you have found the solution, please click the solution button to close the topic.