Creating a service file for Home Assistant

tl;dr: There didn't seem to be a straightforward guide on how to create a systemd service for a virtual environment, so here's a quick'n'dirty version.

Because I prefer to run my Raspberry Pi's with the stock OS instead of the custom ones people have like Volumio, Hassbian, HiFiBerryOS, etc, I need to brush up on my sysadmin skills from time to time. In this case it was the fact that whenever I rebooted the pi that runs my audio receiver, my partner couldn't turn off our bedroom lamp :P Last week instead of fixing the problem I put my phone ssh key onto the pi so I could start Home Assistant from bed, which definitely took more time than creating a service which would run on boot.

Create the service file

emacs /etc/systemd/system/hass.service [Unit] Description=“Home Assistant”

[Service] User=homeassistant WorkingDirectory=/home/homeassistant VIRTUALENV=/srv/homeassistant Environment=PATH=$VIRTUALENV/bin:$PATH ExecStart=/srv/homeassistant/bin/hass Restart=on-failure


Enable the service

systemctl enable hass

Start the service, or reboot

systemctl start hass || reboot