0Managing a Qnap NAS power on and off from home assistant

I have a QNAP NAS that I use for backups. It only needs to run once a week so the rest of the time it’s bet­ter for it to be off to save power (around 40–50W!). After some fid­dling I man­aged to achieve what I needed via home assist­ant. Veja como ...

Getting Qnap ready to talk to HA

Install Putty (inc putty­gen) and then use putty-gen to gen­er­ate a 2048-bit RSA pub­lic-private key pair
Copy the both to your home assist­ance config/.ssh folder (cre­ate it if it does­n’t exist)
Open the PUBLIC key in a text edit­or (por exemplo. Notepad++)
Now log in to the Qnap
Cre­ate a second admin user on the QNAP with a nice long com­plex pass­word. This account unfor­tu­nately does need to be an admin account not just a reg­u­lar user (in order to read the net­work band­width usage)
Click on your user­name at the top right
Select “login and secur­ity” from the drop-down menu
Select the “SSH Keys” tab
Click the blue link to “SSH Con­nec­tion” which will open the con­trol pan­el at the right place
Tick “allow SSH con­nec­tion” and if you wish to change the port (I usu­ally do) e clique em Aplicar
Close the con­trol pan­el and go back to the “login and secur­ity” window
Click the blue text “access permissions”
Tick the box next to the new user­name to allow it.
Log out of the qnap
Log into the Qnap as the new user
Click on your user­name at the top right
Select “login and secur­ity” from the drop-down menu
Select the “SSH Keys” tab
Click “add”
Copy and paste the whole con­tents of the PUBLIC key you opened earli­er and then click “add”

Initial HA setup

Now open your home assist­ant configuration.yaml and at the bot­tom add the fol­low­ing line
shell_command:
shutdown_qnap: !secret SSH_Qnap_Command

Now open secrets.yaml (cre­ate it if it does­n’t exist) and add the fol­low­ing, repla­cing the sec­tions in curly brack­ets with your ssh port (22 unless you changed it), qnap user­name that you cre­ated, o IP of the qnap, and the long ugly pass­word you cre­ated for the new qnap suer
SSH_Qnap_Command: ssh -i /config/.ssh/id_rsa -o 'StrictHostKeyChecking=no' -p {ssh port} {qnap username}@{qnap ip address} 'echo -e "{qnap password}" | sudo -S poweroff'
Now load home assistant
Go to set­tings -> dispositivos & ser­vices -> add integration
Search for QNAP
Fol­low the wiz­ard and enter the IP, user­name and pass­word needed to access your qnap (the new account you cre­ated on the qnap)
This should cre­ate a Qnap device with sev­er­al entit­ies. There may be a num­ber of dis­abled entities.
Check if the eth0 (and/or eth1 etc) entit­ies are enabled or dis­abled. If they are dis­abled enable the ones you need and make sure after a minute or so they pop­u­late cor­rectly. Also check what units they are using (por exemplo. mb/sec or kb/sec)

Creating an HA automation to put inactive qnap to sleep

In HA go to ‘set­tings’ -> ‘auto­ma­tions & scenes’ and click “cre­ate automation”
Cre­ate from blank (don’t use a template)
Once in the auto­ma­tion, Clique no 3 ver­tic­al dots at the top right and click “edit in yaml”
Paste the fol­low­ing code, edit­ing to suit where needed. Change ‘sensor.garage_qnap_eth0_download’ to the eth­er­net usage entity you need. I have used activ­ity below 0.5mb/sec as an indic­a­tion that the device is no longer run­ning any act­ive tasks. Note the action matches the name we added earli­er to configuration.yaml
alias: Put Qnap NAS to sleep
description: >-
When inactive for 60mins put Qnap to sleep. It can be woken via WOL and wakes
on schedule at 11.50pm on Sunday for Monday AM backup routines
triggers:
- trigger: numeric_state
entity_id:
- sensor.garage_qnap_eth0_download
for:
hours: 1
minutes: 0
seconds: 0
below: 0.5
conditions: []
actions:
- data: {}
action: shell_command.shutdown_qnap
mode: single

Create a binary sensor to detect if the qnap is awake

In HA go to set­tings -> Devices & ser­vices and click “add integration”
Search for ping (or you can use nmap if you know what you’re doing)
Introduzir o IP of the NAS and give it the name “qnap nas” (which will cre­ate the entity as ‘binary_sensor.qnap_nas’ which we use in the switch in the next step)

Create HA switch to wake and sleep the qnap

I did this with yaml, but it can now be done via the GUI...
To do it via the GUI...
In HA go to set­tings -> devices and services
click on the “help­ers” tab.
Click “cre­ate help­er” and scroll down to “tem­plate”
Pop­u­late the sec­tions using the inform­a­tion in the yaml below
Or to do it with YAML…
Open configuration.yaml and look for the “tem­plate” sec­tion. If there isn’t one cre­ate one with the fol­low­ing. If there is one then skip this step
template: !include template.yaml
Now open template.yaml and add the fol­low­ing code (OR add this code in configuration.yaml dir­ectly below the tem­plate sec­tion if there is no template.yaml file)
Note you will need to know the MAC address of the qnap inter­face that has WOL, and you should also gen­er­ate a unique guid using a free online guid generator
- switch:
- default_entity_id: switch.switch_qnap
state: "{% if is_state('binary_sensor.qnap_nas', 'on') %}On{% else %}Asleep{% endif %}"
name: QNap NAS
unique_id: "{uuid here}"
turn_on:
service: wake_on_lan.send_magic_packet
data:
mac: "{qnap MAC address here}"
turn_off:
service: shell_command.shutdown_qnap

You can now add this switch to any dash­board and it will show if the qnap is on and will allow you to turn the qnap on or off. Note that the qnap does take a minute or so to shut­down and sev­er­al minutes to boot up.

Setting a wake schedule on the NAS

I use a wake sched­ule to wake my NAS before my backups run. The backups start as Sunday ticks over into Monday (i.. em 00:00 on Monday morning)
Log into the NAS with your main admin user
Open the ‘con­trol pan­el’ and then go to Sys­tem -> Poder
Select the WoL tab and make sure WoL is enabled
then go to the power sched­ule tab
Tick “enable schedule”
click the icon in the “action” tab of the exist­ing entry (or cre­ate one if there isn’t an entry)
Select “turn on the serv­er” and set it to run every sunday at 23:50 (which gives the device 10 mins to boot up which should be sufficient)
In case HA isn’t work­ing for some reas­on I also like to have a fall-back on the NAS to send it back to sleep even­tu­ally, on the assump­tion my backup jobs wont take more than 23 horas (they nev­er get close to this)
Cre­ate a second sched­ule to shut­down the serv­er at 23:00 every day (I use every day so it goes back to sleep even if I woke it up manu­ally and HA isn’t work­ing to put it back to sleep)

Deixe uma resposta