Power on / off NAS by using WOL and SSHPASS

Hello togehter,

I just switched from OpenHAB to HomeAssistant (HassIO on Raspberyy PI4 4GB).

Now I try to adapt my OpenHAB switches etc. to HomeAssistant.

One of the “problems” I’ve run into is a switch to power on / off my synology nas.

It seemed to be quite complicated due to the lack of permissions etc… Also everything i read on did not realy work for me. So I got a bit deeper (first time to use docker etc.)

And found the following Solution for my problem which I will show you below.

  1. Create a SWITCH for powering on / off.
switch:
  - platform: wake_on_lan
    name: YOURNAME
    mac: YOURMAC
    host: YOURIP
    turn_off:
      service: shell_command.turn_off_YOURNAME
shell_command:
    turn_off_YOURNAME: bash /path/to/script/SCRIPTNAME.sh
  1. Create the script “SCRIPTNAME.sh” for powering off:
#! /bin/bash
sshpass -p "REMOTEPASSWORD" ssh -t [email protected] "echo REMOTEPASSWORD | sudo -S poweroff"
  1. Install the “SSH & Web Terminal” Add-On and deactivate the “protection mode”.

In Terminal run:
docker ps -a

Look for the one with two times “homeassistant” in the name by “Image” and copy the “CONTAINER ID”

  1. Run from Terminal:
    sudo docker exec -it CONTAINERID /bin/bash

  2. Run from Terminal:
    apk add sshpass

  3. Connect to remote host to verify the fingerprint
    ssh -t [email protected]

type “yes” to confirm the fingerprint and add your password.

Quit by typing:
exit

  1. Optional: Turn on “protection mode” again.

You’re done now. The switch will power on your NAS by using WOL and power it off by using sshpass.

NOTE: You have to do step 4 to 6 after every Update!

Hope that will help someone else!

Greetings

6 Likes

I did not want to perform the “apk add sshpass” after every update, so I changed the connection to SSH with Keyfiles. For a HowTo to do so on synology nas, you can follow this one: https://chainsawonatireswing.com/2012/01/16/log-in-to-a-synology-diskstation-using-ssh-keys-as-a-user-other-than-root/

First of all, you have to add the SSH Keyfiles on the Hostsystem (the private key needs to be openssh format) and then

Put the Private Key FIle
(private_key)

to

/config/ssh_keys/
on Home Assistant

CHMOD it to 600

chmod 600 /config/ssh_keys/private_key
(use the Console Addon)

Create a SWITCH for powering on / off.

switch:
  - platform: wake_on_lan
    name: YOURNAME
    mac: YOURMAC
    host: YOURIP
    turn_off:
      service: shell_command.turn_off_YOURNAME
shell_command:
    turn_off_YOURNAME: bash /path/to/script/SCRIPTNAME.sh

Create the script “SCRIPTNAME.sh” for powering off:

ssh -i /config/ssh_keys/private_key -o StrictHostKeyChecking=no [email protected] "echo REMOTESERVERROOTPASSWORD | sudo -S poweroff"

SSHUSERNAME = Username to connect via SSH to the remote Host
HOSTIP = IP of the remote Host
REMOTESERVERROOTPASSWORD = RootPassword of the Remote Host to have enough rights to run the poweroff

DONE :wink:

3 Likes

Hi @Lucan,

Minor correction to your code above:

    turn_off:
      service: shell_command.turn_off_YOURNAME

should be

    turn_off:
      service: shell_command.turn_off_storage

matching the shell_command:

shell_command:
    turn_off_storage: bash /path/to/script/SCRIPTNAME.sh

pretty much sure it was a typo …

thanks for your code. I can now easily wake up my NAS and switch it off when needed through HA.

Forgot to rename the name i choose (storage) with “YOURNAME” in the tutorial above. I changed it now - thank you.

It is faster and easier creating a switch like this:

switch:
  - platform: wake_on_lan
    name: "NAME_NAS"
    mac: 00-AA-BB-CC-DD-EE
    host: 192.168.1.2
    turn_off:
      service: synology_dsm.shutdown

Of course, you need first to integrate the Synology NAS in Home Assistant: Menu: Configuration -> Integrations . Search for “Synology DSM”, fill in the configuration form with your username and password, and then click Submit .
MAC and host addresses above are examples, you will need yours.
Wake on lan service also needs to be in configuration.yaml of home assistant:

# Example configuration.yaml entry
wake_on_lan:

True, nice for synology - did not know that there is a service included for that.

But mine could be adapted for almost every linux computer - so both is usefull.

Is there also a way to turn on the synology?

The Switch turns it on with WOL - so yes

I tried doing this but to connect to my windows machine. I generated a key using “ssh-keygen” on windows and copied the contents of the id_rsa to a file in my config “/config/ssh_keys/id_rsa”, so my shell command is:

ssh -i /config/ssh_keys/id_rsa -o StrictHostKeyChecking=no [email protected]_of_windows "powercfg -h off & rundll32.exe powrprof.dll,SetSuspendState 0,1,0"

However it still asks for the password when I test it in the SSH terminal on the Home Assistant.

Any assistance on how to get this working would be greatly appreciated!
Cheers,
Josh

worked for my qnap thank you