How to convert color formats, how to use light_profiles.csv, how to have favorite light colors?

Introduction

I wanted to find a way to define favorite “colors” that I could later apply to any light. Then I discovered the Light component supports a light_profiles.csv. The documentation is sparse, but I manged to figure it out.

Researching

Each row (disregarding the first row — header) has to contain exactly 4 or 5 cells (separated by comma): profile name (string), color_x (float), color_y (float), brightness (byte), transition (positive integer, optional).

Cool, now I want to setup some light profiles myself. So I thought of doing these steps:

  1. Set the light to the desired color and intensity.
  2. Inspect the light attributes to find the xy values.
  3. Write them down into config/light_profiles.csv.

However, I found some issues:

  • Recent Home Assistant versions don’t display the raw values anymore at the bottom of the light entity dialog.
    • Okay, that’s an easy workaround. Just go to Developer Tools and view the entire entity state in there.
  • But my light has color_temp, but not xy_color.
    • What If I try to access it directly from a template? Hmm… state_attr('…', 'xy_color') returns None, so it doesn’t work.
    • Surely there should be some way to convert colors in Home Assistant template. Well, doesn’t seem like so.
    • But Home Assistant internally has color-conversion functions! Can we access those from the template? Well, doesn’t seem possible.
    • If it were possible, can I directly convert mired to xy? Well, first I have to convert mired to temperature in kelvin, and then convert that to either hs or rgb, and then convert that to xy. That’s a lot of steps.
  • Okay, I spent enough time searching for it, and I’m stuck.

TL;DR / Questions

How can I easily convert from color_temp (in mired) to xy? (With the objective of adding to light_profiles.csv.)

If the color profiles are defined in xy, would they work on lights that have color_temp only? (Doesn’t seem to work.)

Is there any (better) way to save a set of favorite colors/intensities so I can apply them to different lights? (Scenes are interesting, but they record the precise state of a light, and the state is tied to that specific light. If light profiles sounds like the solution, but doesn’t seem to work well nor is easy-to-setup.)

I’m trying to figure out exactly the same thing. It seems that x,y colours only work with lights that support RGB. I have some Hue ambient lights that only support colour temperatures and I can’t get light_profiles.csv to work properly with them - I can get it to set the brightness, but it seems to ignore the x,y values.

What I’m looking at now is setting the default in light_profiles.csv to be a very low brightness, and then having an automation that triggers when the light is turned on - the automation will then set the light to my desired colour temperature and brightness. With a suitable transition time it could look quite good.

It might not be the most elegant solution, but then I want to extend it to have different colour temperatures according to the time of day.

https://html-color-codes.info/convert-color-format/

Sorry, but the link is completely unrelated to this topic. That website only converts RGB, HSL, HSV, CMYK, while this topic is talking about temperature (in kelvin and in mired) and xy.

Sorry. Anyway there is plenty of info out there about conversions. It is just math, and computers are good at that.

I did find a link to convert kelvin to xy: https://www.luxalight.eu/en/cie-convertor

Converting mired to kelvin is simple: kelvin = 1000000/mired

The problem is that the xy values will always set a colour. Setting x,y to 0.5056,0.4152 corresponds to approximately 2200K. Doing that on a colour Hue bulb sets the RGB colours. That’s fine on a bulb that supports colours, but doesn’t work on the ambient bulbs that only support color_temp.

I’m guessing HA is only attempting to convert to RGB and the fix would be to check the light’s supported features and convert to color_temp if the light doesn’t support RGB.

2 Likes

There is a feature request forum, alternatively you could submit a PR.

exactly which bulbs?

I think any lamp that supports color temperature (i.e. cold and warm white), but not RGB. Like Philips Hue “Ambience” bulbs and IKEA TRÅDFRI bulbs.

Mine are Hue Ambient.

Looking at the code, when the data is loaded from light_profiles.csv it is converted into hue, saturation values.

The ambient lights only have a color_temp attribute, while the actual Hue colour lights show hs_color, rgb_color and xy_color attributes when set to a colour (or color_temp if they were set to a colour temperature rather than a colour - in which case they don’t show the three colour attributes)

Hi All

If still interested, the link below has the formula to approximate converting from kelvin to xy. I’ve put this into the template editor and it appears to work OK (change the kelvin value to whatever you want greater tha 1667 and less than 25000). If I plug in 6500, the xy values appear to land correctly on the RGB color wheel, but my lights look maybe a little bluer than I would have expected. I suspect it’s a calibration thing for my lights.

{% set kelvin = 6500 %}

{% if 1667 <= kelvin and kelvin < 4000 %}
  {% set x = -0.2661239*10**9/kelvin**3 - 0.2343589*10**6/kelvin**2 + 0.8776956*10**3/kelvin + 0.179910 %}
{% elif 4000 <= kelvin and kelvin <= 25000 %} 
  {% set x = -3.0258469*10**9/kelvin**3 + 2.1070379*10**6/kelvin**2 + 0.2226347*10**3/kelvin + 0.240390 %}
{% else %}
  {% set x = 1 %}
{% endif %}
{{ x }}

{% if 1667 <= kelvin and kelvin < 2222 %}
  {% set y = -1.1063814*x**3 - 1.34811020*x**2 + 2.18555832*x - 0.20219683 %}
{% elif 2222 <= kelvin and kelvin < 4000 %} 
  {% set y = -0.9549476*x**3 - 1.37418593*x**2 + 2.09137015*x - 0.16748867 %}
{% elif 4000 <= kelvin and kelvin <= 25000 %} 
  {% set y = 3.0817580*x**3 - 5.87338670*x**2 + 3.75112997*x - 0.37001483 %}
{% else %}
  {% set y = 1 %}
{% endif %}
{{ y }}

Another method is to convert Kelvin to RGB and have Home Assistant do any further conversion. This page provides such an algorithm. It claims to estimate well down to a lower temperature (1000K), which is nice for night lighting.

The result looks good so far on several different Hue bulbs (which only support xy_color and hs_color. These bulbs have their own color temperature down to 2000K, and I wanted to go lower, so this came in useful.

The following can be used in a template:

{% set kelvin_tmp = kelvin / 100 %}
{% set r = 255 if kelvin_tmp <= 66 else max(0, int(329.698727446 * (kelvin_tmp - 60) ** -0.1332047592)) %}
{% set g = 99.4708025861 * log(kelvin_tmp) - 161.1195681661 if kelvin_tmp <= 66 else max(0, int(288.1221695283 * (kelvin_tmp - 60) ** -0.0755148492)) %}
{% set b = 255 if kelvin_tmp >= 66 else max(0, int(138.5177312231 * log(kelvin_tmp - 10) - 305.0447927307)) if kelvin_tmp > 19 else 0 %}
{{ [r, g, b] }}