Creating a esphome device with a button which will always work

So I have this Arduino Mega with MySensors 2.3.2.
In this code I have a built-in backdoor when a button is pressed the relay will always toggle for 5 seconds.

Now I would like to replace the Arduino Mega with a NodeMCU V3 or something familiar.
I would like to have a fully functional devices like before with the same button.
The new device must always toggle the relay! When it’s not cennected or when wifi is down or when HA is rebooting, etc.

This button is like a secret switch somewhere to open a lock. The ESPHome module is connected to a UPS so power is not an problem.

How to implement this kind of behaviour? Or is it not possible?

Yes totally possible. I use this to power cycle some outlets.

Here is the most relevant piece of the code

button:
  - platform: template
    name: ${device_name} Power Cycle
    id: cycle_button
    icon: "mdi:restart"
    on_press:
      - switch.turn_off: relay
      - delay: 10s
      - switch.turn_on: relay
1 Like

Don’t forget to add:

  reboot_timeout: 0s

to your wifi definition. This stops reboots when HA is down/unavailable.

2 Likes
button:
  - platform: template
    name: ${device_name} Power Cycle
    id: cycle_button
    icon: "mdi:restart"
    on_press:
      - switch.turn_off: relay
      - delay: 5s
      - switch.turn_on: relay
button:
  - platform: template
    name: ${device_name} Toggle Cycle
    id: cycle_button
    icon: "mdi:restart"
    on_press:
      - switch.toggle: relay
      - delay: 5s
      - switch.toggle: relay

:stuck_out_tongue_winking_eye:

and to your api: or mqtt: component in case you use them :warning:

ESPHome reboots on purpose when something is not going right, e.g. wifi connection cannot be made or api connection is lost or mqtt connection is lost. So if you are facing this problem you’ll need to explicitly set the reboot_timeout option to 0s on the components being used.

1 Like

A button that toggles a relay isn’t what you’d commonly call a “back door” unless it opens your secret “back door” porn stash. That’s just a basic function and these basic functions are explained in the documentation with several examples that cover exactly what you want. You would have found this if you had done a simple Google search and tried.