废话不多说,直接上配置,可实现自复位开关每个按键的单击、双击和长按三种控制方式。

需要注意的是,由于hassmart开关模块兼容传统开关,所以自复位开关长按会执行两次单击操作,可通过修改硬件飞线废弃传统开关支持,实现长按功能,具体方法可见文末。

esphome:
  name: hassmart_2key
  platform: ESP8266
  board: esp01_1m
wifi:
  ssid: "**"
  password: "****"
logger:
api:
ota:
switch:
  - platform: gpio
    name: 'hassmartch_2key_switch_1'
    id: relay_1
    pin: GPIO12
  - platform: gpio
    name: 'hassmartch_2key_switch_2'
    pin: GPIO5
    id: relay_2
binary_sensor:
  - platform: gpio
    id: button_1
    name: "自复位开关1"
    pin:
      number: GPIO0
      inverted: True
      mode: INPUT_PULLUP
    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:
        - logger.log: "双击"
    - timing:
        - ON for 0.5s to 2s
        - OFF for at least 0.5s
      then:
        - logger.log: "长按"
    - timing:
        - ON for at most 0.5s
        - OFF for at least 0.5s
      then:
        - logger.log: "单击"
        - switch.toggle: relay_1
  - platform: gpio
    id: button_2
    name: "自复位开关2"
    pin:
      number: GPIO14
      inverted: True
      mode: INPUT_PULLUP
    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:
        - logger.log: "双击"
    - timing:
        - ON for 0.5s to 2s
        - OFF for at least 0.5s
      then:
        - logger.log: "长按"
    - timing:
        - ON for at most 0.5s
        - OFF for at least 0.5s
      then:
        - logger.log: "单击"
        - switch.toggle: relay_2

最后修改:2020 年 12 月 25 日 05 : 26 PM
如果觉得我的文章对你有用,请随意赞赏