我想用旋转编码器+8266+esphome通过api做一个无线调整灯光亮度和色温的测试,现在出现了一个问题:
1、现有代码可以实现顺时针旋转编码器增加亮度,逆时针旋转编码器调暗灯光
2、长按通过color_temp_kelvin只会增加开尔文的数值,当达到灯的最大值后,不会循环减小,而且8266会掉线。
3、如果在esphome中建立一个灯,那么代码使用没问题
可以帮忙看看代码哪地方有问题吗,谢谢!
#旋转调整亮度
sensor:
- platform: rotary_encoder
name: "亮度旋钮"
pin_a:
number: RX
inverted: True
mode:
input: true
pullup: true
pin_b:
number: TX
inverted: True
mode:
input: true
pullup: true
resolution: 2
min_value: 0
max_value: 30
publish_initial_value: true
id: rotary1
on_clockwise:
- homeassistant.service:
service: light.turn_on
data:
entity_id:
light.color_taideng_2024
brightness_step_pct: "5"
transition: "0.1"
- delay: 0.1s
on_anticlockwise:
- homeassistant.service:
service: light.turn_on
data:
entity_id:
light.color_taideng_2024
brightness_step_pct: "-5"
transition: "0.1"
- delay: 0.1s
#长按调调颜色,单击开关灯
globals:
- id: my_global_inta
type: int
restore_value: no
initial_value: '2000'
binary_sensor:
- platform: gpio
name: "调试"
pin:
number: 5
inverted: True
mode:
input: True
pullup: True
internal: true
id: my_binary_sensor
on_press:
then:
- while:
condition:
- binary_sensor.is_on: my_binary_sensor
then:
- while:
condition:
- binary_sensor.is_on: my_binary_sensor
- lambda: |-
return (id(my_global_inta) >= 2000) && (id(my_global_inta) < 6536);
then:
- homeassistant.service:
service: light.turn_on
data:
entity_id: light.color_taideng_2024
transition: "0.1"
color_temp_kelvin: !lambda |-
id(my_global_inta) += 20;
return id(my_global_inta);
- delay: 0.1s
- while:
condition:
- binary_sensor.is_on: my_binary_sensor
- lambda: |-
return (id(my_global_inta) > 2000) && (id(my_global_inta) <= 6536);
then:
- homeassistant.service:
service: light.turn_on
data:
entity_id: light.color_taideng_2024
transition: "0.1"
color_temp_kelvin: !lambda |-
id(my_global_inta) -= 4000;
return id(my_global_inta);
- delay: 0.1s
on_click:
then:
- homeassistant.service:
service: light.toggle
data:
entity_id: light.color_taideng_2024
- delay: 0.05s
|