Autostart Home Assistant

I tried the Hassbian image and the AIO and could not get either to work. With the Hassbian image, Open Zwave did not work after following the instructions and the AIO installer out right failed. So this time I thought I would try the manual install and see if the results were different.

I tried entering the autostart config as sudo su and it at least accepted the configuration without any authentication error, however, when I check the status, I get this:

sudo systemctl status home-assistant@pi -l
● [email protected] - Home Assistant
   Loaded: loaded (/etc/systemd/system/[email protected]; enabled)
   Active: failed (Result: exit-code) since Wed 2016-12-14 12:41:53 CST; 1min 19s ago
  Process: 682 ExecStart=/srv/homeassistant/homeassistant_venv/bin/hass -c /home/hass/.homeassistant (code=exited, status=217/USER)
 Main PID: 682 (code=exited, status=217/USER)

Dec 14 12:41:52 HARPi3 systemd[1]: Started Home Assistant.
Dec 14 12:41:53 HARPi3 systemd[1]: [email protected]: main process exited, code=exited, status=217/USER
Dec 14 12:41:53 HARPi3 systemd[1]: Unit [email protected] entered failed state.

And I have not even gotten to installing Open ZWave yet and it is not encouraging. I understand the AIO is probably the easiest way to go since it does virtually everything for you, but I have tried twice now, on Jessie and Jessie Lite and both times the AIO installer has failed within just a minute or so of starting.

Now that I know that there is way to edit the page so that I can contribute what I find, I will give that a shot, but it doesn’t help that both Hassbian and the AIO do not work and have no idea where to start with respect trying to make it work.

To enable a service to start on reboot, you need to do

sudo systemctl enable <servicename>

On my hassbian image the service name is [email protected], but you seem to have used the pi user to install so your service will be [email protected].

The recommended practice is to create a separate user to run homeassistant, so you can restrict permissions to only what HA needs.

I know I have some Linux experience, but installing the hassbian image took less than hour, so I think you are going about it the hard way.

@gpbenton Thank you for that information.

I will add, if you think I am going about it the hard way, then that just further makes my case about the documentation as I followed only that, step-by-step, to get everything installed.

Kind of in the same boat as you StormStrikes - installed homeassistant using the AIO installer - that failed, found the fix for it (installing some libraries that were missing) on Gitter forum. Then scrapped that and went with the manual install to get a better grasp on how it all worked.

So in all the research I found that the documentation has a huge issue where it refers to hass. When using the AIO installer hass is the user created, but if manually installed homeassistant becomes the user. Really changes up a lot of the ‘how to’ guides.

Right now i can manually start homeassistant and all is good- but I can’t get the systemd daemon to work quite yet. BUT- I should be able to change the path to where homeassistant is much like your revised section above.

EDIT: found the system service poking around using some of the above comments. Big question is: Is the user still hass as the modified code StormStrikes made? On the wiki page it represents the user as %i, on this page it is hass, but should it be homeassistant (as that was the user that was created doing the manual install) ?

The user is whatever user name you (or the AIO installer) created to run home assistant.

Its only a name, you just have to be consistent within your own installation.

I’ve had the same issues that you mentioned with Hassbian and the AIO installer. So I followed the instructions to to the manual installation, which uses different paths and users for the installation. I was finally able to get the autostart working with these steps:

Create the service file manually with

sudo nano /lib/systemd/system/[email protected]

Once that file opens, paste/type in the autostart script

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

[Service]
Type=simple
User=homeassistant
ExecStartPre=source /srv/homeassistant/homeassistant_venv/bin
ExecStart=/srv/homeassistant/homeassistant_venv/bin/hass -c "/home/homeassistant/.homeassistant"

[Install]
WantedBy=multi-user.target

*Note that this is using the ‘homeassistant’ user and the hass path for the virtual installation. This could vary depending on how you setup home assistant (though it sounds like we’ve done the same).

Then press Ctrl+X to close, press Y to save, then hit enter.

The final steps are to reload and enable the autostart file. Run the four steps below, one after another - final step is reboot which should allow you to test it out.

sudo systemctl --system daemon-reload
sudo systemctl enable home-assistant@pi
sudo systemctl start home-assistant@pi
sudo reboot

Hope you helps you!

4 Likes

Thanks!!!

That was exactly it! I did change the user and subdirectory in the original copy/paste code to homeassistant instead of hass.

The big problem I had was where to put the service file!!

On the wiki page for autostart it makes it look like it goes into /etc/systemd/system/[email protected].

But creating it in /lib/systemd/system made it work on the first go round!
Thanks raff - lifesaver there

@StormStrikes take a look below at what @raff has for the systemd directory location and the template. Worked for me.

The only big errors in your template is the Username and the user directory (after the -c argument) should be homeassistant and not hass. I’m just glad to have it autostart!

1 Like

@raff this worked for me. I was also struggling with getting this going. Thanks for the post._

@raff this is amazing! I couldn’t figure out how to create the service file (I tried using information from another tutorial, but ran into a root permissions issue), but your instructions worked like a jiffy!

I recommend that your command for creating the service file be added to the documentation in this pull request.

Thank you so much!

@raff, thank you sooo much. I dont entirely understand what the issue was but so glad it is fixed and works perfectly.

thank you worked for me after watching you tube and various other vids, was lost with he HA guides…

Hopefully this is still on topic. I couldn’t get the @raff solution to work for me, because I am noob, however…
After succesfully installing HASS Python Virtual Environment Install, each time I reboot the pi, I do these steps:

$ cd homeassistant
$ source bin/activate
$ hass --open-ui

I would like to autostart, but I’m very bad at this, but trying to learn.

Is the intent that the above @raff solution solve for this type of install?
If not, is there another way (shell?)
If so, what am I missing?

Thanks all!

@45Matches The information under the [Service] tag need to match where you installed homeassistant and the user you used to install it. Use the pwd command to get the full path to your installation after you cd to homeassistant then modify the /etc/systemd/system/[email protected]

1 Like

@zarthan thanks for the reply, much appreciated. If I understand correctly, is this what you meant?

$ cd homeassistant
$ pwd
/home/pi/homeassistant 

then
sudo nano /lib/systemd/system/[email protected]
and paste

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

[Service]
Type=simple
User=homeassistant
ExecStartPre=source /home/pi/homeassistant/homeassistant_venv/bin
ExecStart=/home/pi/homeassistant/homeassistant_venv/bin/hass -c "/home/homeassistant/.homeassistant"

[Install]
WantedBy=multi-user.target

Running Home Assistant status, I assume it’s not quite correct.

[email protected] - Home Assistant
   Loaded: loaded (/etc/systemd/system/[email protected]; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2017-12-17 13:26:20 AEDT; 17s ago
  Process: 1085 ExecStart=/home/homeassistant/bin/activate -c /home/homeassistant/.homeassistant (code=exited, status=203/EXE
 Main PID: 1085 (code=exited, status=203/EXEC)

Dec 17 13:26:20 raspberrypi systemd[1]: Started Home Assistant.
Dec 17 13:26:20 raspberrypi systemd[1085]: [email protected]: Failed at step EXEC spawning /home/homeassistant/bin/ac
Dec 17 13:26:20 raspberrypi systemd[1]: [email protected]: Main process exited, code=exited, status=203/EXEC
Dec 17 13:26:20 raspberrypi systemd[1]: [email protected]: Unit entered failed state.
Dec 17 13:26:20 raspberrypi systemd[1]: [email protected]: Failed with result 'exit-code'.

Have you considered the hassio install method, which takes care of this for you?

It was the first install I tried. On that first attempt I didn’t do the wifi config correctly. I’m on a Pi Zero W. Having a second go at it now after watching this video https://youtu.be/s5_dtRoFYyU. We’ll see what happens.

This blind install without seeing any progress logs or anything is a little painful when it doesn’t work. Throwing away 30 min chunks of time each time it fails. Trial and error I guess.

Sorry to hear you are having trouble with this! Good news and bad news - the bad news is that they’ve updated the installations a bit since this post, so I think my original instructions are outdated at this point. The good news is that they’ve also updated some of the associated documentation - https://home-assistant.io/docs/autostart/systemd/

Have you tried those instructions for the virtual environment install? I’ll admit that I haven’t tried to new instructions myself, but hopefully it will get you going.

Thanks anyway. I’ve concluded linux/python are too much headache to continue. Back to OpenHab2.