Dec 5, 2019

Controlling TrueNAS CORE using Home Assistant

Disclosure: This post contains affiliate links. If you click through and make a purchase, I’ll earn a commission, at no additional cost to you. Read my full disclosure here.

A digital illustration of a futuristic server room.

In addition to my Proxmox VE application server that hosts my Home Assistant, I also have a NAS (Network Attached Storage) which stores a lot of media, such as backups of my photography and many delicious recipes. Currently, my NAS is operating on TrueNAS CORE and I plan to continue using that operating system for the near future, despite its faults.

My TrueNAS CORE NAS is far from ideal. It's quite basic, I bought the most affordable case available and installed the bare minimum of hardware. It's not particularly quiet and there is no sound dampening, so it gets loud when writing. Since it's located right next to where I sleep, I can't leave it on 24/7.

Table of Contents

Why I need Home Assistant to control TrueNAS CORE

This is where Home Assistant enters the fray. I needed a way of remotely turning my TrueNAS CORE server on and off, as it sits on a shelf that isn’t easy to reach.

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.

The FreeNAS switch in my Home Assistant

Booting TrueNAS CORE using Home Assistant

Before you implement this, it is important to note that I'm assuming you have a solid understanding of both Home Assistant and TrueNAS CORE. To set up the WOL switch, you will need to know the IP and MAC address of your FreeNAS; technically, the IP address is optional, though you are likely to know it already.

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 are going to use the switch in the UI, I would definitely recommend adding the IP to the configuration.

Once you have the MAC and IP on hand, the following entry to your configuration.yaml file will create a switch that allows you to boot your TrueNAS CORE machine:

 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

Shutting down a TrueNAS CORE system using Home Assistant can be slightly more complex. There is no option for reverse WOL or similar functionality, and no standard method for handling shutdowns. Therefore, I have chosen to use the TrueNAS CORE API. To make the API call, you can create a REST command that includes 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

With the TrueNAS CORE RESTful Command complete, it can be added to the switch that was created earlier. To accomplish this, simply append the “turn_off” variable to the end of the switch's configuration, as shown 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_on, switch.toggle, and switch.turn_off and can be placed in the UI.

And that's it, you can now control your TrueNAS CORE machine using Home Assistant. This switch can be integrated into automations using commands such as “switch.turn_on”, “switch.toggle”, and “switch.turn_off”. You can also add it to your Dashboard for manual control.

4 thoughts on “Controlling TrueNAS CORE using Home Assistant”

    • That’s great to hear! I don’t use FreeNAS anymore so thanks for confirming that this still works😊

      Reply

Leave a comment