DEV Community

jeikabu
jeikabu

Posted on • Originally published at rendered-obsolete.github.io on

Installing Home Assistant

For quite some time now I’ve been plotting to put a Raspberry Pi 3 model B to work as some kind of home automation/”smart” home thingy. After looking at a few options, I’ve more or less settled on Home Assistant- aka “Hass”. It seems to check all the boxes: open-source, optionally offline, just-works™, and not fugly. An official Android app was also recently announced.

Before getting started, install Raspbian or some other distribution of your choice.

Install

Installing Hass is easy. The top-level install instructions list three ways to do it:

Here we’ll go through the manual route also referencing additional instructions for the Raspberry Pi. If you’re unfamiliar with Python/pip/venv, read “Installing packages using pip and virtual environments”.

# Install pre-requisites
sudo apt-get install -y python3 python3-dev python3-venv python3-pip libffi-dev libssl-dev

# Create `homeassistant` user Hass will run as
sudo useradd -rm homeassistant -G dialout,gpio,i2c,audio
cd /srv
sudo mkdir homeassistant
sudo chown homeassistant:homeassistant homeassistant
# Switch to `homeassistant` user
sudo -u homeassistant -H -s

# Create python virtual environment and switch to it
cd /srv/homeassistant
python3 -m venv .
source bin/activate

# Install Home Assistant in venv and run once to complete setup
venv$ python3 -m pip install wheel
venv$ pip3 install homeassistant
venv$ hass
Enter fullscreen mode Exit fullscreen mode

The first time it runs a bunch of additional packages will be installed. Once the following text appears installation has completed:

INFO (MainThread) [homeassistant.core] Timer:starting
Enter fullscreen mode Exit fullscreen mode

Auto Start

Additional instructions are provided to make Hass start automatically at boot.

As super-user create /etc/systemd/system/home-assistant@YOUR_USER.service (where YOUR_USER is the name of the user you created above, in this case homeassistant):

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

[Service]
Type=simple
User=%i
ExecStart=/srv/homeassistant/bin/hass -c "/home/%i/.homeassistant"
# Restart on failure
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

For example:

sudo vim /etc/systemd/system/home-assistant@homeassistant.service
## Paste the above configuration then `:wq`

# Reload systemd and enable to start automatically at boot
sudo systemctl --system daemon-reload
sudo systemctl enable home-assistant@homeassistant
# Start it now
sudo systemctl start home-assistant@homeassistant
Enter fullscreen mode Exit fullscreen mode

For help debugging Hass installs:

sudo systemctl status home-assistant@homeassistant
# Stop Home Assistant
sudo systemctl stop home-assistant@homeassistant
# Restart Home Assistant
sudo systemctl restart home-assistant@homeassistant
# View log output
sudo journalctl -f -u home-assistant@homeassistant
Enter fullscreen mode Exit fullscreen mode

Dashboard

My Pi is tucked inside the official 7” Touch Display, and I’d like to have it launch the Hass GUI as a dashboard to serve as a control panel for my entire flat. The two main approaches are:

  • systemd
  • autostart

There’s a few more ideas in this forum thread.

Systemd

I first tried the solution from this post about starting a dashboard on a Raspberry Pi 4. It didn’t work for me, but I didn’t spend much time debugging it so I plan to come back to it.

As super-user create /etc/systemd/system/dashboard.service:

[Unit]
Description=Chromium Dashboard
Requires=graphical.target
After=graphical.target

[Service]
Environment=DISPLAY=:0.0
Environment=XAUTHORITY=/home/pi/.Xauthority
Type=simple
ExecStart=/home/pi/dashboard.sh
Restart=on-abort
User=pi
Group=pi

[Install]
WantedBy=graphical.target
Enter fullscreen mode Exit fullscreen mode

Create ~/dashboard.sh:

#!/usr/bin/env bash

# Prevent putting screen to sleep
xset s noblank
xset s off
xset -dpms

# Hide mouse cursor
unclutter -idle 0.5 -root &

# Ignore "unclean" Chrome shutdowns
sed -i 's/"exited_cleanly":false/"exited_cleanly":true/' /home/pi/.config/chromium/Default/Preferences
sed -i 's/"exit_type":"Crashed"/"exit_type":"Normal"/' /home/pi/.config/chromium/Default/Preferences

# Open Home Assistant dashboard
/usr/bin/chromium-browser --noerrdialogs --disable-infobars --kiosk http://localhost:8123 &

# Make the script executable
chmod +x ~/dashboard.sh
# Install helper
sudo apt-get install unclutter
# Enable the service
sudo systemctl enable dashboard.service
# Optionally, view log output
sudo journalctl -f -u dashboard.service
Enter fullscreen mode Exit fullscreen mode

Autostart

The solution that did work for me was following a sparkfun tutorial covers using autostart to launch a program at startup. Create ~/.config/autostart/dashboard.desktop:

[Desktop Entry]
Type=Application
Name=Dashboard
Exec=/home/pi/dashboard.sh
Enter fullscreen mode Exit fullscreen mode

Here using the same dashboard.sh from above.

End Result

The default GUI is servicable:

During Hass initial setup it automatically detected the Philips Hue “smart lighting” in my home. You can turn lights on and off, adjust the brightness, etc. And by supplying my location there’s a weather widget with the local weather and forecast.

This serves as a good starting point. Next I’ll be looking at doing some actual automation and getting some other features working.

Common Problems

If you don’t install ffi, pip install homeassistant will fail with:

c/_cffi_backend.c:15:10: fatal error: ffi.h: No such file or directory
       #include <ffi.h>
                ^ ~~~~~~
Enter fullscreen mode Exit fullscreen mode

There are issues installing hass without venv:

2019-10-04 03:16:59 INFO (MainThread) [homeassistant.setup] Setting up recorder
Exception in thread Recorder:
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.6/dist-packages/homeassistant/components/recorder/ __init__.py", line 211, in run
    from .models import States, Events
  File "/usr/local/lib/python3.6/dist-packages/homeassistant/components/recorder/models.py", line 6, in <module>
    from sqlalchemy import (
ModuleNotFoundError: No module named 'sqlalchemy'

2019-10-04 03:17:09 WARNING (MainThread) [homeassistant.setup] Setup of recorder is taking over 10 seconds.
Enter fullscreen mode Exit fullscreen mode

If not using venv and python3 -m pip install without sudo or --user:

ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/local/lib/python3.6/dist-packages/_cffi_backend.cpython-36m-aarch64-linux-gnu.so'
Consider using the `--user` option or check the permissions.
Enter fullscreen mode Exit fullscreen mode

If you ssh in without -X, hass --open-ui gets stuck without UI:

2019-10-04 22:02:40 INFO (MainThread) [homeassistant.core] Timer:starting
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
deciduously profile image
Ben Lovy

Timely - I was already planning this exact project/product in the next few weeks! Thanks for the write-up, this will be very useful.

Collapse
 
jeikabu profile image
jeikabu

There's actually another good one here on dev.to: Living with Smart Home Automation for a Year

On a related note, your post on robot vacuums spurned me to get one of my own. I'm not 100% sure, but I'm pretty sure I can do some basic "smart home" integration.

Collapse
 
deciduously profile image
Ben Lovy

Ah, I hadn't even thought of that! Ours is just set to run once every 24h at a set time, but there's definitely potential for tuning there. Thanks for the link!

Thread Thread
 
jeikabu profile image
jeikabu

See, I've got static schedules: morning alarm, work tasks, lighting, now vacuuming, etc., but I always need at least one more level of logic. It's the exceptions: I come home early, take PTO, etc. that break "dumb" smart homes.