Autostart Home Assistant on raspbian

hello all, i’m new in this community,
i want to auto-start HA when boot,

when i want to operate HA i write:

cd homeassistant
source bin/activate
hass

this is the homeassistant.service file that i made:

[Unit]
Description=Home Assistant
After=network-online.target

[Service]
Type=simple
User=homeassistant
ExecStartPre=source /home/pi/homeassistant/bin/activate
ExecStart=/home/pi/homeassistant/bin/hass
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

it’s saved here:
/etc/systemd/system

this error i receive when i make this:

pi@raspberrypi:~ $ sudo systemctl --system daemon-reload
pi@raspberrypi:~ $ sudo systemctl enable homeassistant
pi@raspberrypi:~ $ sudo systemctl start homeassistant
Failed to start homeassistant.service: Unit homeassistant.service has a bad unit file setting.
See system logs and 'systemctl status homeassistant.service' for details.
pi@raspberrypi:~ $ sudo systemctl status homeassistant
● homeassistant.service - Home Assistant
   Loaded: bad-setting (Reason: Unit homeassistant.service has a bad unit file s
   Active: inactive (dead)

Nov 16 20:16:44 raspberrypi systemd[1]: /etc/systemd/system/homeassistant.servic
Nov 16 20:16:51 raspberrypi systemd[1]: /etc/systemd/system/homeassistant.servic
Nov 16 20:19:31 raspberrypi systemd[1]: /etc/systemd/system/homeassistant.servic
Nov 16 20:19:36 raspberrypi systemd[1]: /etc/systemd/system/homeassistant.servic
Nov 16 20:22:16 raspberrypi systemd[1]: /etc/systemd/system/homeassistant.servic
Nov 16 20:34:12 raspberrypi systemd[1]: /etc/systemd/system/homeassistant.servic
Nov 16 20:34:25 raspberrypi systemd[1]: /etc/systemd/system/homeassistant.servic

please help me to fix this issue, i tried many days, but no luck, thank you all.

You’ll be happy to know that the documentation has examples you can use. Probably you’ll want this one.

thank you for help, i tried it, it’s not works for me:

pi@raspberrypi:~ $ sudo systemctl status homeassistant
● homeassistant.service - Home Assistant
   Loaded: loaded (/etc/systemd/system/homeassistant.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2019-11-17 19:51:02 IST; 5min ago
 Main PID: 554 (hass)
    Tasks: 4 (limit: 4915)
   Memory: 37.7M
   CGroup: /system.slice/homeassistant.service
           └─554 /home/pi/homeassistant/bin/python3 /home/pi/homeassistant/bin/hass -c /home/pi/.homeassistant

Nov 17 19:51:02 raspberrypi systemd[1]: Started Home Assistant.
Nov 17 19:51:07 raspberrypi hass[554]: 2019-11-17 19:51:07 ERROR (MainThread) [homeassistant.bootstrap] Unable to set up error log /home/pi/.homeassistant/home-assistan
Nov 17 19:51:07 raspberrypi hass[554]: 2019-11-17 19:51:07 ERROR (SyncWorker_1) [homeassistant.util.json] JSON file reading failed: /home/pi/.homeassistant/.storage/cor
Nov 17 19:51:07 raspberrypi hass[554]: Traceback (most recent call last):
Nov 17 19:51:07 raspberrypi hass[554]:   File "/home/pi/homeassistant/lib/python3.7/site-packages/homeassistant/util/json.py", line 30, in load_json
Nov 17 19:51:07 raspberrypi hass[554]:     with open(filename, encoding="utf-8") as fdesc:
Nov 17 19:51:07 raspberrypi hass[554]: PermissionError: [Errno 13] Permission denied: '/home/pi/.homeassistant/.storage/core.config'
Nov 17 19:51:07 raspberrypi hass[554]: 2019-11-17 19:51:07 ERROR (MainThread) [homeassistant.bootstrap] Home Assistant core failed to initialize. Further initialization
Nov 17 19:51:07 raspberrypi hass[554]: 2019-11-17 19:51:07 INFO (MainThread) [homeassistant.core] Starting Home Assistant
Nov 17 19:51:07 raspberrypi hass[554]: 2019-11-17 19:51:07 INFO (MainThread) [homeassistant.core] Timer:starting

pi@raspberrypi:~ $ 

Looks like you’ve been using sudo or running things as a user other than pi

What does the following return:

find /home/pi/.homeassistant/ ! -user pi -print|wc -l

It should return zero.

pi@raspberrypi:~ $ find /home/pi/.homeassistant/ ! -user pi -print|wc -l
0

what’s the meaning of this result?
what i have to do to fix my issue?

this is my homeassistant.service:

[Unit]
Description=Home Assistant
After=network-online.target

[Service]
Type=simple
User=homeassistant
ExecStart=/home/pi/homeassistant/bin/hass -c "/home/pi/.homeassistant"
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

i changed the user from homeassistant to pi and now it’s works perfect thank you for helping

This means you didn’t follow the directions properly on setting up your python virtual environment. It should be running as the homeassistant user.

1 Like

This example is not good, thats why I am searching, why a @?

In what way is it “not good”?