This will create a new sensor.sun_colour_temp who’s value is the colour temperature outside in Mireds - the unit used by many colour temperature changing bulbs. I have constrained the values to between 250 and 454 as that is what my bulbs are limited to
I then use an automation to update the colour temperature of all my bulbs when sensor.sun_colour_temp changes.
Explanation
First it gets the ‘elevation’ attribute from sun.sun (in all default HA installations)
It constrains sun.sun’s elevation to positive values between 0 (sunset/sunrise) and 90 (midday)
It then applies a custom calculation to the elevation to calculate the colour temperature, I derived this formula from a graph in an academic paper, more importantly, I have been testing it and have found it to work well.
Rounds to the nearest integer
It then constrains the output to the range 250 - 454 which is the range of my IKEA Tradfri bulbs support
For colour temperature in Kelvin, you can try this (untested), adjust 2200 and 4000 to the supported range of your bulbs: {{ [ [ (1000000/(4791.67 - 3290.66/(1 + 0.222 * [ [ 0, state_attr('sun.sun', 'elevation') ] | max, 90 ] | min**0.81)))|int, 2200] | max, 4000] | min }}
Edit: Removed unnecessary/legacy sensor and added Explanation
But why are you creating the sun_elevation entity and don’t use it in the second sensor (not that it’s needed).
It seems to me as the first sensor is not needed.
Except the formula is for a curve that is based on colour temperature not brightness. You could give it a go and see if it gives the desired results - I doubt you would though, it will almost never spit out a number near 0.
{{state_attr('sun.sun', 'elevation') + 10}} would give you a number that varies between 10 at sunrise/sunset and 100. If you use that to set brightness percentage…
Or {{state_attr('sun.sun', 'elevation') /90*100}} would vary between 0-100.
But beware the seasons and your latitude would effect the brightness. The sun only reaches maximum elevation in the summer. Unless you live on the equator…
Otherwise to get 100% brightness at midday every day and 0 at sunrise you need a different formula.
This post has info on getting the percentage of the day remaining Sun elevation based on percentage of day which is tangentially related to what you want to achieve