Sometimes there’s a need to remote start and stop a Linux PC through Home Assistant. I’m currently having a need to start and stop my headless VDR/streaming PC when needed. The PC is only required once or twice in a month so I don’t want to keep it up 24/7 just to consume electricity.. Since it’s not ‘that simple’ I decided to write a quick guide about it.
For the startup we are going to be using Wake-On-Lan (WOL). WoL is a ethernet computer networking standard that allows a computer to be turned on or awakened by a network message.
WoL needs to be enabled from computers BIOS settings. So, to get the startup working, find a manual of your remote PC motherboard and find a way to enable WoL or just enter BIOS and explore 🙂
That’s it.. we’ll get back to this when configuring the Home Assistant at the end of this article.
The shut down procedure is a bit more complicated than the start up. In this guide I’m using remote shell commands through SSH and password-less SSH connection is achieved by using public and private key pairs. Public and private key pairs can be though of as a key (local machine) and a lock (remote machine) that work together for security.
I’m assuming you’re even a bit familiar with linux shell and have already basic SSH access to your remote machine and home assistant.
You should also know the IP and MAC addresses of your Remote PC.
Configuring the remote PC
First we need to configure the remote linux pc to support shut down in a secure way. Normally shutting down a linux machine from command line requires a root user access and of obviously we don’t want to give our Home Assistant full root access in case of e.g. security breach.
So, let’s create a new user for our Home Assistant instance by SSH into remote machine and typing
sudo adduser homeassistant
Fill out the information requested including password, you’ll need it later.
Now that we have our HA user created we need to grant it an access to reboot the machine. That can be done by editing the sudoers file.
The sudoers file should be now open with visudo that verifies syntax of the file. You really don’t want to mess it up 🙂 Now lets add the access right for our newly created homeassistant user by adding line below user privilege specification
# User privilege specification homeassistant ALL=(ALL) NOPASSWD: /sbin/poweroff, /sbin/reboot, /sbin/shutdown
Remember not to delete anything from the file, just add the above line in it. Now save and exit and we’re good to move to the next section. Oh, the SSH connection to your remote machine can now be closed, no need to configure that part anymore.
Creating SSH key pair
Next we’ll going to SSH into Home Assistant (you need to have SSH add-on installed in home assistant).
Start by creating the RSA public and private key pairs by typing
mkdir /config/ssh_keys ssh-keygen -t rsa -f /config/ssh_keys/id_rsa_homeassistant
When a password is queried, leave it empty. If fulfilled with password the SSH connection will require both key and password and can’t be used for remote commands by home assistant.
In above lines we first create a directory for our ssh keys, because Home Assistant is run in a docker container and does not have visibility to default /root/.ssh folder at all. Second line starts the key generation.
Next we are going to copy the public key to our remote machine (just change the remote_hostname to proper IP address or hostname of the remote linux PC):
ssh-copy-id -i /config/ssh_keys/id_rsa_homeassistant.pub homeassistant@remote_hostname
Your homeassistant password to the remote machine will be queried at this point, so enter it.
That’s it! We now have key pairs created and transferred it to the remote machine. You can verify that it’s working by SSH’ing into remote machine from home assistant shell
ssh -i /config/ssh_keys/id_rsa_homeassistant homeassistant@remote_hostname
If everything is working as expected, no password will be queried. You can even then try to turn off the machine by typing ‘sudo poweroff -h now’. That should not query for password either and the machine should be turned off. Now lets move to configure the home assistant!
Configuring Home Assistant
Configuration is done through configuration.yaml and shell_commands.yaml files (using SSH or configuration editor add-on).
Open the shell_commands.yaml in editor, create if not existing, and add following line in it (change the remote_hostname to your remote PC IP address or hostname):
turn_off_remote_pc: "ssh -i /config/ssh_keys/id_rsa_homeassistant -o 'StrictHostKeyChecking=no' homeassistant@remote_hostname sudo shutdown -h now"
Let’s add support to shell commands in our configuration.yaml file
shell_command: !include shell_commands.yaml
Now that the shell_commands are in place, let’s create switch that turns on/off the machine (replace host and mac address of your remote machine):
switch: - platform: wake_on_lan name: RemotePC host: 192.168.1.100 mac: 12:34:56:78:90:ab turn_off: service: shell_command.turn_off_remote_pc
And it’s done! Now restart home assistant and add the RemotePC switch in Lovelace UI. The machine should start and turn off when requested.