If you run home assistant in docker and are not using network mode host, then you can’t send magic wakeonlan packets out from homeassistant inside the docker. My solution is to create a user on your host server called “hawol” and it only has ssh access to a single command to send magic packet.
Solution as follows:
Check the wakeonlan command works on your server (it does in ubuntu 18.04)
$ wakeonlan ma:ca:dd:re:ss
If wakeonlan is an invalid command then you can install etherwake instead
Create a user on your server for the wakeonlan
$ sudo adduser hawol
Change your docker-compose or docker-run for home assistant and add the following volume mapping so that the ssh key is persistant when home assistant is upgraded (similar to the /config directory).
# Docker-compose
volumes:
- /root/.ssh:/some_direcotry
Now get a terminal inside the homeassistant docker
$ docker exec -it homeassistant bash
(or use portainer, click homeassistant container, then click the >_ console button )
Now create a directory in the home assistant docker container to store the ssh key
# mkdir /config/.ssh/
Now create an ssh key inside the homeassistant docker container (more info)
# ssh-keygen -t rsa -b 4096 -C "[email protected]"
Save the passkey in the default directory (press enter)
Choose the passphrase as nothing (press enter twice)
Now copy to ssh key from inside the docker container on to the server
$ ssh-copy-id [email protected]
Now verify you can ssh into your server from inside homeassistant docker without a password by typing
# ssh [email protected]
Now as the root user on the main server edit the file \home\hawol\.ssh\authorized_keys
It will be a one line file and look like this ssh-rsa AAA...
one very long key
Change the start of the single line in the file by adding the following bit at the start
command="/usr/bin/wakeonlan ma:ca:dd:re:ss" ssh-rsa AAAQIUHINEFAS...
Now get back inside the home assistant container check that it works by doing the following
root@server:~# docker exec -it homeassistant bash
bash-5.0# ssh [email protected]
Sending magic packet to 255.255.255.255:9 with ma:ca:dd:re:ss
Connection to server.ip.address closed.
bash-5.0# exit
exit
root@server:~#
You can use the following yaml
switch:
- platform: command_line
switches:
desktop_pc:
friendly_name: Desktop PC
command_off: "/usr/bin/curl http://desktop.ip.address:7760/suspend"
command_on: "/usr/bin/ssh [email protected]"
value_template: "{{ states('binary_sensor.desktop_pc') }}"
binary_sensor:
- platform: ping
name: desktop_pc
host: desktop.pc.ip.address
scan_interval: 2
count: 2
homeassistant:
customize:
switch.desktop_pc:
assumed_state: false
The program I run on my Windows 10 Desktop is http://www.ireksoftware.com/SleepOnLan/
It can run in the system tray, it listens on port 7760 for an http command (suspend, hibernate, logoff, poweroff, forcepoweroff, lock, reboot, test)