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 better for it to be off to save power (around 40–50W!). After some fiddling I managed to achieve what I needed via home assistant. Here’s how…
Getting Qnap ready to talk to HA
Install Putty (inc puttygen) and then use putty-gen to generate a 2048-bit RSA public-private key pair
Copy the both to your home assistance config/.ssh folder (create it if it doesn’t exist)
Open the PUBLIC key in a text editor (e.g. Notepad++)
Now log in to the Qnap
Create a second admin user on the QNAP with a nice long complex password. This account unfortunately does need to be an admin account not just a regular user (in order to read the network bandwidth usage)
Click on your username at the top right
Select “login and security” from the drop-down menu
Select the “SSH Keys” tab
Click the blue link to “SSH Connection” which will open the control panel at the right place
Tick “allow SSH connection” and if you wish to change the port (I usually do) and click apply
Close the control panel and go back to the “login and security” window
Click the blue text “access permissions”
Tick the box next to the new username to allow it.
Log out of the qnap
Log into the Qnap as the new user
Click on your username at the top right
Select “login and security” from the drop-down menu
Select the “SSH Keys” tab
Click “add”
Copy and paste the whole contents of the PUBLIC key you opened earlier and then click “add”
Initial HA setup
Now open your home assistant configuration.yaml and at the bottom add the following lineshell_command:
shutdown_qnap: !secret SSH_Qnap_Command
Now open secrets.yaml (create it if it doesn’t exist) and add the following, replacing the sections in curly brackets with your ssh port (22 unless you changed it), qnap username that you created, the IP of the qnap, and the long ugly password you created for the new qnap suerSSH_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 settings -> devices & services -> add integration
Search for QNAP
Follow the wizard and enter the IP, username and password needed to access your qnap (the new account you created on the qnap)
This should create a Qnap device with several entities. There may be a number of disabled entities.
Check if the eth0 (and/or eth1 etc) entities are enabled or disabled. If they are disabled enable the ones you need and make sure after a minute or so they populate correctly. Also check what units they are using (e.g. mb/sec or kb/sec)
Creating an HA automation to put inactive qnap to sleep
In HA go to ‘settings’ -> ‘automations & scenes’ and click “create automation”
Create from blank (don’t use a template)
Once in the automation, click on the 3 vertical dots at the top right and click “edit in yaml”
Paste the following code, editing to suit where needed. Change ‘sensor.garage_qnap_eth0_download’ to the ethernet usage entity you need. I have used activity below 0.5mb/sec as an indication that the device is no longer running any active tasks. Note the action matches the name we added earlier to configuration.yamlalias: 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 settings -> Devices & services and click “add integration”
Search for ping (or you can use nmap if you know what you’re doing)
Enter the IP of the NAS and give it the name “qnap nas” (which will create 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 settings -> devices and services
click on the “helpers” tab.
Click “create helper” and scroll down to “template”
Populate the sections using the information in the yaml below
Or to do it with YAML…
Open configuration.yaml and look for the “template” section. If there isn’t one create one with the following. If there is one then skip this steptemplate: !include template.yaml
Now open template.yaml and add the following code (OR add this code in configuration.yaml directly below the template section if there is no template.yaml file)
Note you will need to know the MAC address of the qnap interface that has WOL, and you should also generate 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 dashboard 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 shutdown and several minutes to boot up.
Setting a wake schedule on the NAS
I use a wake schedule to wake my NAS before my backups run. The backups start as Sunday ticks over into Monday (ਆਈ. at 00:00 on Monday morning)
Log into the NAS with your main admin user
Open the ‘control panel’ and then go to System -> Power
Select the WoL tab and make sure WoL is enabled
then go to the power schedule tab
Tick “enable schedule”
click the icon in the “action” tab of the existing entry (or create one if there isn’t an entry)
Select “turn on the server” 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 working for some reason I also like to have a fall-back on the NAS to send it back to sleep eventually, on the assumption my backup jobs wont take more than 23 hours (they never get close to this)
Create a second schedule to shutdown the server at 23:00 every day (I use every day so it goes back to sleep even if I woke it up manually and HA isn’t working to put it back to sleep)

“Hi James I realise it has been a long while, but I just checked this on windows 11 (build 23H2)…”