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:
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.
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.