Power on and off Diskstation with Home Assistant
I use my Synology Diskstation mainly as a backup device. Therefore it does not need to run all the time. In the past I configured a "Power schedule" directly within the Diskstation. It would turn on at a certain time and power off every night. Now I wanted a smarter behaviour e.g. I would like to automatically turn on the Diskstation when I come home, so my Smartphone can backup data. Another example would be to power on the diskstation to store a Home Assistant Backup and afterwards power it off again. Also it would be nice if I could turn it off with my "good night"-Switch next to my bed. I achieved this by controlling the Diskstation via Home Assistant. This article describes the configuration steps, necessary for this.
Turn on Diskstation with Wake On Lan
We can use Wake on Lan to turn of the diskstation by a network message.
First we need to enable Wake on Lan on our diskstation:
- Login into your Diskstation
- Go to Control Panel > Hardware & Power > General
Enable WOL on Lan
After that you are able to start your Diskstation over the network. e.g. on a Mac you could run following command in the terminal:
# Install wakeonlan command via brew brew install wakeonlan # Wake up diskstation by using its MAC Address wakeonlan XX:XX:XX:XX:XX:XX
Note: For sending a magic package via wakeonlan you can specify an ip:
-i ip_address set the destination IP address default: 255.255.255.255 (the limited broadcast address)
This is NOT the ip-address of your device, this is the broadcast address. Your device is identified by the mac address.
Home Assistant Configuration
Now we need to send this magic package from Home Assistant. This can be done with the Wake on LAN integration. It allows to add a switch which sends the packages when it is turned on. We just need to supply the mac address of our diskstation in the configuration. My configuration looks like this:
switch: - platform: wake_on_lan mac: XX:XX:XX:XX:XX:XX host: "192.168.179.30" name: "Diskstation"
host parameter contains the ip of the diskstation, so it can check if the diskstation is online or not. For more details see the integration documentation.
Turn off Diskstation via ssh
To turn off the diskstation remotely we need to login via ssh and call the poweroff command. I used following steps:
1. Create HomeAssistant User on diskstation
First we need to create a new user on our diskstation:
Create a user which can be used from home assistant to login to your diskstation
- Login to Diskstation
- Control Panel -> User -> Create -> Enter password, username, ...
- Make sure to give the user adminstrator rights otherwise he cannot execute the poweroff command
2. Require no password for
To shutdown the diskstation we use the command
sudo poweroff. If you ssh with the new user to your diskstation and run it you would be asked to enter your password before the command is executed. When we run this command later using a script we cannot enter the password. Therefore we add following line to the end of
home_assistant_ssh ALL=NOPASSWD: /sbin/poweroff
This will allow the user
home_assistant_ssh to execute the
poweroff command without entering a password.
3. Create keys on your Home Assistant device (e.g. Raspi)
Now we want to ssh into the dikstation from Home Assistant.
Login to Home Assistant via ssh
- If not already done configure SSH-Server Addon
Create keys in
ssh-keygen -t rsa -b 4096 -C "home_assistant@raspi"
- do NOT set a password
- make sure your private (id_rsa) and public key (id_rsa_pub) are generated in
Copy public key to diskstation
ssh-copy-id -i .ssh/id_rsa.pub email@example.com
- this adds your public key to the authorized_keys file on your dikstation
Next we need to set the correct permission for the folder & files on the diskstation
ssh USERNAME@DISKSTATION_IP- enter password
chmod 0711 ~
chmod 0711 ~/.ssh
chmod 0600 ~/.ssh/authorized_keys
- Now you should be able to login to the disktation from the Home Assistant shell without entering a password e.g.
4. Copy ssh keys to configuration
Our keys created in 3 are not accessible by other integration e.g. the shell_command integration we will use later. Therefore we need to copy the ssh keys to the configuration folder.
cp ~/.ssh/id_rsa /config/.ssh cp ~/.ssh/id_rsa.pub /config/.ssh
Make sure that you keep your private key (id_rsa) safe. e.g. take care when publishing your configuration online.
5. Call power off command from Home Assistant
After all the preperations in the previous steps we are now ready to shutdown the diskstation from home assisant by executing following command:
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa firstname.lastname@example.org sudo poweroff
To call this command from inside HomeAssistant we extend the wake_on_lan configuration by the
switch: - platform: wake_on_lan mac: ... host: "192.168.179.30" name: "Diskstation" turn_off: service: shell_command.power_off_diskstation shell_command: power_off_diskstation: 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa email@example.com sudo poweroff'
The switch will now ssh into the diskstation and call poweroff when it is turned off.
And that's it. 😀
The switch now controls your diskstation. Of course you can use it as any other switch in the Lovelace UI. If you want to trigger it from code you can call the service
homeassistant.turn_off with the entity, in my case