本帖最后由 shadowba 于 2023-6-1 21:46 编辑
一直想把老吊扇拆了,老婆大人不同意,说最实用的就是它,夏天用风很大,很凉快。算了改造一下吧!1.材料,旋转编码器一个,四联继电器模块,8266.吊扇调速电容
用旋转编码器替换波段开关,需要电路基础
2.实现功能,小爱接入,三挡调速,多余的引脚顺便控制了灯
3.代码如下:
esphome:
name: living-room
friendly_name: living-room
esp8266:
board: esp01_1m
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
password: "0000000000000"
wifi:
ssid: 0000000000000
password: 000000000000
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "living_room Fallback Hotspot"
password: "0000000000000"
captive_portal:
web_server:
port: 80 # web端口
binary_sensor:
# 继电器高档二进制取反
- platform: template
id: sw3
lambda: |-
if (id(relay3).state) {
return false;
} else {
return true;
}
# 继电器中档二进制取反
- platform: template
id: sw2
lambda: |-
if (id(relay2).state) {
return false;
} else {
return true;
}
# 开关机、调速
- platform: gpio
id: btnpower
#是否对外隐藏(true/false)
#internal: true
pin:
number: GPIO13
mode: INPUT_PULLUP
# on_press:
# - switch.toggle: switch0
filters:
- invert:
- delayed_on_off: 40ms
on_press:
then:
- switch.toggle: switch0
- platform: gpio
id: btnoff
#是否对外隐藏(true/false)
internal: true
pin:
number: GPIO14
mode: INPUT_PULLUP
# on_press:
# - switch.toggle: switch1
filters:
- invert:
- delayed_on_off: 100ms
on_press:
- switch.toggle: switch1
- platform: gpio
pin:
number: GPIO0
mode:
input: true
pullup: true
inverted: true
name: yiya_light
on_press:
- switch.toggle: relay4
switch:
- platform: template
name: "fan_H"
id: relayH
icon: "mdi:fan-speed-3"
lambda: |-
if (id(relay1).state && id(relay2).state && id(relay3).state) {
return true;
} else {
return false;
}
turn_on_action:
- switch.turn_on: relay1
- switch.turn_on: relay2
- switch.turn_on: relay3
turn_off_action:
- switch.turn_off: relay1
- switch.turn_off: relay2
- switch.turn_off: relay3
- platform: template
name: "fan_M"
id: relayM
icon: "mdi:fan-speed-2"
lambda: |-
if (id(relay1).state && id(relay2).state && id(sw3).state) {
return true;
} else {
return false;
}
turn_on_action:
- switch.turn_on: relay1
- switch.turn_on: relay2
- switch.turn_off: relay3
turn_off_action:
- switch.turn_off: relay1
- switch.turn_off: relay2
- switch.turn_off: relay3
- platform: template
name: "fan_L"
id: relayL
icon: "mdi:fan-speed-1"
lambda: |-
if (id(relay1).state && id(sw2).state && id(sw3).state) {
return true;
} else {
return false;
}
turn_on_action:
- switch.turn_off: relay2
- switch.turn_off: relay3
- switch.turn_on: relay1
turn_off_action:
- switch.turn_off: relay1
- platform: template
name: "fan_on"
id: switch0
icon: "mdi:ceiling-fan"
turn_on_action:
then:
- if:
condition:
and:
- switch.is_off: relay1
- switch.is_off: relay2
- switch.is_off: relay3
then:
- switch.turn_on: relay1
else:
- if:
condition:
and:
- switch.is_on: relay1
- switch.is_off: relay2
- switch.is_off: relay3
then:
- switch.turn_on: relay2
else:
- if:
condition:
and:
- switch.is_on: relay1
- switch.is_on: relay2
- switch.is_off: relay3
then:
- switch.turn_on: relay3
else:
- if:
condition:
and:
- switch.is_on: relay1
- switch.is_on: relay2
- switch.is_on: relay3
then:
- switch.turn_off: relay1
- switch.turn_off: relay2
- switch.turn_off: relay3
- platform: template
#全部关闭
name: "fan_off"
#output: realy0
id: switch1
icon: "mdi:fan-off"
turn_on_action:
- lambda: |-
id(relay2).turn_off();
id(relay3).turn_off();
id(relay1).turn_off();
turn_off_action:
- lambda: |-
id(relay2).turn_off();
id(relay3).turn_off();
id(relay1).turn_off();
- platform: gpio
name: "fan_H_relay"
pin: 5
id: relay3
icon: "mdi:fan"
restore_mode: ALWAYS_OFF
- platform: gpio
name: "fan_M_relay"
pin: 4
id: relay2
icon: "mdi:fan"
restore_mode: ALWAYS_OFF
- platform: gpio
name: "fan_L_relay"
pin: 15
id: relay1
icon: "mdi:fan"
restore_mode: ALWAYS_OFF
- platform: gpio
name: "living_room_light"
pin: 12
id: relay4
icon: "mdi:wall-sconce-flat"
restore_mode: ALWAYS_OFF
- platform: restart
name: "living_Restart"
|