本帖最后由 sorrypqa 于 2023-11-29 10:33 编辑
Sonoff的T1、T2、T3、M5系列86型开关一直以来是我的最爱,不是因为它好看,而是有插件方便接入HA,大部分还能纯本地控制,最重要的一点是方便刷ESPHome,定制各种功能,下面简单说一下怎么为M5系列智能开关添加雷达模块,
更改按键功能为单击时控制继电器、双击或长按时控制模板开关、在HA自动化里控制其它东西
下图这个位置刚好能安装LD2410B雷达模块,可以用热熔胶固定:
主板边缘要车一个6mm大小的缺口才能引出线:
先刷好ESPHome固件
主板里有5V、GND、TX、RX测试点,按图焊好线(蓝色线不用)
装上背板:
搞掂:
参考配置:
#打开Profile按钮后,按键将不可以控制继电器,中间蓝色指示灯会亮,可以发送mqtt 单击 双击 长按
#设置场景1+2按键按住5秒切换模式 ,3+2按键按住10秒重启面板
substitutions:
device_name: sonoff-m53c-02 #设备名称
friendly_name: sonoff_m53c_02
switch_name: sonoff_m53c_02
button1_pin: GPIO4 #按键1用引脚
button2_pin: GPIO0 #按键2用引脚
button3_pin: GPIO15 #按键3用引脚
relay1_pin: GPIO23 #驱动继电器1用引脚
relay2_pin: GPIO19 #驱动继电器2用引脚
relay3_pin: GPIO22 #驱动继电器3用引脚
led_state_pin: GPIO5 #蓝色指示灯引脚
pwm1_pin: GPIO18 #PWM输出
tx_pin: GPIO1 #UART TX引脚(用于外部传感器)
rx_pin: GPIO3 #UART RX引脚(用于外部传感器)
esphome:
name: $device_name
comment: 3位86型轻触开关。ESP主控、通过串口接入LD2410B雷达模块 #有关此节点的注释文本信息。仅用于在 UI 中显示。
platform: ESP32
board: esp32dev
preferences:
flash_write_interval: 10min #自定义数据刷新到闪存的频率
logger:
#esp32_ble_tracker:
#bluetooth_proxy:
api:
encryption: #启用API层的传输加密
key: !secret api_key #用于加密的预共享密钥
reboot_timeout: 0s #与客户端(如HA)失去连接多久后重启设备,秒s/分钟min/小时h,不需要此功能设置为0s
ota:
password: !secret ap_password
wifi:
networks:
- ssid: !secret wifi_ssid
password: !secret wifi_password
- ssid: !secret wifi_ssid2
password: !secret wifi_password2
- ssid: !secret wifi_ssid3
password: !secret wifi_password3
- ssid: !secret wifi_ssid4
password: !secret wifi_password4
# fast_connect: true #快速连接,如果SSID是隐藏的,必须设置为true
reboot_timeout: 0s #WiFi离线多久后重启设备,秒s/分钟min/小时h,不需要此功能设置为0s
# power_save_mode: LIGHT #WiFi接口的省电模式,可选NONE(ESP8266默认)、LIGHT(ESP32默认)、HIGH(最省电)
# output_power: 20dB #WiFi 接口的 TX 功率量,从 8.5dB 到 20.5dB
# use_address: sonoff-m53c-02.local #更新设备名称时,需要指定设备旧地址。
# manual_ip:
# static_ip: 192.168.99.106
# gateway: 192.168.99.1
# subnet: 255.255.255.0
# dns1: 114.114.114.114
# dns2: 180.76.76.76
# ap:
# ssid: "${friendly_name}"
# password: !secret ap_password
captive_portal:
#web_server:
# port: 80
# auth:
# username: !secret web_name
# password: !secret web_password
#mqtt:
# broker: 192.168.66.11 #MQTT主机地址
# port: 1883 #连接端口。默认为1883
# username: linhuajin
# password: devmgmt12001
# discovery: true #启用Home Assistant自动发现。默认为true
# reboot_timeout: 0s #与MQTT服务器失去连接时重新启动前等待的时间,秒s/分钟min/小时h,不需要此功能设置为0s
#button: #按钮开关
# - platform: restart
# name: "${friendly_name}_restart" #重启按钮
# # inverted: true
# id: "${friendly_name}_Restart"
uart:
id: ld2410_uart
tx_pin: GPIO1
rx_pin: GPIO3
# baud_rate: 9600
baud_rate: 256000
parity: NONE
stop_bits: 1
ld2410:
uart_id: ld2410_uart #指定ld2410B使用的串口
globals: #全局变量
- id: Keylock_gl
type: int
restore_value: yes #启动时还原状态
initial_value: "0" #在无法还原状态或未启用状态还原时用于初始化此变量的值
- id: auto_gl
type: int
restore_value: yes #启动时还原状态
initial_value: "1" #在无法还原状态或未启用状态还原时用于初始化此变量的值
switch:
- platform: restart #重启开关
name: "${friendly_name}_Restart"
icon: mdi:car-esp #自定义图标
id: "${friendly_name}_Restart"
- platform: safe_mode #安全模式开关
name: "${friendly_name}_SafeMode"
- platform: ld2410
# engineering_mode:
# name: "${friendly_name}_engineering mode" #启用/禁用工程模式
bluetooth:
name: "${friendly_name}_control bluetooth" #打开/关闭蓝牙适配器
- platform: gpio
restore_mode: RESTORE_DEFAULT_ON
pin: GPIO26
name: "${friendly_name}_Profile" #情景模式切换开关
id: ${friendly_name}_Profile
on_turn_on: #打开时执行自动化
then:
# - switch.turn_off: "${friendly_name}_relay1"
# - switch.turn_off: "${friendly_name}_relay2"
# - switch.turn_off: "${friendly_name}_relay3"
- switch.turn_on: "${friendly_name}_led"
on_turn_off: #关闭时执行自动化
then:
- switch.turn_off: "${friendly_name}_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: template #模板开关1
name: "${friendly_name}_single1"
id: "${friendly_name}_single1"
# restore_state: yes #是否应尝试在启动时还原状态,并使用恢复的值调用打开/关闭操作。默认值为 。no
optimistic: true #乐观模式,发送到模板封面的任何命令都将立即更新报告的状态,无需使用 lambda。默认值为 。false
- platform: template #模板开关2
name: "${friendly_name}_single2"
id: "${friendly_name}_single2"
# restore_state: yes #是否应尝试在启动时还原状态,并使用恢复的值调用打开/关闭操作。默认值为 。no
optimistic: true #乐观模式,发送到模板封面的任何命令都将立即更新报告的状态,无需使用 lambda。默认值为 。false
- platform: template #模板开关3
name: "${friendly_name}_single3"
id: "${friendly_name}_single3"
# restore_state: yes #是否应尝试在启动时还原状态,并使用恢复的值调用打开/关闭操作。默认值为 。no
optimistic: true #乐观模式,发送到模板封面的任何命令都将立即更新报告的状态,无需使用 lambda。默认值为 。false
- platform: template #模板开关4
name: "${friendly_name}_double1"
id: "${friendly_name}_double1"
# restore_state: yes #是否应尝试在启动时还原状态,并使用恢复的值调用打开/关闭操作。默认值为 。no
optimistic: true #乐观模式,发送到模板封面的任何命令都将立即更新报告的状态,无需使用 lambda。默认值为 。false
- platform: template #模板开关5
name: "${friendly_name}_double2"
id: "${friendly_name}_double2"
# restore_state: yes #是否应尝试在启动时还原状态,并使用恢复的值调用打开/关闭操作。默认值为 。no
optimistic: true #乐观模式,发送到模板封面的任何命令都将立即更新报告的状态,无需使用 lambda。默认值为 。false
- platform: template #模板开关6
name: "${friendly_name}_double3"
id: "${friendly_name}_double3"
# restore_state: yes #是否应尝试在启动时还原状态,并使用恢复的值调用打开/关闭操作。默认值为 。no
optimistic: true #乐观模式,发送到模板封面的任何命令都将立即更新报告的状态,无需使用 lambda。默认值为 。false
- platform: template #模板开关7
name: "${friendly_name}_Long1"
id: "${friendly_name}_Long1"
# restore_state: yes #是否应尝试在启动时还原状态,并使用恢复的值调用打开/关闭操作。默认值为 。no
optimistic: true #乐观模式,发送到模板封面的任何命令都将立即更新报告的状态,无需使用 lambda。默认值为 。false
- platform: template #模板开关8
name: "${friendly_name}_Long2"
id: "${friendly_name}_Long2"
# restore_state: yes #是否应尝试在启动时还原状态,并使用恢复的值调用打开/关闭操作。默认值为 。no
optimistic: true #乐观模式,发送到模板封面的任何命令都将立即更新报告的状态,无需使用 lambda。默认值为 。false
- platform: template #模板开关9
name: "${friendly_name}_Long3"
id: "${friendly_name}_Long3"
# restore_state: yes #是否应尝试在启动时还原状态,并使用恢复的值调用打开/关闭操作。默认值为 。no
optimistic: true #乐观模式,发送到模板封面的任何命令都将立即更新报告的状态,无需使用 lambda。默认值为 。false
- platform: template
name: "${friendly_name}_Keylock"
id: "${friendly_name}_Keylock"
icon: mdi:gesture-tap-button #自定义图标
turn_on_action:
then:
- lambda: |-
id(Keylock_gl)=1;
turn_off_action:
then:
- lambda: |-
id(Keylock_gl)=0;
lambda: |-
return id(Keylock_gl);
- platform: template
name: "${friendly_name}_auto"
id: "${friendly_name}_auto"
icon: mdi:focus-auto #自定义图标
turn_on_action:
then:
- lambda: |-
id(auto_gl)=1;
turn_off_action:
then:
- lambda: |-
id(auto_gl)=0;
lambda: |-
return id(auto_gl);
######## 这里是继电器配置 #################################
- platform: gpio
pin: GPIO23
name: "${friendly_name}_relay1"
id: "${friendly_name}_relay1"
on_turn_on:
then:
#- switch.turn_off: "${friendly_name}_Profile"
#- switch.turn_on: "${friendly_name}_LED1"
on_turn_off:
then:
#- switch.turn_off: "${friendly_name}_LED1"
- platform: gpio
pin: GPIO19
name: "${friendly_name}_relay2"
id: "${friendly_name}_relay2"
on_turn_on:
then:
#- switch.turn_on: "${friendly_name}_LED2"
#- switch.turn_off: "${friendly_name}_Profile"
on_turn_off:
then:
#- switch.turn_off: "${friendly_name}_LED2"
- platform: gpio
pin: GPIO22
name: "${friendly_name}_relay3"
id: "${friendly_name}_relay3"
on_turn_on:
then:
#- switch.turn_on: "${friendly_name}_LED3"
#- switch.turn_off: "${friendly_name}_Profile"
on_turn_off:
then:
#- switch.turn_off: "${friendly_name}_LED3"
########这里是LED配置 ##################################
- platform: gpio
pin: GPIO05
inverted: true
name: "${friendly_name}_led"
id: "${friendly_name}_led"
on_turn_on:
then:
# - switch.turn_on: "${friendly_name}_LED4"
#- switch.turn_off: "${friendly_name}_Profile"
on_turn_off:
then:
#- switch.turn_off: "${friendly_name}_LED4"
output:
- platform: ledc
pin: GPIO18
id: gpio_18
# Example usage in a light
light:
- platform: monochromatic
output: gpio_18
name: "${friendly_name}_ledc_Light"
######## 这里是按键配置 ##################################
##########按键1#####################
binary_sensor:
- platform: status
name: "${friendly_name}_status"
id: "${friendly_name}_status"
- platform: gpio
pin:
number: GPIO04
mode: INPUT_PULLUP
inverted: true
id: "${friendly_name}_binary1"
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:
- switch.toggle: "${friendly_name}_double1" #切换开关状态
# - if:
# condition: #检查条件
# switch.is_on: "${friendly_name}_Profile" #如果情景模式已打开
# 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: "${friendly_name}_Profile"
then:
- switch.toggle: "${friendly_name}_single1"
# - mqtt.publish:
# topic: "/esp/${switch_name}/h1"
# payload: "single"
else:
- switch.toggle: "${friendly_name}_relay1"
- timing: #长按
- ON for at least 0.7s
then:
- switch.toggle: "${friendly_name}_Long1"
# - if:
# condition: #检查条件
# switch.is_on: "${friendly_name}_Profile" #如果情景模式已打开
# 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: "${friendly_name}_binary1"
# 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 5s #按下时间5S
then:
- if:
condition:
binary_sensor.is_on: "${friendly_name}_binary2" #开关2也是按下状态
then: #就执行
- switch.toggle: "${friendly_name}_Profile"
# on_press:
# then:
# - if:
# condition:
# switch.is_on: "${friendly_name}_Profile"
# then:
#- switch.turn_on: LED_01
# on_release:
# then:
# - if:
# condition:
# switch.is_on: "${friendly_name}_Profile"
# then:
#- switch.turn_off: LED_01
##########按键2#####################
- platform: gpio
pin:
number: GPIO0
mode: INPUT_PULLUP
inverted: true
id: "${friendly_name}_binary2"
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:
- switch.toggle: "${friendly_name}_double2" #切换开关状态
# - if:
# condition:
# switch.is_on: "${friendly_name}_Profile"
# 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: "${friendly_name}_Profile"
then:
- switch.toggle: "${friendly_name}_single2"
# - mqtt.publish:
# topic: "/esp/${switch_name}/h2"
# payload: "single"
else:
- switch.toggle: "${friendly_name}_relay2"
- timing: #长按
- ON for at least 0.7s
then:
- switch.toggle: "${friendly_name}_Long2"
# - if:
# condition:
# switch.is_on: "${friendly_name}_Profile"
# 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: "${friendly_name}_binary2"
# 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: "${friendly_name}_Profile"
# then:
#- switch.turn_on: LED_02
# on_release:
# then:
# - if:
# condition:
# switch.is_on: "${friendly_name}_Profile"
# then:
#- switch.turn_off: LED_02
##########按键3#####################
- platform: gpio
pin:
number: GPIO15
mode: INPUT_PULLUP
inverted: true
id: "${friendly_name}_binary3"
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:
- switch.toggle: "${friendly_name}_double3" #切换开关状态
# - if:
# condition:
# switch.is_on: "${friendly_name}_Profile"
# 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: "${friendly_name}_Profile" #如果情景模式已打开
then: #就执行
- switch.toggle: "${friendly_name}_single3"
# - mqtt.publish:
# topic: "/esp/${switch_name}/h3"
# payload: "single"
else:
- switch.toggle: "${friendly_name}_relay3"
- timing:
- ON for at least 0.7s
then:
- switch.toggle: "${friendly_name}_Long3"
# - if:
# condition:
# switch.is_on: "${friendly_name}_Profile"
# 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: "${friendly_name}_binary3"
# 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: #长按10秒
- ON for at least 10s
then:
- if:
condition:
binary_sensor.is_on: "${friendly_name}_binary2"
then:
# - button.press: "${friendly_name}_Restart" #重启设备
- switch.turn_on: "${friendly_name}_Restart" #重启设备
# on_press:
# then:
# - if:
# condition:
# switch.is_on: "${friendly_name}_Profile"
# then:
# - switch.turn_on: "${friendly_name}_LED3"
# on_release:
# then:
# - if:
# condition:
# switch.is_on: "${friendly_name}_Profile"
# then:
# - switch.turn_off: "${friendly_name}_LED3"
- platform: ld2410
has_target:
name: ${friendly_name}_Presence #在场检测
icon: mdi:radar #自定义图标
on_press:
then:
- if:
condition:
lambda: 'return id(${friendly_name}_auto).state == 1 ;'
then:
- switch.turn_on: ${friendly_name}_relay1
on_release:
then:
- if:
condition:
lambda: 'return id(${friendly_name}_auto).state == 1 ;'
then:
- switch.turn_off: ${friendly_name}_relay1
# has_moving_target:
# name: ${friendly_name}_Moving_Target #移动目标
# has_still_target:
# name: ${friendly_name}_Still_Target #静态目标
select:
- platform: ld2410
distance_resolution:
name: "${friendly_name}_distance resolution" #控制门距离分辨率
# baud_rate:
# name: "${friendly_name}_baud rate" #控制串口波特率
# light_function:
# name: ${friendly_name}_light function
# out_pin_level:
# name: ${friendly_name}_out pin level
number: #控制LD2410B传感器的配置
- platform: ld2410
timeout:
name: ${friendly_name}_timeout #离开后保持当前状态的时间
# light_threshold:
# name: ${friendly_name}_light threshold #设置光照函数的光照阈值
max_move_distance_gate:
name: ${friendly_name}_max move distance gate #移动检测的最大距离门
max_still_distance_gate:
name: ${friendly_name}_max still distance gate #静止检测的最大距离门
# g0:
# move_threshold:
# name: ${friendly_name}_g0 move threshold #运动检测门的阈值
# still_threshold:
# name: ${friendly_name}_g0 still threshold #静止检测的门阈值
# g1:
# move_threshold:
# name: ${friendly_name}_g1 move threshold
# still_threshold:
# name: ${friendly_name}_g1 still threshold
# g2:
# move_threshold:
# name: ${friendly_name}_g2 move threshold
# still_threshold:
# name: ${friendly_name}_g2 still threshold
# g3:
# move_threshold:
# name: ${friendly_name}_g3 move threshold
# still_threshold:
# name: ${friendly_name}_g3 still threshold
# g4:
# move_threshold:
# name: ${friendly_name}_g4 move threshold
# still_threshold:
# name: ${friendly_name}_g4 still threshold
# g5:
# move_threshold:
# name: ${friendly_name}_g5 move threshold
# still_threshold:
# name: ${friendly_name}_g5 still threshold
# g6:
# move_threshold:
# name: ${friendly_name}_g6 move threshold
# still_threshold:
# name: ${friendly_name}_g6 still threshold
# g7:
# move_threshold:
# name: ${friendly_name}_g7 move threshold
# still_threshold:
# name: ${friendly_name}_g7 still threshold
# g8:
# move_threshold:
# name: ${friendly_name}_g8 move threshold
# still_threshold:
# name: ${friendly_name}_g8 still threshold
time:
# - platform: homeassistant
# id: homeassistant_time
# Enable time component to reset energy at midnight
# - platform: sntp
# id: my_time
sensor:
- platform: uptime
id: ${friendly_name}_raw_up_time
update_interval: 15min
internal: true
on_raw_value:
then:
- text_sensor.template.publish:
id: ${friendly_name}_uptime_human
state: !lambda |-
int seconds = round(id(${friendly_name}_raw_up_time).raw_state);
int days = seconds / (24 * 3600);
seconds = seconds % (24 * 3600);
int hours = seconds / 3600;
seconds = seconds % 3600;
int minutes = seconds / 60;
seconds = seconds % 60;
return (
(days ? String(days) + "d " : "") +
(hours ? String(hours) + "h " : "") +
(minutes ? String(minutes) + "m " : "") +
(String(seconds) + "s")
).c_str();
- platform: wifi_signal #WiFi信号强度
name: "${friendly_name}_WiFi_Signal"
icon: mdi:signal #自定义图标
update_interval: 120s
# - platform: ld2410
# moving_distance:
# name : ${friendly_name}_Moving_Distance #检测到的移动目标的距离
# still_distance:
# name: ${friendly_name}_Still_Distance #检测到的静止目标的距离
# moving_energy:
# name: ${friendly_name}_Move_Energy #移动目标的能量
# still_energy:
# name: ${friendly_name}_Still_Energy #静止目标的能量
# detection_distance:
# name: ${friendly_name}_Detection_Distance #目标的距离
text_sensor:
- platform: template
name: '${friendly_name}_Uptime'
id: ${friendly_name}_uptime_human
icon: mdi:clock-start #自定义图标
- platform: wifi_info
ip_address:
name: ${friendly_name}_IP #IP地址
icon: mdi:ip-network #自定义图标
ssid:
name: ${friendly_name}_SSID
icon: mdi:wifi #自定义图标
mac_address:
name: ${friendly_name}_mac
icon: "mdi:wifi-marker" #自定义图标
|