Triggering entity to fade from start point to end point at a given time

I should start off by saying I’m quite new to HomeAssistant and Automations (and YAML in general), but I’ve been trying to develop an automation to fade a light entity from a “start point” (Brightness = X0, Color_temp = Y0) to an “end point” (Brightness = X1, Color_temp = Y1) and all of this would happen at a given “trigger time”. At first I tried modifying the popular “wake up light” blueprint that’s on these forums, but I quickly found out that wasn’t going to work so I tried developing my own. After quite a few errors, I tried to scale back and just set up a working automation and I’m running into some issues. The automation that I currently have is as follows:

alias: Bedtime
description: Turn on the lights and slowly fade them off
# Time to trigger fade event
trigger:
  - platform: time
    # Ideally would like to have this be a variable: 'Trigger_time'
    # for testing purposes was using current time as trigger time and (current time + 1 minute) for Trigger_time+Fade_Duration
    at: "15:40:00"
#Check if I'm home
condition:
  - condition: state
    entity_id: person.me
    state: home
action:
  #Check if light is off, if so then turn it on to max brightness and lowest color temp
  - if:
      - condition: state
        entity_id: light.bedroom_lights
        state: "off"
    then:
      - service: light.turn_on
        data:
          color_temp: 1
          brightness_pct: 100
        target:
          entity_id: light.bedroom_lights
  #Begin the "fade" process
  - repeat:
      #How many times to "tick". I've tried different types of repeats and none work perfectly
	  #Ideally would like the numbers here to be variables: (Start_brightness - End_brightness)/(Delta_brightness/Fade_duration)
	  # The "abs" is there because I would like to not only fade the light to turn it off but also turn it on (ie: Fade from 0 to 100)
      count: "{{ (( (100 - 1) | abs)/(99/60)) }}"
      sequence:
        - service: light.turn_on
          data:
		    #Change the brightness every "tick" depending on the current time vs the (Trigger_time + Fade_duration)
            brightness_pct: >-
              {{ (( 0 ) + ( (99/60) * (as_timestamp(today_at("15:41:00")) -
              as_timestamp(now()) ) )| abs)}}
			#Change the color temp every "tick" depending on the current time vs the (Trigger_time + Fade_duration)
            color_temp: >-
              {{( 501 ) - ( (500/60) * (as_timestamp(today_at("15:41:00")) -
              as_timestamp(now()) ) )}}
          target:
            entity_id: light.bedroom_lights
	    #Ideally would like the delay to change depending on the "count" and the "duration" selected. ie: If the light is supposed to tick down
		#60 times in 1 minute then it would be best to have the delay be 1 second. But if it's ticking 60 times in 60 hours, then maybe the delay
		#would be best set to 1 hour
        - delay:
            hours: 0
            minutes: 0
            seconds: 1
            milliseconds: 0
  #Turn the light off at the end
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.bedroom_lights
mode: single

I’ve tried to enter as many comments as I could that would be relevant but I’m running into a few issues:

  1. For some reason the “Repeat” action is occurring 64 times instead of the 60 specified. Can anyone explain why this is?
  2. Due to the above issue and/or a rounding issue, the brightness occasionally goes below 0% at which point the light resets to 100%. I tried to combat this by taking the absolute value of brightness_pct as you can see in the code but this isn’t the greatest solution and also due to the count issue mentioned above, the light gets to roughly 0% then starts ticking upwards 0%>2%>4%, etc.
  3. I don’t like the idea of a standalone automation for this process. Especially when it seems like Variables can’t be used. Ideally I’d like to have everything be defined by variables so I don’t have to edit the code every time.
    Any help with what I’m trying to achieve would be appreciated.

Not to take anything away from your effort, but the script part of this has been solved pretty well. Maybe give it a go.

There’s also this more recent method that can fade according to multiple curve functions. It’s implemented as a python_script.

This one is meant for volume fading (media_player) and it also supports multiple curve functions. It’s implemented as a script and can serve as the basis for developing a version for lighting.

Thank you for your reply

The script in the link you provided didn’t work however there was a script further down in thread that did work. I would still like to develop my own script since I found the script wasn’t versatile enough for my needs.

@123 that’s very interesting, I was hoping to implement an exponential curvature later on. Does HA natively support python in scripts or is additionally setup required?