autostart and rc service - tprelog/iocage-homeassistant GitHub Wiki

Home Assistant Core - RC Service Quick Start

Create a new jail

These examples use standard settings with DHCP. Replace --name $JAIL_NAME with the --name NAME_OF_YOUR_JAIL

  • TrueNAS Core
iocage create -r 13.2-RELEASE dhcp=on bpf=yes vnet=on boot=on allow_raw_sockets=1 --name $JAIL_NAME

Connect to the jail's console and install the suggested requirements

iocage console $JAIL_NAME
pkg install -y autoconf bash ca_root_nss curl ffmpeg gcc gmake libjpeg-turbo libxml2 libxslt pkgconf python311 py311-numpy py311-sqlite3 rust sudo wget zip

Create a non-root user to run the Home Assistant Core service

Typically, the username is similar to the name of the service running inside the jail. Also, the UID usually matches the default port number the service uses. Here, I'm using the username homeassistant with a UID of 8123, matching the default port. Since this account is only used for running a service inside this jail, -w no is used to disable password login. The argument -G dialout adds the user to the dialout group as required for Z-Wave and Zigbee USB sticks. Finally, -d /home/homeassistant assigns the user's $HOME. This is also the default location for the Home Assistant configuration directory.

  • This example matches the homeassistant user created by the TrueNAS Core Community Plugin
    • UID = 8123 This matches the default port used by the Home Assistant
    • SHELL = /usr/local/bin/bash This service expects the shell to be bash
    • USER = homeassistant
    • HOME = /home/homeassistant
install -d -g 8123 -o 8123 -m 775 -- /home/homeassistant
pw adduser -u 8123 -n homeassistant -d /home/homeassistant -w no -s /usr/local/bin/bash -G dialer

Home Assistant Core - RC Service Script

Create directory /usr/local/etc/rc.d if it does not exist.

mkdir -p /usr/local/etc/rc.d

Download the latest rc service script

wget -O /usr/local/etc/rc.d/homeassistant https://raw.githubusercontent.com/tprelog/iocage-homeassistant/master/overlay/usr/local/etc/rc.d/homeassistant
chmod +x /usr/local/etc/rc.d/homeassistant

Configure RCVARS for Home Assistant using sysrc

Set the Python version to be used when creating the virtualenv

  • Check Home Assistant for a supported version of Python
sysrc homeassistant_python=/usr/local/bin/python3.11

Set the directory where the virtualenv will be installed

sysrc homeassistant_venv=/usr/local/share/homeassistant

Set the user this service will run as

  • This user must already exist (should be the user you previously created)
  • default if not set: homeassistant
sysrc homeassistant_user=homeassistant

Set the Home Assistant config directory

  • default if not set: ~/.homeassistant
sysrc homeassistant_config_dir=/home/homeassistant/config

Enable the service

sysrc -f /etc/rc.conf homeassistant_enable=yes

Install Home Assistant Core

  • Install the latest version
service homeassistant install homeassistant
  • Install a different version
service homeassistant install homeassistant==0.109.3
  • You can include any additional packages as well
service homeassistant install homeassistant colorlog

Starting the service

  • Initial startup may take an additional 5-10 minutes before the Home Assistant UI reachable
  • Please allow additional time after updates as packages maybe updated during this time as well
service homeassistant start
  • If you have enabled logging in homeassistant/configuration.yaml, you can can watch what's happening
service homeassistant logs -f

Check if the service is running

service homeassistant status

homeassistant is running as pid 19589.
http://10.10.1.223:8123

Restart the service

  • The service will run config check before restarting
  • A failed config check does not prevent a restart
service homeassistant restart

Testing configuration at /home/homeassistant/homeassistant
Stopping homeassistant.
Starting homeassistant.
homeassistant is running as pid 22512.
http://10.10.1.223:8123

Upgrading Home Assistant Core

  • This will stop the service ( if its running )
  • Upgrade Home Assistant Core to the latest version
  • Run a config check - A failed config check does not prevent an attempt to restart
  • Attempt to start Home Assistant ( only if it was running before the update )
service homeassistant upgrade
  • You can also perform these steps separately, for example to upgrade to the next beta release
service homeassistant stop
service homeassistant install --upgrade homeassistant
service homeassistant check_config
service homeassistant start
  • If you want to try out the next beta release you can use the following
service homeassistant stop
service homeassistant install --pre --upgrade homeassistant
service homeassistant check_config
service homeassistant start

Some basic functions to view the log file

  • default: Show full log
    • logs -f Update view as new lines are added
    • logs -h NUM Show first NUM of lines
    • logs -l Show with less
    • logs -n NUM Display the last NUM of lines
    • logs -t NUM Same as -n NUM
service homeassistant logs -f

Reinstall Home Assistant Core

  • This will recreate ( delete then clean install ) the virtualenv

Reinstall the same version your are currently using

  • This will first try to get the version from ${homeassistant_config_dir}/.HA_VERSION
  • If no version can be determined, this will reinstall the latest version
service homeassistant reinstall

You are about to recreate the virtualenv: /usr/local/share/homeassistant
The following packages will be reinstalled: homeassistant==0.112.5
 Type 'YES' to continue:

Reinstall using the latest version

service homeassistant reinstall homeassistant

You are about to recreate the virtualenv: /usr/local/share/homeassistant
The following packages will be reinstalled: homeassistant
 Type 'YES' to continue:

Reinstall a specific version

service homeassistant reinstall homeassistant==0.109.3

You are about to recreate the virtualenv: /usr/local/share/homeassistant
The following packages will be reinstalled: homeassistant==0.109.3
 Type 'YES' to continue:

Include other packages here as well

  • You can include any additional packages as well
service homeassistant reinstall homeassistant colorlog

You are about to recreate the virtualenv: /usr/local/share/homeassistant
The following packages will be reinstalled: homeassistant colorlog
 Type 'YES' to continue: yes

There's an option to clear the pip cache

  • This is optional but should typically be safe in most cases
  • This is recommended if your using this service in a jail
  • This is required if you are reinstalling to use a different version of openssl
Do you want to clear the pip cache?
 Type 'YES' to clear cache: