Adding "Inching" to ex-Sonoff ESPHome smart plug

Hi all!

Recently I flashed one of my (many) Sonoff S26 smart plugs with ESPHome. Great fun - managed to do it by holding probes on the 4 contact points by hand :laughing:

One thing I missed from the Sonoff functionality was the idea of “inching” - where you can set a delay on the plug itself to turn off automatically. Yes, easy enough to do in HA itself, but having it built into the plug gives you confidence in case of network outage etc that it’ll definitely get turned off.

Anyway after some fiddling I have it working, but it’s not very pretty. Mostly because the “inching time” entity comes up in HA as “number.xxxx” not “smartplug.xxxx” so I had to use a vertical stack card to put the two together. Any ideas to improve this would be welcome!

Here’s the ESPHome code:

esphome:
  name: smartplug1
  platform: ESP8266
  board: esp01_1m
  board_flash_mode: dout


status_led:
  pin:
    number: GPIO13
    inverted: false

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxx"

ota:
  password: "xxxxxxxxxxx"

wifi:
  power_save_mode: light
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Smartplug1 Fallback Hotspot"
    password: "xxxxxxxxxx"

captive_portal:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "smartplug1 Button"
    on_press:
      - switch.toggle: relay
  - platform: status
    name: "smartplug1 Status"

sensor:
  - platform: wifi_signal
    name: "smartplug1 WiFi Signal"
    update_interval: 60s

number:
  - platform: template
    id: inching_on_time
    name: "Inching on time"
    optimistic: true
    restore_value: true
    initial_value: 60
    min_value: 1
    max_value: 120
    step: 1
    unit_of_measurement: minutes
    entity_category: config
    mode: slider

switch:
  - platform: gpio
    name: "smartplug1 Relay"
    pin: GPIO12
    id: "relay"
    on_turn_on:
      then:
        - switch.turn_on: relay
        - delay: !lambda 'return id(inching_on_time).state * 60000;'
        - switch.turn_off: relay
  - platform: restart
    name: "smartplug1 Restart"

And here’s the lovelace card:

type: vertical-stack
cards:
  - type: entities
    entities:
      - switch.smartplug1_relay
  - type: entities
    entities:
      - number.inching_on_time

image

2 Likes

This is, in my opinion, a significant missing feature in ESPHOME.
Shelly among other devices have a resetable auto-off timer for this function.
It’s an important feature as it provides a dependable control directly on the ESP device which isn’t subject to timers or callbacks from HA.

An example would be a trigger event for central heating - you don’t want the boiler overrunning or being left on overnight if there is no call for heat.

How would one request that type of a feature-add into ESPHOME? Is that done from within this community?

1 Like
1 Like

Many thanks for this.
I tested it out on my relay board and it’s working well. It would be nice to have a countdown timer visible, I wonder if this could be added?

working but web lacks a unit

image

This is best way:

switch:
  - platform: gpio
    id: switch_1
    name: Open Gate
    pin: P24
    on_turn_on:
    - delay: 500ms
    - switch.turn_off: switch_1
  - platform: gpio
    id: switch_2
    name: Close Gate
    pin: P6
    on_turn_on:
    - delay: 500ms
    - switch.turn_off: switch_2
  - platform: gpio
    id: switch_3
    name: Stop Gate
    pin: P26
    on_turn_on:
    - delay: 500ms
    - switch.turn_off: switch_3
  - platform: gpio
    id: switch_4
    name: Gate SbS
    pin: P14
    on_turn_on:
    - delay: 500ms
    - switch.turn_off: switch_4

Thanks a lot, that is what I am looking for to reboot my router in case of internet disconnection.