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:
- For some reason the “Repeat” action is occurring 64 times instead of the 60 specified. Can anyone explain why this is?
- 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.
- 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.