Newbie help - override default ESPHome behaviour with delayed turn off

Hi, this is my first post here, and I’m looking for a kind soul to point me in the right direction. I am very new to home automation, and have watched lots of videos and read through lots of docs but still struggling moving from just app enabled switches to something more “intelligent”.

Please be gentle with me if I have not adhered to the rules here :blush:

I am running Home Assistant 2021.4.5 on a Pi4 and have Node Red, ESP Home and Mosquitto installed (plus a few other helpful bits like Samba share) but have no automations at all yet.

I am trying to override the behaviour of a Sonoff T1 light switch which is flashed with ESPHome. I can turn it on or off by touching the physical switch, or by pressing the bulb on the card in HA.
However, I would like make it so that a short press turns it on or off, but if it’s switched on, a long press (say 4 seconds) sets a timer, which switches the light off after another 20 seconds. I really don’t know where I should start. Would I do it in ESPHome, create an automation in Home Assistant, use Node Red ???

I could just program the ESP myself in Arduino, but I feel that I should be using Home Assistant and the various plugins to do this rather coding it myself. Any help offered would be very gratefully received (even just point me at the right doc) :slight_smile:
Many thanks.

Hi there you can do this in esphome itself, is the best method which makes sure that even if there is a connectivity issue or HA goes down, this function wont be affected. Just adapt the following code to your system.

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    id: button_1
    on_click:
      min_length: 50ms
      max_length: 350ms
      then:
        - delay: 20s
        - switch.turn_off: relay_1

You can find other binary sensor configuration in this link

1 Like

Thanks, that pointed me to exactly what I needed to read. :grinning:

The code I ended up with looked this this, so if the light is off, clicking the button will always turn it on, but I can use a short press to turn off immediately or a long press to give me 20s to leave the room.

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    id: button_1
    on_click:
    - min_length: 50ms
      max_length: 1s
      then:
        if:
          condition:
            light.is_on: light_1
          then:
            - light.turn_off: light_1
          else:
            - light.turn_on: light_1

    - min_length: 1s
      max_length: 4s
      then:
        if:
          condition:
            light.is_on: light_1
          then:
            - delay: 20s
            - light.turn_off: light_1
          else:
            - light.turn_on: light_1

  - platform: status
    name: "Test Status"

I think I might have to start moving my other devices to ESPHome now I realise how powerful it is.
Many thanks.

1 Like