No set_color_temperature_kelvin()

Hello
Recently I started with my very first ESPHome project, noticing there is no esphome::light::LightCall::set_color_temperature_kelvin() method available.
The only method available is set_color_temperature() and takes mireds as an argument.

At the same time, retrieving light warmth supports both: mireds and kelvin:

esphome::light::LightColorValues::get_color_temperature()
esphome::light::LightColorValues::get_color_temperature_kelvin()

Coincidentally, the March release of HA comes with decomisioning of mireds support in favor of Kelvin.

Since I’m starting with ESPHome, it’s possible that I missed a bit of the puzzle.
Can you clarify the reason for such an inconsistency?

From the docs I think this might accept Kelvin or mireds, possibly inferring the unit from the range (unconfirmed).

color_temperature (Optional, float, templatable): The color temperature (in mireds or Kelvin) of the white channel.

I tried to trace this through the source code of the api but couldn’t quite follow the crumbs.

https://api-docs.esphome.io/color__mode_8h_source#l00049

What is even more “confusing” is, that following methods exist:

void esphome::light::LightColorValues::set_color_temperature()
void esphome::light::LightColorValues::set_color_temperature_kelvin()

Could you link to your source please?

Hi, thank you for your interest

At first, it seems that esphome::light::LightCall::set_color_temperature() doesn’t allow to pass kelvin value. An attempt throws an error:

'Light': Color temperature value 3460.00 is out of range [153.8 - 370.4]

I suppose that the range is inherited from light properties. It matches my light definition:

light:
  - platform: cwww
    id: desk_light
    name: "Light"
    warm_white: warm_pwm
    cold_white: cool_pwm
    warm_white_color_temperature: 2700 K
    cold_white_color_temperature: 6500 K

You will see in one of the last lines of code below that I need to convert Kelvin to mireds.

Here is the lambda activated on button press. It has to step up warmth by 20%:

                const float max_k = 6500;  // cold end
                const float min_k = 2700;  // warm end
                const float step_k = (max_k - min_k) * 0.2;  // 20%

                float current_k = id(desk_light).current_values.get_color_temperature_kelvin();

                ESP_LOGD("LIGHT", "MIN: %.1f   MAX: %.1f   STEP: %.1f  CURRENT: %.1f", min_k, max_k, step_k, current_k);

                // If light was off or undefined, default to warm
                if (current_k < min_k || current_k > max_k) current_k = min_k;  // default to warm

                float next_k = current_k + step_k;
                if (next_k > max_k) next_k = min_k;

                auto call = id(desk_light).turn_on();
                call.set_color_temperature(1000000.0f / next_k);
                call.set_transition_length(300);  // ms
                call.perform();

BTW, if I make the whole lambda (from beginning) to work in the mireds domain, the 20% steps become not linear!!! At the end of the day, it became obvious, but surprising at first glance. It clearly manifests when looking on warmth slider of the light card.

I found get/set color temp kelvin in the source code (for my curiosity).

See around row 265.

set_color_temperature_kelvin seems to convert to Mireds (with the 1000000). Although my cpp is beginner, so I find it a bit hard to read along with the source code.

https://api-docs.esphome.io/light__color__values_8h_source

set_color_temperature_kelvin(float color_temperature) {
    if (color_temperature <= 0) {
      return;
    }
    this->color_temperature_ = 1000000.0 / color_temperature;
  }

https://api-docs.esphome.io/classesphome_1_1light_1_1_light_color_values#a5da2e70d7e87091adb05325b7adcee6a

Sounds like your already on your way, I just took a second look for my own interest.

Indeed. I already mentioned this function. It exists as a method of esphome::light::LightColorValues class.

But it doesn’t exist inesphome::light::LightCall class, which is needed when using call object like in my example:

auto call = id(desk_light).turn_on();
call.set_color_temperature(1000000.0f / next_k);

Actually this thread is about why esphome::light::LightCall class has no setter for kelvin, while kelvin supporting methods appear in other places. It seems like kind of inconsistency.

1 Like

Not sure if you’ve found the ESPHome Discord yet but a lot of the devs are very active there and would probably be of help.

1 Like