本帖最后由 kaka0992 于 2023-3-25 20:10 编辑
抄了个大神写的三档的风扇调节,我这边想增加到5档调节,不知道这个代码该怎么改
substitutions:
device_name: "auto Fan"
device_name_low: "auto-fan"
comment: "auto Fan"
version: "1.0.0"
speed_low: "30"
speed_med: "50"
speed_full: "80"
esphome:
name: "${device_name_low}"
project:
name: "n1.fan_controller"
version: "${version}"
on_boot:
then:
- delay: 10s
- fan.turn_on:
id: fan_speed
speed: "${speed_low}"
- delay: 5s
- fan.turn_on:
id: fan_speed
speed: "${speed_full}"
esp8266:
board: esp01_1m
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
password: "12345678"
wifi:
ssid: 1111 #WiFi SSID
password:111 #WiFi密码
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "${device_name_low}"
password: "12345678"
captive_portal:
web_server:
port: 80
output:
- platform: esp8266_pwm
pin: GPIO2 #D4
frequency: 15000 Hz
id: pwm_output
fan:
- platform: speed
id: fan_speed
output: pwm_output
speed_count: 100
name: "${device_name} 开关"
number:
- platform: template
icon: "mdi:fan"
id: speed_input
name: "${device_name} 风速"
lambda: |-
return id(fan_speed).speed;
step: 1
min_value: 20
max_value: 100
unit_of_measurement: "%"
update_interval: 1s
set_action:
then:
- fan.turn_on:
id: fan_speed
speed: !lambda "return x;"
- component.update: fanmode
- component.update: sensor_fanspeed
select:
- platform: template
icon: "mdi:fan"
name: "${device_name} 模式"
id: fanmode
update_interval: 1s
lambda: |-
if(id(fan_speed).speed <= ${speed_low})
return std::string("Low");
else if(id(fan_speed).speed == ${speed_med})
return std::string("Medium");
else if(id(fan_speed).speed >= ${speed_full})
return std::string("Full");
else
return std::string("Custom");
set_action:
then:
- lambda: |-
auto call = id(fan_speed).turn_on();
if(x == "Low")
call.set_speed(${speed_low});
else if(x == "Medium")
call.set_speed(${speed_med});
else if(x == "Full")
call.set_speed(${speed_full});
else
call.set_speed(id(speed_input).state);
call.perform();
- component.update: speed_input
- component.update: sensor_fanspeed
options:
- Low
- Medium
- Full
- Custom
button:
- platform: template
icon: "mdi:fan-speed-1"
name: "${device_name} Low"
id: fanlow
on_press:
- fan.turn_on:
id: fan_speed
speed: "${speed_low}"
- platform: template
icon: "mdi:fan-speed-2"
name: "${device_name} Med"
on_press:
- fan.turn_on:
id: fan_speed
speed: "${speed_med}"
- platform: template
icon: "mdi:fan-speed-3"
name: "${device_name} Full"
on_press:
- fan.turn_on:
id: fan_speed
speed: "${speed_full}"
- platform: restart
name: "${device_name} 初始化"
sensor:
- platform: template
id: sensor_fanspeed
icon: "mdi:fan"
name: "风扇速度"
lambda: |-
return id(fan_speed).speed;
update_interval: 1s
accuracy_decimals: 0
unit_of_measurement: "%"
- platform: dht
pin: GPIO4 #D2
temperature:
name: "${device_name} Temperature"
humidity:
name: "${device_name} Humidity"
model: AUTO_DETECT
update_interval: 5s #获取频率
|