Esphome switch (Relay) trigger 2 times (double click)

I have a simple task emulate double click with esphome, I want it to be done on the esp device and not from Home Assistant.
Doing one click is easy

switch:
  - platform: gpio
    pin: GPIO0
    id: lock
    name: "Front Door Close"
    icon: "mdi:door"
    restore_mode: ALWAYS_OFF
    on_turn_on:
    - delay: 200ms
    - switch.turn_off: lock

But since it’s triggered by “on_turn_on:” I cant just add delay and trigger the switch once more, such construction will be looped.

Please suggest best practices to achieve double click emulation.
Thanks!

I have not tried this, so it’s just a suggestion on a possible path to follow, not a complete solution.

My first thought on this is to create a template switch: https://esphome.io/components/switch/template.html

Code that I have not tried to compile, but is included just to let you know what I’m thinking (getting it to work is left as an exercise to the reader! :wink: )

switch:
  - platform: gpio
    // No name or icon is defined, so this physical switch output will not show up in HA
    // This switch just exists to control the physical GPIO pin, by using its id value
    pin: GPIO0
    id: lock_pin
    restore_mode: ALWAYS_OFF

  - platform: template
    // A virtual switch that appears in HA. Turning this switch on will trigger the on_turn_on action,
    // which will generate two pulses of the physical GPIO pin by calling the above switch, and then
    // will turn itself off. Since there is no lambda expression, the returned on/off status should be
    // the assumed status (I think?) which is ON while the output is being pulsed, and OFF when
    // the pulsing is done.
    name: "Front Door Close"
    id: lock
    icon: "mdi:door"
    restore_mode: ALWAYS_OFF
    on_turn_on:
    - switch.turn_on: lock_pin
    - delay: 200ms
    - switch.turn_off: lock_pin
    - delay: 200ms
    - switch.turn_on: lock_pin
    - delay: 200ms
    - switch.turn_off: lock_pin
    - switch.turn_off: lock

The general idea is to hide the low level switch that runs the pin directly, and create a higher level virtual switch that triggers the low level switch when it is triggered.

You will undoubtedly have to change something in this code to get it to work, but hopefully it’s a start.

There is probably a more efficient way to do this, and if so, I’m sure someone will be along to mention it.

Thanks for helping.
In case anyone will need such feature in future here is the working code example.

substitutions:
  location: front_door_lock_switch
  
esphome:
  name: front-door-lock-switch
  platform: ESP8266
  board: esp01_1m
  on_boot:
   then:
    - switch.turn_off: lock_pin
    - switch.turn_off: lock_open

  
# Enable logging
logger:
packages:
  #WiFi and API settings
  network: !include common/network.yaml
  #system info sensors
  system_sensors: !include common/system_sensors.yaml


  
switch:
  - platform: gpio
    pin: GPIO0
    id: lock_pin
    on_turn_on:
      - delay: 100ms
      - switch.turn_off: lock_pin
    internal: true

  - platform: template
    name: "Front Door Close"
    id: lock_open
    icon: "mdi:lock"
    turn_on_action:
    - switch.turn_on: lock_pin
    - switch.turn_off: lock_open
 
  - platform: template
    name: "Front Door Open"
    id: lock_close
    icon: "mdi:lock-open-variant"
    turn_on_action:
    - switch.turn_on: lock_pin
    - delay: 500ms
    - switch.turn_on: lock_pin
    - switch.turn_off: lock_close