﻿Switching your Windows or Linux PCs on and off automatically or from a remote location
can be important. In this video, you will learn how to do this from Home Assistant, Node-Red,
and the Linux shell. As an example, I will save about 99% of the energy used by my Proxmox Backup
server because it only runs a few minutes a day. I will also start a Windows PC in our
remote radio station—all free of charge, of course. Wake-on-LAN is the “magic” word.
Grüezi YouTubers. Here is the guy with the Swiss accent. With a new episode and fresh ideas around
sensors and microcontrollers. Remember: If you subscribe, you will always sit in the first row.
As many of you know, we have a solar-powered radio station in a remote location. Because of
energy constraints, I wanted to be able to switch the PC remotely. Switching off a Windows PC is a
piece of cake. Just hit the “shutdown” button. But how to turn it on remotely? Not so easy.
You can buy such PCIE cards to power and reset a PC. But my one-liter PCs do not have such a slot,
and why spend money for nearly nothing? How can it be done instead? Most modern
PCs have a built-in “Wake on LAN” functionality. Even when the PC is powered down, its Ethernet
chip still listens to a “magic packet” and, if everything is set up correctly,
boots the PC as soon as it gets such a packet. This is exactly what we need. Of course,
this does not work for PCs or laptops connected by Wi-Fi only. But who operates important
systems over Wi-Fi? To get this working,
we have to solve two problems: 1. Enable Wake on LAN on the target PC
2. Produce a “magic packet” from our preferred platform
Let’s start with point 1. On all PCs, we have to enable the feature in the BIOS. As usual,
you have to search for it—usually somewhere in the power options. Unfortunately, this is not all.
So, let’s continue with a Windows machine. Here, we have to enable “Wake on Magic
Packet” in the device management → network adapters → Advanced tab.
Because the magic packet has to be sent to the MAC address of the network adapter,
we go to the command shell and enter ipconfig /all. Here we find the MAC address of the network
adapter we want to use. Note it down for later. For Linux systems, we use the shell and type “ip
a” to show all network connections. Usually, we need the first adapter after loopback.
On my Proxmox server, it is eno1. Often, it is eth0. Also, copy this MAC address to a notepad.
Now we enter “ethtool eno1 | grep Wake”. Maybe you need to add sudo. The result is
something like that. If you find a “d” after “Supports Wake-on”, your network adapter does
not support WOL. This applies to virtual network adapters, of course. If you find the letter “g”,
it supports the “magic packets”. Next, we check “Wake-on”. If it says “g”,
everything is okay. If not, we have to change it persistently to survive a reboot of the
system. We can do that by adding this line to the interfaces file, just below the iface definition.
After reboot, we can check if it is ok. Now we can proceed to point 2:
Wake the target PC up from another machine. Here, I want to show you three possibilities
and start with Node-Red. Go to “manage palette,” search for “node-red-node-wol,” and install it.
Now, you should find a new node called “wake on LAN.” During configuration, I had some
issues that made it necessary to dig into how WOL works, so let’s look at what I learned.
The “magic packet” we talked about before is a simple UDP packet usually sent to port 9 of all
addresses connected to your local area network. Let’s capture one of those using Wireshark and
the incredibly useful “packet sniffer” of my MikroTik router. I have included a link to
a video if you use MikroTik equipment and want to use it in your projects.
Here is the packet: It contains six times FF and then 16 times the MAC address of the computer
to wake up. To be sent to everybody, it has the “broadcast” destination address 255.255.255.255.
Unfortunately, because my Node-Red runs in a Docker container, broadcast packets are
blocked somewhere in the Docker bowels, and we have to enter the target PC's IP address
in addition to its MAC address. Like that, it is no longer a broadcast package, and it works.
Cool. I can now add a button node to my simple dashboard to switch the PC on from remote.
But how is this done with Home Assistant? It's not much more complicated.
We have to add these lines in the config.yaml file. Give it a good name,
check the configuration, and reload the yaml file. Now, you should be able to find
the switch in the entities menu. Next, go to the switches dashboard,
and add a new button card. Select the entity, and select “call service” as a tap action, not
as a hold action. If you select “wake on LAN”, you can add the MAC address from before. Save and test
it out. It should work without problems. At least if you use a standard HA configuration. Otherwise
you probably have to add the IP address, too. So we can proceed to the most complex problem:
Automate my backup server to save a lot of energy. How can this be done? Let’s start with my setup.
I run all my stuff on the Proxmox VE server hosted on this mini PC. Of course,
I want a backup of all my VMs in case something happens. I want my backup system completely
separated from the productive machine. This is why I added a second of these small
servers on top of the first one and installed Proxmox Backup server. Maybe I will move this
system to a different location in the future. Installing and using Proxmox Backup server is
simple and straightforward, BTW. I used the full built-in disk for backup and installed Proxmox on
a memory stick. The only thing I had to do on my Proxmox VE production server was to add a backup
job that includes all VMs. It runs at 1 a.m. in the night and takes a few minutes. The only
problem is that my backup server runs 24/7 and wastes a lot of energy. This is not what I want.
What I want is that it starts three minutes before the backup and runs till after the backup is
finished. Then, it has to automatically shut down and conserve energy for the next 23 hours and 50
minutes. Again, we have to solve two problems: 1. Start the backup server from remote
2. Shut it down after the last backup has finished
After all our preparation, point one is easy. We could automate it with Node-Red
or with Home Assistant. But we will start the backup server from the production Proxmox
Linux server. Why? Because we can, of course! A big shout out to “Apalrd's Adventures,” who
did a write-up on how to do this task. How can we create a “magic packet” from
Linux? Easy. We just have to install wakeonlan on our Proxmox console.
If we add the backup server's MAC address, we can test it. It works without any problems.
So, what is the overall setup for this “energy-saving” scenario?
We need a timer on the production system that runs 24/7 and calls a service that executes
the wakeonlan command 3 minutes before 1 a.m. The backup job is scheduled to run at 1 a.m.
and runs for whatever time it takes. In parallel, we need a second timer
that starts a few minutes after the backup server is started. It calls a service that
executes a bash script that waits until the last backup is done and, one minute later,
shuts the backup server down. That is all. Let’s start the implementation. By the way,
to make it simple for you, the video description includes a link to the writeup.
We start with the service on the production server that executes the wake on LAN command
when called. Its name is “wakebigstor.service”. As said before, we want to wake our backup server
at 00:57. This is why we create wakebigstor.timer that does exactly that. Of course, we have to
enable the timer and reload the daemons. With these steps, our Proxmox production
system creates a magic packet for our backup server before 1 a.m. Proxmox Backup
boots fast and is ready for the backup task that starts at precisely 1 a.m.
So, we learned how to create a “magic packet” on Linux, too. Excellent!
We can now proceed to step two: Wait until all backups
are finished and shut the backup server down. Of course, this has to be done in the shell of the
backup server. We use a simple trick to find out when the last backup is finished: As we see here,
each backup runs in a task. No backup, no task. So we need a shell script that continuously counts
the tasks and, if no task is running anymore, waits for a minute before it issues the shutdown
command. Not too complicated. We create and call the bash script “autoshutdown”. And, as usual,
we do not forget to make it executable. As with wakebigstor.service, we need
autoshutdown.service to call this script. autoshutdown.timer makes sure the service
is called in parallel or after the backup job. In my case, it is set to 1:03 a.m.
Overall, this is an elegant solution! It could also be used for other scenarios
where you do not want to run a machine 24/7. For example, you could power some
of your systems down when you are sleeping. In this video, we learned an elegant way to
start Windows and Linux PCs remotely and created “magic packets” from Node-Red,
Home Assistant, and Linux. With Node-Red, I switched the
PC in my remote radio station, and with Linux, I automated my Proxmox
Backup server to conserve a lot of energy. One last thing as a bonus for the guys still
watching: Because the backup runs around midnight, I added an email notification
to my Proxmox VE. In its newest version, this became much easier because you can directly add
an SMTP service. Very handy, also for other error messages. Maybe you give it a try?
This was all for today. As always, you find all the relevant links in the description.
I hope this video was useful or at least interesting for you. If true,
please consider supporting the channel to secure its future existence. Thank you! Bye