就是抄3楼大佬的代码,我把我完整的代码贴出来
api key、ota password建议重新生成一下
wifi根据实际情况填写
最下面的sensor应该可以删掉,我没有接温湿度传感器
substitutions:
device_name: "pwmfan"
speed_low: "20"
speed_med: "50"
speed_full: "100"
esphome:
name: $device_name
friendly_name: 风扇调速器
esp8266:
board: esp01_1m
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "6szoHt/4a5L8F5t0nRZneOOF0Z866CcDUX/QtK8hk8g="
ota:
password: "1bb5fc0fbba711303f69620b8a2b14a8"
wifi:
ssid: ssid
password: password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Pwm Fallback Hotspot"
password: "password123"
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: 30
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: 20
- platform: template
icon: "mdi:fan-speed-2"
name: "${device_name} Med"
on_press:
- fan.turn_on:
id: fan_speed
speed: 50
- platform: template
icon: "mdi:fan-speed-3"
name: "${device_name} Full"
on_press:
- fan.turn_on:
id: fan_speed
speed: 100
- 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 #获取频率
|