[Tutorial] Wake up a client on another network via Home Assistant (RestAPI)

Started by balrog, Today at 11:51:09 AM

Previous topic - Next topic
Introduction

With ISC DHCP, it was possible to set up WoL over routed networks using a NAT workaround. However, since ISC DHCP is deprecated, and tools like Dnsmasq or KEA DHCP do not offer an option for "fake" ARP entries, and solutions such as defining the MAC address via Neighbors did not work for me (presumably due to CARP), this guide describes a solution for setting up WoL using OPNsense's built-in API. In this example, the "Magic Packet" is sent via Home Assistant.

OPNsense
1. Install the plugin
The os-wol plugin is required for this solution. Simply installing it is sufficient; no configuration is needed here.


2. Create a user
A user must be created for API access. You can choose the name yourself.
Under Privileges, select the service we just installed: Service: Wake on LAN
Then save.


3. Create API Key
We create the API key pair by clicking this icon. This downloads the key pair to a .txt file.


4. Extract Network Identifier
In the script later on, we'll specify which network the client to be woken up is on. To do this, we need to note down the identifier of the interface. In my case: lan


Home Assistant

I'm going to create a button in Home Assistant that I'll use to wake up the client. To do this, the following must be entered in the Home Assistant YAML configuration. I've moved "templates" to a separate file called template.yaml, which is why I have the following line in my configuration.yaml:
template: !include template.yaml

1. configuration.yaml
Insert the following block and adjust it according to the comments. For payload: make sure the identifier of the OPNsense interface is included:
rest_command:
  opnsense_wake_client:
    url: "https://x.x.x.x:xxx/api/wol/wol/set" # actual IP and Port of the OPNsense WebGUI
    method: POST
    username: "Kc/vYhlWU/K4fS3N3vhyKJ1234oxwg2adbDtVJZLZwL2k5QhhmxhLa/mxxI4LNhqKyZougxnSMnHWiA6" # key from the txt
    password: "Yz/7A1lTzSi+I4XXWMMrV1234FlwIwFZsowocpAH1r7dfBHRAGZLVk7aSBX2/n5tg4cwKaEmeV9GJIuS" # secret from the txt
    content_type: "application/json"
    payload: '{"wake":{"interface":"lan","mac":"AA:BB:CC:DD:EE:FF"}}' # interface=identifier, mac=MAC-Address of the client
    verify_ssl: false # when you use the default self-signed cert from OPNsense

2. template.yaml
Insert the following block into template.yaml:
- button:
  - name: "WoL Client"
    press:
      - action: rest_command.opnsense_wake_client


3. Restart Home Assistant
For me, simply loading the YAML config wasn't enough, so I had to restart Home Assistant


4. Add a button
Now you can create a button on the dashboard and select "WoL Client" as the entity.