Condition: template messing up

I’m learning to make my own scripts and I have this one script (the contents of which I will apply to other blueprints and scripts I’m currently working on), see below.
As you might see, I want the lights to turn on at 50% brightness and 3500K and gradually brighten over a span of 10 minutes to 5000K and 85%.
I also want to be able to change the brightness or color temperature manually, which will stop the script from running (not for this script necessarily, but it’s the simpelest to show my issue).
I added a condition that is supposed to check if the context of an entity was changed by something other than this automation, and if true, the scipt stops looping and keeps its brightness on the brightness/temperature that were set.
However, this condition is messing up my script and having it stuck at 50% brightness and 3500K temperature.

I read somewhere on this forum that a context condition could be added to an automation to recognize ‘manual’ state changes.
Hope someone can help me out!

sequence:
  - target:
      area_id:
        - keuken
        - woonkamer
    data:
      brightness_pct: 50
      kelvin: 3500
    action: light.turn_on
  - variables:
      start_kelvin: 3500
      stop_kelvin: 5000
      start_bright: 50
      stop_bright: 85
      duration: 60
      intervals: "{{ duration * 2 }}"
      step_kelvin: "{{ ((stop_kelvin - start_kelvin) / intervals) }}"
      step_bright: "{{ ((stop_bright - start_bright) / intervals) }}"
  - repeat:
      until: "{{ repeat.index == intervals }}"
      sequence:
        - condition: template
          value_template: >-
            {{ state_attr(light.keuken, 'context') is not none and
            state_attr(light.keuken, 'context')['user_id'] != context.user_id }}
        - target:
            area_id:
              - keuken
              - woonkamer
          data:
            brightness_pct: "{{ (start_bright + (step_bright * repeat.index)) | round }}"
            kelvin: "{{ (start_kelvin + (step_kelvin * repeat.index)) | round }}"
          action: light.turn_on
        - delay: "5"
  - target:
      area_id:
        - keuken
        - woonkamer
    data:
      brightness_pct: 85
      kelvin: 5000
    action: light.turn_on
alias: 1. Wake-up
description: ""

I don’t see a condition in your script.

Oops, I forgot to add it back.

   sequence:
     - condition: template
          value_template: >-
            {{ state_attr(light.keuken, 'context') is not none and
            state_attr(light.keuken, 'context')['user_id'] != context.user_id }}```
        - condition: template
          value_template: >-
            {{ state_attr(light.keuken, 'context') is not none and
            state_attr(light.keuken, 'context')['user_id'] != context.user_id }}

Thanks! But I’d like to learn to make it myself so can understand what’s happening and how to adjust the settings for my specific situation

Unless you’re going to do something fancier than a linear fade, why use a loop at all? Just use two light.turn_on actions after each other with a transition parameter on the second.

If you insist on doing it with a manual loop, you cannot put the break condition inside the loop as that will only skip the current iteration and jump immediately to the next. You need to put your break conditions as part of until (or while) on the loop itself.

I do something similar in my alarm clock blueprint where I am slowly increasing the volume of a media player. If the volume at the beginning of each iteration is not what I expected it to be, i.e. someone has manually adjusted the volume, I break the loop.

You can also learn by reviewing the code of the linked example. It’s more feature-rich and you may wish to borrow from it for your own use.

context and user_id are not attributes of an entity. They are properties of an automation’s trigger variable.

To see what I mean, copy-paste your template condition into Developer Tools → Template and observe the result.

In addition, the first parameter for the state_attr() function must be a string or a variable containing a string. In your example, it contains an unquoted light.keuken so Home Assistant handles it like a variable (not a string).