本帖最后由 602293544 于 2024-5-26 17:56 编辑
硬件参考 https://bbs.hassbian.com/thread-23541-1-1.html
自己又加了继电器,一个控制灯,一个控制风扇火线,一个蜂鸣器,一个433接收方便遥控
自行编译
substitutions: #每个文件添加这个
device_name: fan_light
esphome:
name: $device_name
platform: ESP8266
board: nodemcuv2 #nodemcuv2 esp8285 esp01_1m
on_boot: #重启蜂鸣一下,提示硬件已准备就绪
priority: -100
# ...
then:
- switch.turn_on: relay3
- delay: 200ms
- switch.turn_off: relay3
wifi:
ssid: !secret wifi_shufang
password: !secret wifi_password
reboot_timeout: 120s #超时重新连接等待时间
fast_connect: true # true,直接连接到 WiFi 网络而不先进行扫描。隐藏网络必选,可以显着缩短连接时间。默认为false. 缺点是此选项连接到 ESP 看到的第一个网络,即使该网络非常远且有更好的网络可用。
power_save_mode: none #省电模式
# manual_ip:
# static_ip: 192.168.123.68
# gateway: 192.168.123.1
# subnet: 255.255.255.0
ap:
# ssid: wifi_ssid_AP #禁用: 将设备名称作为 ssid
ssid: ${device_name}_ap
password: "12345678"
api:
reboot_timeout: 0s #连接home assistant 默认不重启
ota:
# safe_mode: true #安全模式,没密码不能ota
# password: 123456
captive_portal: #连不到WiFi进AP模式配网
web_server: #web模式
port: 80
logger:
level: DEBUG
text_sensor:
- platform: wifi_info
ip_address: {"name": "${device_name}_IP","icon": "mdi:ip"}
mac_address: {"name": "${device_name}_MAC","icon": "mdi:access-point-network"}
- platform: version
name: "ESPHome Version"
- platform: template
name: fan_speed
id: global_value
button:
- platform: restart
name: ${device_name}_reboot
globals: #全局参数,存储风扇风速
- id: shared_value
type: int
initial_value: '0'
output:
- platform: ac_dimmer
id: dimmer1
min_power: 0
max_power: 1
gate_pin: GPIO13 #用于控制可控硅的引脚。
# inverted: true
method: leading pulse
zero_cross_pin: #用于感测交流过零事件的引脚
number: GPIO12
mode:
input: true
# inverted: true
- platform: template #220v不是线性的,到一定值才会启动,通过模板模拟可以从0开始启动,自行测试修改下方函数
id: outputsplit
type: float
min_power: 0
max_power: 1
write_action:
- output.set_level:
id: dimmer1
level: !lambda |-
if(state == 0)
{
return 0;
}
else
{
return (state * 0.27 + 0.6);
}
fan:
- platform: speed
# output: dimmer1
output: outputsplit
id: little_fan
name: "little fan"
speed_count: 100
on_turn_on:
- switch.turn_on: relay2
on_turn_off:
- switch.turn_off: relay2
- lambda: |-
id(shared_value) = 0;
id(global_value).publish_state(std::to_string(id(shared_value)));
ESP_LOGD("little_fan", "speed is: %d", id(shared_value));
on_speed_set:
then:
- lambda: |-
id(shared_value) = id(little_fan).speed;
id(global_value).publish_state(std::to_string(id(shared_value)));
ESP_LOGD("little_fan", "speed is: %d", id(shared_value));
switch:
- platform: gpio
name: ${device_name}_light
pin: 14
id: relay1
on_turn_on:
- switch.turn_on: relay3
- delay: 200ms
- switch.turn_off: relay3
on_turn_off:
- switch.turn_on: relay3
- delay: 200ms
- switch.turn_off: relay3
- platform: gpio
name: ${device_name}_fanswitch
pin: 16
id: relay2
on_turn_on:
- switch.turn_on: relay3
- delay: 200ms
- switch.turn_off: relay3
on_turn_off:
- switch.turn_on: relay3
- delay: 200ms
- switch.turn_off: relay3
- platform: gpio
pin: 15
id: relay3
remote_receiver:
pin: GPIO4
dump:
- rc_switch
tolerance: 50%
filter: 250us
idle: 4ms
buffer_size: 2kb
binary_sensor:
- platform: remote_receiver
# name: "Remote_light"
id: Remote_light
filters:
- delayed_off: 300ms
rc_switch_raw:
code: "000000011000011010100010"
on_press:
then:
- switch.toggle: relay1
- platform: remote_receiver
# name: "Remote_fan_up"
id: Remote_fan_up
filters:
- delayed_off: 300ms
rc_switch_raw:
code: "000000011000011010100011"
on_press:
then:
- lambda: |-
if (id(shared_value) >= 90) {
// global value is greater than 5
id(shared_value) = 100;
id(global_value).publish_state("100");
} else {
id(shared_value) += 10;
id(global_value).publish_state(std::to_string(id(shared_value)));
}
ESP_LOGD("little_fan", "speed is: %d", id(shared_value));
- fan.turn_on:
id: little_fan
speed: !lambda return id(shared_value);
- platform: remote_receiver
# name: "Remote_fan_down"
id: Remote_fan_down
filters:
- delayed_off: 300ms
rc_switch_raw:
code: "000000011000011010100100"
on_press:
then:
- lambda: |-
if (id(shared_value) <= 10) {
// global value is greater than 5
id(shared_value) = 0;
id(global_value).publish_state("0");
} else {
id(shared_value) -= 10;
id(global_value).publish_state(std::to_string(id(shared_value)));
}
ESP_LOGD("little_fan", "speed is: %d", id(shared_value));
- if:
condition:
lambda: 'return id(shared_value) >= 10;'
then:
- fan.turn_on:
id: little_fan
speed: !lambda return id(shared_value);
else:
- fan.turn_off:
id: little_fan
- platform: remote_receiver
# name: "Remote_fan_off"
id: Remote_fan_off
filters:
- delayed_off: 300ms
rc_switch_raw:
code: "000000011000011010100001"
on_press:
then:
- lambda: |-
id(shared_value) = 0;
id(global_value).publish_state("0");
ESP_LOGD("little_fan", "speed is: %d", id(shared_value));
- fan.turn_off: little_fan
|