Hey there!
I have a remote for a desk light that I’ve modified with an esp but I’m running into an issue with the color temp. Basically the remote uses a rotary encoder for the brightness and color temperature which means I have “press” a button x number of times. I’ve gotten the brightness working but now the issue is that I can’t figure out the color temperature. The temperature range is 3000k-6500k but only has 15 steps in between the two. I currently have a number slider that works but I can’t integrate it into a light entity. I’m trying to figure out how I’d convert it so 15 would be 3000 and 0 6500k. Please ignore the code since I had to mash some scraps I found together. Please let me know if there is anything I could improve!
light:
- platform: color_temperature
name: "Desk Light"
id: desklight
color_temperature: templateout_temp
brightness: templateout
cold_white_color_temperature: 6500 K
warm_white_color_temperature: 3000 K
globals:
- id: brightness_prev
type: int
restore_value: no
initial_value: '-1'
- id: temp_prev
type: int
restore_value: no
initial_value: '-1'
- id: executed
type: bool
output:
- platform: template
id: templateout
type: float
write_action:
- number.set:
id: brightness_slider
value: !lambda |-
return (int(id(desklight).remote_values.get_brightness()*41));
- platform: template
id: templateout_temp
type: float
write_action:
- number.set:
id: temp_slider
value: !lambda |-
return (int(id(desklight).remote_values.get_color_temperature()*15));
- platform: gpio
pin: D1
id: gpio_d1
inverted: true
- platform: gpio
pin: D2
id: gpio_d2
inverted: true
- platform: gpio
pin: D3
id: gpio_d3
inverted: true
- platform: gpio
pin: D5
id: gpio_d5
inverted: true
- platform: gpio
pin: D6
id: gpio_d6
inverted: true
button:
- platform: template
name: Decrease
id: bright_down
on_press:
- lambda: |-
id(gpio_d1).turn_on();
delay(5);
id(gpio_d2).turn_on();
delay(5);
id(gpio_d1).turn_off();
delay(5);
id(gpio_d2).turn_off();
delay(5);
- platform: template
name: Increase
id: bright_up
on_press:
- lambda: |-
id(gpio_d2).turn_on();
delay(5);
id(gpio_d1).turn_on();
delay(5);
id(gpio_d2).turn_off();
delay(5);
id(gpio_d1).turn_off();
delay(5);
- platform: template
name: Warm
id: warm
on_press:
- lambda: |-
id(gpio_d5).turn_on();
delay(5);
id(gpio_d6).turn_on();
delay(5);
id(gpio_d5).turn_off();
delay(5);
id(gpio_d6).turn_off();
delay(5);
- platform: template
name: Cold
id: cold
on_press:
- lambda: |-
id(gpio_d6).turn_on();
delay(5);
id(gpio_d5).turn_on();
delay(5);
id(gpio_d6).turn_off();
delay(5);
id(gpio_d5).turn_off();
delay(5);
- platform: template
name: Power
id: power
on_press:
- output.turn_on: gpio_d3
- delay: 50ms
- output.turn_off: gpio_d3
number:
- platform: template
name: brightness
id: brightness_slider
min_value: 0
max_value: 41
initial_value: 0
step: 1
optimistic: true
on_value:
then:
- lambda: |-
int steps = 0;
//Just booted up? Get the LED strip to known value (0), otherwise get the difference
if (id(brightness_prev) == -1)
steps = -41;
else
steps = x - id(brightness_prev);
//No difference? Don't do anything.
if (steps==0)
return;
//Should we go up or down?
auto button = steps>0 ? bright_up : bright_down;
//How many steps?
steps = abs(steps);
//Press the button repeatedly
for (int i=0;i<steps;i++)
{
button->press();
delay(3);
}
ESP_LOGI("main", "Brigthness: %d -> %d", id(brightness_prev), id(brightness_slider).state);
//Save current value for future change
id(brightness_prev) = x;
- platform: template
name: Temp
id: temp_slider
min_value: 0
max_value: 15
initial_value: 0
step: 1
optimistic: true
on_value:
then:
- lambda: |-
int steps = 0;
//Just booted up? Get the LED strip to known value (0), otherwise get the difference
if (id(temp_prev) == -1)
steps = -15;
else
steps = x - id(temp_prev);
//No difference? Don't do anything.
if (steps==0)
return;
//Should we go up or down?
auto button = steps>0 ? warm : cold;
//How many steps?
steps = abs(steps);
//Press the button repeatedly
for (int i=0;i<steps;i++)
{
button->press();
delay(3);
ESP_LOGD("Custom", "Temp: %f", int(id(desklight).remote_values.get_color_temperature()*15));
}
ESP_LOGI("main", "Temp: %d -> %d", id(temp_prev), id(temp_slider).state);
//Save current value for future change
id(temp_prev) = x;