[Solved] Wake On LAN packet from ESP32 to HA server; how to automate?

I included a wake_on_lan button and the web server component in one of my ESP32 devices. So, presumably, the next time the power goes out at the house and I’m not home I can VPN into my network and restart my HA server via that ESP32. …my UPS needs some new batteries.

I’m trying to figure out a way to automate this so that if the ESP32 status binary sensor is off for 2 minutes the ESP32 will automatically send the WOL packet. Can someone help me out, please? I’ve tried on_... and I’ve tried Lambdas, and I’ve come up short. Perhaps it would be better to use api.connected?


web_server:
  port: 80
  version: 2
  ota: false
  local: true

button:
  - platform: wake_on_lan
    name: "Start the Dell Server"
    id: start_the_dell_server
    target_mac_address: XX:XX:XX:XX:XX:XX

binary_sensor:
  - platform: status
    name: "Guest Bedroom ESP32 Status"
    id: guest_bedroom_esp32_status

1 Like

The below yaml is working. I changed it to 5 minutes.

Maybe someone else will benefit from this. To be clear, this is the ESPHome yaml for an ESP32. Now, when the status changes the ESP32 checks again 5 minutes later, and if the ESP32 is still not connected to HA it sends a WOL to my server.


binary_sensor:
  - platform: status
    name: "Guest Bedroom ESP32 Status"
    id: guest_bedroom_esp32_status
    internal: true
    on_state:
      then:
        - delay: 5min
        - lambda: !lambda |-
            if (id(guest_bedroom_esp32_status).state) {
              ;
            } else {
              id(start_the_dell_server).press();
            }

web_server:
  port: 80
  version: 2
  ota: false
  local: true
  include_internal: true

button:
  - platform: wake_on_lan
    name: "Start the Dell Server"
    id: start_the_dell_server
    target_mac_address: "XX:XX:XX:XX:XX:XX"
    internal: true

Edit: I realized that the ESP32 will likely boot up with a “false” status and thereby never experience the state change needed to trigger the WOL. So I’ve added this to the top of the yaml file:


esphome:
  name: esp32-guest-bedroom
  # ...
  on_boot:
    priority: 600
    # ...
    then:
      - delay: 5min
      - lambda: !lambda |-
          if (id(guest_bedroom_esp32_status).state) {
            ;
          } else {
            id(start_the_dell_server).press();
          }

Edit again: I replaced the on_state automation entirely with the on_boot automation, because I’m really only concerned with the ESP32 sending WOL when it powers on. But if I were to want to keep the on_state automation and incorporate a way to trigger a state change when the ESP32 powers on, I could add publish_initial_state: true to the status sensor. Just an FYI for anyone interested.

3 Likes

Thanks for sharing this. It’s exactly what I was looking for to automatically reboot my HA server after a graceful shutdown due to low UPS battery.