Ashley’s Light Fader 2.0✨: fade lights and/or color temperature with your choice of easing (curves), including ease-in, ease-out, and ease-in-out

@edi023 Ah—I’m glad to hear that things seem to be heading in the right direction!

And I’ll also be interested to hear how your full-scale test pans out now that—from what I understand—you’ve taken out the stopEntity portion.

yes I did, left that part out.
At the moment aim testing out the fade in music script, but that doesn’t look that promising.
when I have that up and running I will test it out completely.

Its a bit of a complicated automation, and I am still quite new to this and have te rely on the ui for the most part so i’am convinced that there will be a easer way to do things.

1 Like

@handcoding you’re light fader script worked perfectly this morning, thank you!

Next challange is to get music fade working :slight_smile:

1 Like

@edi023 Ah, fantastic—I’m very happy to hear that!

And as for the music fader, while I don’t want to get off topic here, I’ll just quickly mention that I think that we might be using the Smooth Media Player Volume Fade script in our morning routines, and I’ve DMed you with some potential ideas for debugging that part.

1 Like

I have to say, this is one of the nicest, most straightforward and useful scripts I’ve found on here.

Saying that - I’ve been wrecking my brains trying to implement colour temperature into your script somehow, and I keep hitting brick walls.

The way I’ve gone around it with night-time lighting fade-down is by first hitting the lights with a temperature change, if on, then roll your script on them. It does the job, but I’m sure there is a way to do this more elegantly :wink:

Thanks again, for your script!

1 Like

@kvikindi Many thanks for your kind words—that means a lot!

And while I’m not opposed to potentially adding a feature to have the script also fade color temperature, I’ve come across a bit of a couple of hiccups while trying to envision how I might approach such a feature. (And I’m open to any ideas or suggestions that you may have for getting around these problems:)

  • I’m not quite sure what might be a reasonable way for the script to handle a scenario in which someone were to request a color-temperature fade but the targeted lamp’s starting color wasn’t a traditional Kelvin color temperature. For instance, if a lamp’s starting color were green, then what should the script do? Should the script maybe just abort the fade in that case? Or should the script perhaps do the brightness portion of the fade but skip the color-temperature part of the fade? :woman_shrugging:t2:

  • A second issue is that whenever a light is off, it has no defined color_temp_kelvin value. And so that would make it rather difficult to calculate the fade curve in a scenario where someone were to be fading a light from off up to ##% while also fading its color temperature at the same time. (If it may be of interest, you can see this for yourself by checking on a light’s properties in Developer Tools → States and then manually turning off that light.)

  • And lastly, what might perhaps be a larger issue is that while there’s a single brightness-measuring system for lights (namely, its brightness attribute), there are several color-temperature systems that Home Assistant supports—Kelvin, xy, rgb, and mired—and Home Assistant doesn’t appear to have any means of converting between them.

    So just for a quick experiment, I checked on the current properties for the Hue lamp that’s next to my desk:

In this case, the lamp is currently set to 2906 Kelvin.

And since the lamp’s rgb value is listed as 255, 174, 103, I would presume that that rgb value would be equivalent to 2906 Kelvin? But if I manually set the lamp to rgb(255, 174, 103) using the Developer Tools → Services tab, the lamp’s color_temp_kelvin attribute disappears from its listing under the Developer Tools → States tab. (I guess because the lamp is technically now in rgb mode?) And on top of that, the lamp’s visual appearance with that new RGB doesn’t seem to match the original Kelvin value that it had before, which is seems weird.

The main hangup, though, is that it’s seemingly possible for someone to set a lamp to a particular RGB color or an XY color that happens to line up perfectly with an existing Kelvin color—but if someone were to do that, I don’t know how I’d be able to have the script fade that lamp to a target Kelvin color since the lamp’s color_temp_kelvin attribute disappears as soon as its color is set using RGB or XY.

As well, one might reasonably assume that surely there would have to be some formulas available that one could use to convert an RGB color to a Kelvin value or to convert from an XY color to a Kelvin value—but I’ve done a bit of looking around, and I haven’t seen any.

In theory, I’m not opposed to adding this color-temperature-fading feature—but as it is at the moment, I don’t know how to get around these roadblocks.

That being said, if anyone might have any ideas for how I could resolve these hiccups, I’d be legitimately interested. (And if it turns out that there may be a way to get around these problems, I’d then be quite open to trying to add this feature.)

1 Like

FWIW, there’s some information in this post that might be useful:

1 Like

@123 Thanks for the heads-up about that!

And while I don’t at all want to take away from @cloak’s commendable efforts there, it seems that that post offers these color-conversion functions, and of them, it doesn’t appear that there are currently functions for converting from those color systems to Kelvin (other than colour_mireds_to_kelvin)?:

  • cct_to_xy
  • xyY_to_XYZ
  • XYZ_to_sRGB
  • gamma_correct_sRGB
  • colour_temperature_to_rgb
  • rgb_to_hs
  • colour_mireds_to_kelvin

Not sure if this is what you want but googling “convert color to kelvin” produced many results; here’s one:

@123 Thanks for passing that along!

And while I don’t want to appear ungrateful for your helpful efforts in finding that, it so happens that my script is written in Jinja and it looks like the linked conversion relies upon Python’s Image.convert() function. And I don’t personally have the Python knowledge to be able to port that Python function to Jinja.

And while again I don’t want to appear ungrateful as I very much appreciate your engagement and comments here, I also can’t help thinking that even if I were able to find a formula for converting from RGB to Kelvin, I might still seem to have a gap in terms of converting from XY to Kelvin?

It’s using image.convert because the application has a completely different goal:

For a science project I had to convert a lot of JPG files to different color temperature output files.

The part that I thought was applicable is that it employs a map (supported by Jinja) to cross-reference temperature in Kelvin to an RGB value.

Why wonder when the answer is just a google away? :slightly_smiling_face:

I can understand where you’re coming from. And I agree that the map there is intriguing. All the same, I’m not quite sure how to make use of that map so as to convert in the opposite direction, from RGB to Kelvin? For instance, if a light were set to, say, rgb(255, 231, 211), how might I convert that to a Kelvin value?

I’ve tried googling for a formula to convert from XY to Kelvin, and I haven’t come up with anything. If you can find something, I would welcome it.

It wasn’t meant for that; it was meant for this:

It’s a map, not a formula, but easily implemented in Jinja2.

Literally the first search result:

Hi! I just found this script, and it almost exactly fits my needs - it’s brilliant! My one request would also be for colour support, so here’s my idea: Why not simply have a fade-from and fade-to value, both of which can be inputted as kelvin (or whatever is easiest for you to implement) and fade between them, instead of trying to fade from the current colour?

1 Like

@ThatBlockyPenguin Big thanks for your kind words!

And your idea could potentially work!

At the same time, I might imagine that there could also be occasions where the user might not have any way of knowing what a lamp’s current Kelvin value might be? (And I wouldn’t want to leave those folks hight and dry either.)

And so at a minimum, if I were to implement the feature that you had suggested, I think that I’d also want to implement the feature where the user only had to enter the final Kelvin value?

I definitely want to implement color fading of some sort, and I very much appreciate your thoughts and feedback on this!

This is something FANTASTIC !!! I would like to say GIANT THANK YOU for your time, work, effort and incredible result ! The script is great just as it is, but im glad that some updates are already planned. Fingers crossed, and my compliments once again !

1 Like

Dear @handcoding Ashley !

Maybe a dumb question, but i would like to kindly ask you about “End brightness level”. I try to explain the topic :
The slider allows easily change the desired end brightness level in percentage from 0% to 100%. I have some lights which i wish to DIM to value of “1” in scale “0-255”. I can switch the option “The lamp’s internal brightness scale” but the “End brightness level” remains in percents. I know it sounds strange, but the reason is, that some of our LED strips (especially in children’s room) remains on during night, just to avoid full darkness. In percent scale is 1% end brightness brighter as “value 1”. I know it is maybe construction fault of cheap bulbs and LED strip controllers, but it is clearly visible and maybe im not alone who has crap equipment like this. I can also after dimming period set another automation like “Light: Turn On” and set the brightness value to “1”, but it would be really awesome to have the option to set the End Brightness Level in “value range” also inside of your fantastic script.

Thank you very much !

Best wishes

Ronnie

1 Like

Hi, @ronnieSVK—thanks so much for your kind words!

And for whatever it’s worth, while I totally understand your use case—and I also have an idea in the next paragraph that may be able to help with that!—I’m also slightly hesitant to add a secondary brightness slider for 0–255 if only because the script already has tons of options, and I just feel that with each additional field that I were to add, the script might become a bit more daunting to newcomers?

All the same, I also have an idea that I think may be able to resolve your use case: I believe that you can achieve what you describe if you were to add an input-number helper within your Home Assistant instance? And if by chance you might be new to that sort of thing, here’re the steps for that if it may be of interest—

  1. Within your Home Assistant instance, go to Settings → Devices & services.
  2. Then click on the “Helpers” tab (across the top).
  3. From there, click on “Create Helper” (at the bottom right)
  4. Then select “Number”.
  5. And then on the next screen, you might enter values such as these:
    • Name: “Extra-low brightness”
    • Icon: [anything that you’d like]
    • Minimum value: 1
    • Maximum value: 5
    • Display mode: slider
    • Step size: 1
    • Unit of measurement: “brightness”

(And all together, that might look something like this:)

  1. And then click “Create”.

  2. After you’ve created it, you should then see an entry for “Extra-low brightness” (or whichever name you’ve given it) within the list of items on the “Helpers” tab.

  3. Lastly, I’d recommend clicking on that “Extra-low brightness” entry and then setting its slider’s value to “1” (if it isn’t already set to that value).

From here, if you then open to the automation where you’re calling Ashley’s Light Fader, you can then make use of that entity by entering its name within the ”Use an entity instead for the end-brightness value?” field and then setting the “The end-brightness entity’s brightness scale” field to “0 to 255”:

And with that in place, I think that that should hopefully do what you need. And if you might have any questions along the way, feel free to reply here, and I’ll be happy to chime in!

1 Like

Thank you for the really helpful script! I was just wondering how I could use this script to always fade my light if I change the brightness. So that if I change the brightness a fade should always be applied. I thought that using a number helper as an entity for the end-brightness value should do the job. It does work if I directly control the number helper. But I am controlling my lights through my Google Assistant and my Google Home App where the number helper does not appear and thus cannot be used. Is it somehow possible to make this work?

1 Like

Hi, @kubilayc38!

For better or for worse, what you describe—having this fader script be automatically applied for all light transitions—isn’t really possible. And that’s in part because when you change your lights through Google Assistant or the Google Home app, that controls the lights directly, which bypasses most of the triggers that Home Assistant can hook into.

With that being said, if the lights that you’re wanting to control might have built-in support for transitions, there’s a chance that you might be able to get an automatic fading effect whenever you change the lights’ levels through Google Assistant or the Google Home App by setting up a default transition time for some (or all) of your lights using light_profiles.csv.

I haven’t messed about with light_profiles.csv myself, so if by chance you were to get stuck, I unfortunately won’t be able to help you with that part. But if it may help as a starting point, it looks like this thread goes over the broad strokes of the syntax for light_profiles.csv.

1 Like