Reboot script built in to ESPhome

I had searched for a bit to try and find a way to make a device always want to be on no matter what but would be easy to trigger an off cycle for rebooting a modem or router even if I am gone from home and using a VPN or something else to access my installation. Below is the full YAML that I used to do this and wanted to share it with anyone else who may have a need. Basically the device defaults to an on state so if there is a power outage it will turn back on, if the button is pressed it will turn off, wait 30 seconds and then turn back on and the same goes for a signal sent from Home Assistant.

I have been using this for a couple weeks on my router, modem, and access point without issue and it pretty well keeps them up and running. I will say that you may want to set a static IP address if your router and AP are separate devices as if the AP is working but the router is not fully booted you could end up with a boot loop otherwise set the reboot timer in ESPhome for a longer time so that all devices needed will be up and running. When I first put my router on this script the device would connect to wifi but not get an IP address then revert to it’s own AP mode then eventually reboot which was right about the time that the router was fully operational. Setting a static IP address fixed this as even if the router is not fully booted once it connects to the AP it has the ability to talk to my Home Assistant install which also has a static IP. This has worked excellent for my modem though as before I had to unplug it and then wait for a few and plug it back in. Now I can just trigger the switch in Home Assistant and walk away as it will wait 30 seconds and then power back up. This is the YAML for my Sonoff Basic but with a couple tweaks should work for just about anything.

esphome:
  name: device
  platform: ESP8266
  board: esp01_1m
  on_boot:
    priority: -10
    # turn power on at boot
    then:
      - switch.turn_on: relay
wifi:
  ssid: "WiFi-SSID"
  password: "supersecretpassword"

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

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:


binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "Device Button"
    on_press:
      - switch.turn_off: relay
      - delay: 30s
      - switch.turn_on: relay
switch:
  - platform: gpio
    name: "Device"
    pin: GPIO12
    id: relay
    icon: "mdi:device"
    on_turn_off:
    - switch.turn_off: relay
    - delay: 30s
    - switch.turn_on: relay
status_led:
  pin:
    number: GPIO13
    inverted: yes
1 Like