Update: Changed mac_address to mac
Besides my application server running Proxmox VE (where my Home Assistant currently resides), I have a NAS (Network Attached Storage) containing plenty of media, backups of my photography, and many delicious recipes. My NAS is currently running the TrueNAS CORE operating system and despite its many faults, I’ll be sticking to that OS for the foreseeable future.
My NAS is far from perfect. In fact, it’s borderline embarrassing. I bought the cheapest case I could find and fitted the absolute minimum of hardware. It isn’t particularly quiet and nothing is dampened, so during writes this thing gets loud. Seeing as it sits right next to where I sleep, there’s no way of leaving it turned on 24/7.
This is where Home Assistant enters the fray. I needed a way of remotely turning my TrueNAS CORE on and off as it sits on a shelf that isn’t easy to reach. And this is what the result looks like in my Lovelace UI:
For this to work, I’m using the TrueNAS CORE API to trigger a system shutdown from Home Assistant. Turning on the TrueNAS CORE is way easier thanks to Wake on LAN (WOL) integration in Home Assistant. Let me start by showing you how to use the WOL switch to turn on your TrueNAS CORE instantly.
Booting TrueNAS CORE using Home Assistant
If you’re considering implementing this, I’m going to assume you are technically well versed and understand the basics of how Home Assistant and TrueNAS CORE work. For the WOL switch, you need to know the IP (though this is technically only optional) and MAC of your FreeNAS.
To find out your MAC, simply enter the command
ifconfig in the TrueNAS CORE shell and look for
hwaddr. The IP is solely used for Home Assistant to figure out if your TrueNAS CORE is turned on or not. So if you’re going to use the switch in the UI, I’d definitely recommend adding the IP.
switch: - platform: wake_on_lan name: TrueNAS CORE host: YOUR-TRUENAS-CORE-IP mac: "XX:XX:XX:XX:XX:XX"
Shutting down TrueNAS CORE using Home Assistant
Turning off a TrueNAS CORE using Home Assistant is slightly more complex. There’s no reverse WOL or anything similar and no universal way of handling shutdowns, so instead I’ve opted to use the TrueNAS CORE API. To make the API call, simply create a REST command containing the following details:
rest_command: truenas_core_shutdown: url: http://IP-OF-YOUR-TRUENAS-CORE/api/v1.0/system/shutdown/ method: post username: !secret truenas_core_user password: !secret truenas_core_password headers: content-Type: application/json
As you might have noticed, I’ve used the
secrets.yaml file to store my credentials. I had to use root to get this API call to work. There might be a nicer and safer way of doing this. To accomplish this, simply open the
secrets.yaml on your Home Assistant server and enter the following on a new line:
truenas_core_user: YOUR-ROOT-USER truenas_core_password: YOUR-PASSWORD
Tying it all together in one switch
Now that the TrueNAS CORE RESTful Command is complete, it can be added to the switch created in the first step. To do so, simply add the
turn_off variable at the end of the switch, as seen below:
switch: platform: wake_on_lan name: TrueNAS Core host: YOUR-TRUENAS-CORE-IP mac_address: "XX:XX:XX:XX:XX:XX" turn_off: service: rest_command.truenas_core_shutdown
And there you have it, TrueNAS CORE can now be controlled by Home Assistant. This switch can be used in automations using
switch.turn_off and can be placed in the UI.