谢谢你 我大概弄一个场景和开关二选一的功能,打开opem mqtt按钮后 按键将不可以控制继电器 中间蓝色指示灯会亮 可以发送mqtt 单击 双击 长按 设置场景
1+2按键按住10秒切换模式 ,3+2按键按住重启面板
esphome:
name: sonoff_3
platform: ESP32
board: esp32dev
substitutions:
switch_name: sonoff_3
logger:
esp32_ble_tracker:
bluetooth_proxy:
api:
ota:
sensor:
# Wifi signal
- platform: wifi_signal
name: "${switch_name}_wifi"
update_interval: 60s
id: wifi1
wifi:
ssid: XXXXXXXXX
#password: !secret wifi_password
ap:
ssid: "${switch_name}"
password: "12345678"
captive_portal:
web_server:
port: 80
mqtt:
broker: 192.168.1.122
port: 1883
username: mqtt
password: mqtt
discovery: true
button: #重启
- platform: restart
name: "${switch_name}_restart"
# inverted: true
id: Restart
######## 这里是配置判断要不要单击开继电器 #################################
switch:
- platform: gpio
restore_mode: RESTORE_DEFAULT_ON
pin: GPIO26
name: "open_01"
id: open_01
on_turn_on:
then:
- switch.turn_off: switch_01
- switch.turn_off: switch_02
- switch.turn_off: switch_03
- switch.turn_on: led
on_turn_off:
then:
- switch.turn_off: led
- platform: gpio
restore_mode: RESTORE_DEFAULT_ON
pin: GPIO26
name: "last_hold1"
id: last_hold1
- platform: gpio
restore_mode: RESTORE_DEFAULT_ON
pin: GPIO26
name: "last_hold2"
id: last_hold2
- platform: gpio
restore_mode: RESTORE_DEFAULT_ON
pin: GPIO26
name: "last_hold3"
id: last_hold3
######## 这里是继电器配置 #################################
- platform: gpio
pin: GPIO23
name: "switch_01"
id: switch_01
on_turn_on:
then:
- switch.turn_off: open_01
#- switch.turn_on: LED_01
on_turn_off:
then:
#- switch.turn_off: LED_01
- platform: gpio
pin: GPIO19
name: "switch_02"
id: switch_02
on_turn_on:
then:
#- switch.turn_on: LED_02
- switch.turn_off: open_01
on_turn_off:
then:
#- switch.turn_off: LED_02
- platform: gpio
pin: GPIO22
name: "switch_03"
id: switch_03
on_turn_on:
then:
#- switch.turn_on: LED_03
- switch.turn_off: open_01
on_turn_off:
then:
#- switch.turn_off: LED_03
########这里是LED配置 ##################################
- platform: gpio
pin: GPIO05
inverted: true
name: "led"
id: led
on_turn_on:
then:
# - switch.turn_on: LED_04
#- switch.turn_off: open_01
on_turn_off:
then:
#- switch.turn_off: LED_04
output:
- platform: ledc
pin: GPIO18
id: gpio_18
# Example usage in a light
light:
- platform: monochromatic
output: gpio_18
name: "ledc Light"
######## 这里是按键配置 ##################################
##########按键1#####################
binary_sensor:
- platform: gpio
pin:
number: GPIO04
mode: INPUT_PULLUP
inverted: true
id: switch1
internal: true
on_multi_click:
- timing:
- ON for at most 0.5s
- OFF for at most 0.5s
- ON for at most 0.5s
- OFF for at least 0.2s
then:
- if:
condition:
switch.is_on: open_01
then:
- mqtt.publish:
topic: "/esp/${switch_name}/h1"
payload: "double"
- timing:
- ON for at most 0.3s
- OFF for at least 0.2s
then:
- if:
condition:
switch.is_on: open_01
then:
- mqtt.publish:
topic: "/esp/${switch_name}/h1"
payload: "single"
else:
- switch.toggle: switch_01
- timing:
- ON for at least 0.7s
then:
- if:
condition:
switch.is_on: open_01
then:
- mqtt.publish:
topic: "/esp/${switch_name}/h1"
payload: "hold"
- if:
condition:
switch.is_on: last_hold1
then:
- delay: 1s
- while:
condition:
binary_sensor.is_on: switch1
then:
- mqtt.publish:
topic: "/esp/${switch_name}/h1"
payload: "hold"
- delay: 1s
- timing:
- ON for at least 0.7s
- OFF for at least 0.2s
then:
- if:
condition:
switch.is_off: last_hold1
then:
- mqtt.publish:
topic: "/esp/${switch_name}/h1"
payload: "Not_hold"
- timing:
- ON for at least 10s
then:
- if:
condition:
binary_sensor.is_on: switch2
then:
- switch.toggle: open_01
on_press:
then:
- if:
condition:
switch.is_on: open_01
then:
#- switch.turn_on: LED_01
on_release:
then:
- if:
condition:
switch.is_on: open_01
then:
#- switch.turn_off: LED_01
##########按键2#####################
- platform: gpio
pin:
number: GPIO0
mode: INPUT_PULLUP
inverted: true
id: switch2
internal: true
on_multi_click:
- timing:
- ON for at most 0.5s
- OFF for at most 0.5s
- ON for at most 0.5s
- OFF for at least 0.2s
then:
- if:
condition:
switch.is_on: open_01
then:
- mqtt.publish:
topic: "/esp/${switch_name}/h2"
payload: "double"
- timing:
- ON for at most 0.3s
- OFF for at least 0.2s
then:
- if:
condition:
switch.is_on: open_01
then:
- mqtt.publish:
topic: "/esp/${switch_name}/h2"
payload: "single"
else:
- switch.toggle: switch_02
- timing:
- ON for at least 0.7s
then:
- if:
condition:
switch.is_on: open_01
then:
- mqtt.publish:
topic: "/esp/${switch_name}/h2"
payload: "hold"
- if:
condition:
switch.is_on: last_hold2
then:
- delay: 1s
- while:
condition:
binary_sensor.is_on: switch2
then:
- mqtt.publish:
topic: "/esp/${switch_name}/h2"
payload: "hold"
- delay: 1s
- timing:
- ON for at least 0.7s
- OFF for at least 0.2s
then:
- if:
condition:
switch.is_off: last_hold2
then:
- mqtt.publish:
topic: "/esp/${switch_name}/h2"
payload: "Not_hold"
on_press:
then:
- if:
condition:
switch.is_on: open_01
then:
#- switch.turn_on: LED_02
on_release:
then:
- if:
condition:
switch.is_on: open_01
then:
#- switch.turn_off: LED_02
##########按键3#####################
- platform: gpio
pin:
number: GPIO15
mode: INPUT_PULLUP
inverted: true
id: switch3
internal: true
on_multi_click:
- timing:
- ON for at most 0.5s
- OFF for at most 0.5s
- ON for at most 0.5s
- OFF for at least 0.2s
then:
- if:
condition:
switch.is_on: open_01
then:
- mqtt.publish:
topic: "/esp/${switch_name}/h3"
payload: "double"
- timing:
- ON for at most 0.3s
- OFF for at least 0.2s
then:
- if:
condition:
switch.is_on: open_01
then:
- mqtt.publish:
topic: "/esp/${switch_name}/h3"
payload: "single"
else:
- switch.toggle: switch_03
- timing:
- ON for at least 0.7s
then:
- if:
condition:
switch.is_on: open_01
then:
- mqtt.publish:
topic: "/esp/${switch_name}/h3"
payload: "hold"
- if:
condition:
switch.is_on: last_hold3
then:
- delay: 1s
- while:
condition:
binary_sensor.is_on: switch3
then:
- mqtt.publish:
topic: "/esp/${switch_name}/h3"
payload: "hold"
- delay: 1s
- timing:
- ON for at least 0.7s
- OFF for at least 0.2s
then:
- if:
condition:
switch.is_off: last_hold3
then:
- mqtt.publish:
topic: "/esp/${switch_name}/h3"
payload: "Not_hold"
- timing:
- ON for at least 10s
then:
- if:
condition:
binary_sensor.is_on: switch2
then:
- button.press: Restart
on_press:
then:
- if:
condition:
switch.is_on: open_01
then:
# - switch.turn_on: LED_03
on_release:
then:
- if:
condition:
switch.is_on: open_01
then:
# - switch.turn_off: LED_03
|