Install Home Assistant on RaspberryPi
First Things First… what’s that?
In brief (very brief):
Home Assistant will track the state of all the devices in your home
Control all your devices from a single, mobile-friendly, interface
Set up advanced rules to control devices and bring your home alive.
… and what about RaspberryPi?
A small and affordable computer
To say a little bit more: it’s a single-board computer that can fit in your hand and has (the current latest model):
Raspberry Pi 3 Model B+
- 1.4GHz 64-bit quad-core A53 (ARMv8) processor
- 1GB LPDDR2 SDRAM
- Gigabit Ethernet, 2.4GHz and 5GHz 802.11b/g/n/ac Wi-Fi
- Bluetooth 4.2, Bluetooth Low Energy (BLE)
- 40-pin GPIO header
- HDMI
- 3.5mm analogue audio-video jack
- 4x USB 2.0
and more
At the very minimum you just need to put it in a micro SD card with the OS and power it up with a compatible power supply!
Then if you don’t want to connect to it purely through SSH and remote connections you can plug it to a monitor with and HDMI cable and add a mouse and keyboard using the available USB ports
What you need
In order to follow this tutorial you need:
- a RaspberryPi – I will use a Raspberry Pi 3 Model B (and yes, a power supply!)
- micro SD card – I will use a 16GB (the one I have available right now :P)
- an ethernet cable… or you could avoid this if you setup the wifi with a monitor and keyboard… or you can even avoid all of this with an automatic WiFi setup (but I’ll cover this step somewhere else to avoid digressions)
Install and access Raspbian
Grab the latest Raspbian Lite image.
You can get it from here: https://www.raspberrypi.org/downloads/raspbian/
Uncompress the image and copy it on the SD card. You can use Etcher for this: https://etcher.io/ and as a reference, this is how it looks like:
You can then put the SD card into your RaspberryPi and boot it up to setup the system.
If you are using Ethernet you are good to go, otherwise with a monitor and a keyboard log in into your raspberry and setup the WiFi.
Once your RaspberryPi is on the local network you can SSH to it… and this is what I’ll do from now on.
To connect to it from another computer (including the one you just used to create the SDCard) you can open a shell and execute:
1 |
ssh pi@raspberrypi |
you can use the IP address (if you know it) if you prefer.
Side Note
I used another tool (you can check it out here: ) to create the bootable SDCard and automatically configure the WiFi. As part of the initial boot I also configured to change the host name from the default “raspberrypi” to “homeassistant” and change the default passsword for “pi” from “raspberry” to… :D well I’ll probably skip this detail :P.
Anyway, this is why you’ll see that the host name is “homeassistant” in my case. This means that the above step is for me:
1 |
ssh pi@homeassistant |
When you execute this command you could see something like this:
1 2 3 |
The authenticity of host '192.168.1.17 (192.168.1.17)' can't be established. ECDSA key fingerprint is SHA256:ufp0kBmSz0MysXcR5+qjHJaPrDUm1ifU0AT8sgtRAQ0. Are you sure you want to continue connecting (yes/no)? yes |
Just type “yes” as you see above, you will be requested to enter the password for your “pi” user and you should finally find yourself logged into the RaspberryPi system:
1 2 3 4 5 6 7 8 9 10 11 12 |
Warning: Permanently added '192.168.1.17' (ECDSA) to the list of known hosts. pi@192.168.1.17's password: Linux homeassistant 4.9.80-v7+ #1098 SMP Fri Mar 9 19:11:42 GMT 2018 armv7l The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Sat Sep 15 08:52:02 2018 pi@homeassistant:~ $ |
Troubleshooting
If you struggle to connect to your raspberry without the IP but you don’t know the IP there are two quick ways to figure it out:
Method one
Log into your router and try to locate it here, looking into the list of connected devices.
Every router is different so there is no way for me to point exactly where this information is in your router.
Method Two
In this case connect a monitor and a keyboard to your RasberryPi and login directly on the machine.
Then type “ifconfig” and locate the ip there… or use the grep command to isolate the information:
1 2 3 |
pi@homeassistant:~ $ ifconfig | grep "inet " inet 127.0.0.1 netmask 255.0.0.0 inet 192.168.1.17 netmask 255.255.255.0 broadcast 192.168.1.255 |
The highlighted line shows how the IP in my case is “192.168.1.17”
Install Home Assistant
Install python virtual environment
1 2 |
sudo apt-get update sudo apt-get install python3-venv |
Create a new HomeAssistant user
1 |
sudo useradd -rm homeassistant -G dialout,gpio |
here the official guide suggests to create a folder for the installation of the home assistant and this is what we’ll do as well.
1 2 |
sudo mkdir /srv/homeassistant sudo chown homeassistant:homeassistant /srv/homeassistant |
Create a virtual environment for HomeAssistant
Switch to the homeassistant account:
1 |
sudo -u homeassistant -H -s |
At this point you should see the change of the prompt:
1 2 |
pi@homeassistant:~ $ sudo -u homeassistant -H -s homeassistant@homeassistant:/home/pi $ |
and create the virtual environment in the folder we previously created:
1 |
python3 -m venv /srv/homeassistant/ |
Install HomeAssistant
We want to install the python home assistant package only for the homeassistant virtual environment.
In order to do so we need first to switch to this virtual environment for python, and this is done executing the “source” command and pointing to the “activate” file locate into the “bin” folder of the virtual environment folder structure we previously created with the python3 “venv” command. Essentially:
1 |
source /srv/homeassistant/bin/activate |
You should see a similar result:
1 2 |
homeassistant@homeassistant:/home/pi $ source /srv/homeassistant/bin/activate (homeassistant) homeassistant@homeassistant:/home/pi $ |
Note the “(homeassistant)” prepended to the prompt.
We can now install some required packages:
1 |
python3 -m pip install wheel |
and finally the Home Assistant!
1 |
pip3 install homeassistant |
Note – Deactivate the virtual environment
You can exit the virtual environment anytime using the command:
1 |
deactivate |
1 2 |
(homeassistant) homeassistant@homeassistant:/home/pi $ deactivate homeassistant@homeassistant:/home/pi $ |
Run it
We can the start Home Assistant typing:
1 |
hass |
from within the homeassistant virtual environment.
This will complete the installation.
It will also create the “.homeassistant” configuration directory in the “/home/homeassistant” directory (see below) and install any basic dependencies:
1 2 |
(homeassistant) homeassistant@homeassistant:/home/pi $ hass Config directory: /home/homeassistant/.homeassistant |
At this point you can reach your home assistant from a browser, at the same address of your RaspberryPi, and port “8123”:
1 |
http://homeassistant:8123 |
Troubleshooting
If the DNS fails, you can try to append the “.local” postfix: http://homeassistant.local:8123
(read why here: https://en.wikipedia.org/wiki/.local)
What it looks like
The first time you’ll see a screen that will ask you to add new credentials and from that moment on every time you’ll land on that page it will ask you for the same credentials to login into the system. This is how the login looks like:
and once you log in it should be similar to this:
Setup it to Autostart
This step is required (and I would say highly recommended!) in order to automatically restart your Home Assistant every time the system restarts.
There are several method that you can find here: https://www.home-assistant.io/docs/autostart/
but I will cover the one for this system that is the “systemd” option.
You can check that this is supported by your system executing:
1 |
ps -p 1 -o comm= |
and confirming that the output of this command is “systemd“:
1 2 |
homeassistant@homeassistant:/home/pi $ ps -p 1 -o comm= systemd |
Create the service
If you followed the steps above all you need to do is create a special file “/etc/systemd/system/home-assistant@homeassistant.service” that is nothing else than a service that will run as the user homeassistant (the one that owns the virtual environment and the hass installation).
This requires root privileges, therefore if you are still using the homeassistant user, you’ll need to switch back to pi with the “exit” command:
1 2 3 |
homeassistant@homeassistant:/home/pi $ exit exit pi@homeassistant:~ $ |
at this point we can create the file I mentioned, using the command:
1 |
sudo nano -w /etc/systemd/system/home-assistant@homeassistant.service |
this will open the “nano” editor and you need just to type this in it:
1 2 3 4 5 6 7 8 9 10 11 |
[Unit] Description=Home Assistant After=network-online.target [Service] Type=simple User=%i ExecStart=/srv/homeassistant/bin/hass -c "/home/homeassistant/.homeassistant" --log-rotate-days 5 [Install] WantedBy=multi-user.target |
I highlighted the line that you could need to change if you didn’t follow the tutorial or if your installation differs for any other reason.
The “ExecStart” needs to point to the “hass” file and the “-c” is used to pass the information about the location of the home assistant configuration folder:
-c path_to_config_dir, –config path_to_config_dir
Directory that contains the Home Assistant configuration
I also added an extra parameter ” –log-rotate-days 5″ that will do what it says on the tin and it’s something good to have to avoid that the log file will grow indefinitely, unless you’ll restart the service and at that point it will be wiped…
Save with “CTRL+O” (and enter to confirm the filename) and exit the editor with “CTRL+X”
Reload systemd
We can let systemd know about the new configuration we just created just restarting it
1 |
sudo systemctl --system daemon-reload |
Enable the service
We can enable the service to automatically star it with:
1 |
sudo systemctl enable home-assistant@homeassistant |
To disable it is the same command but of course with “disable” instead of “enable”
Start it now
Once again, same command but with “start”
1 |
sudo systemctl start home-assistant@homeassistant |
You can use also “stop” and “restart” to do what it says on the tin
At last to check its status you can type:
1 |
sudo systemctl status home-assistant@homeassistant |
and see something like this:
1 2 3 4 5 6 7 8 9 10 |
pi@homeassistant:~ $ sudo systemctl status home-assistant@homeassistant ● home-assistant@homeassistant.service - Home Assistant Loaded: loaded (/etc/systemd/system/home-assistant@homeassistant.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2018-09-15 09:37:47 UTC; 28min ago Main PID: 369 (hass) CGroup: /system.slice/system-home\x2dassistant.slice/home-assistant@homeassistant.service └─369 /srv/homeassistant/bin/python3 /srv/homeassistant/bin/hass -c /home/homeassistant/.homeassistant Sep 15 09:37:59 homeassistant hass[369]: 2018-09-15 09:37:59 INFO (MainThread) [homeassistant.core] Timer:starting [... more here ...] |
(press “q” to exit)
To get the logging (and filter to see only the errors for example) use:
1 |
sudo journalctl -f -u home-assistant@homeassistant | grep -i 'error' |
(remove the “| grep -i ‘error’” section if you don’t want any filter)
Conclusion
Here you go. We have now a fresh installation of Home Assistant running with the user account “homeassistant” (that is safer given that this one doesn’t have root priviledges on the Raspbian system) and in a dedicated virtual environment (so isolated from any other python requirements you may have outside).
We are good to go and start exploring and expanding this system and I’ll cover several other posts related to this so stay tuned.
Please Comment, and share if you have questions or found this useful.
Thanks!
nice guide I’ll tried this night but the HA settings page is very poor respect the native HA docker standard setup.
It misses Backup, add-ons and much more other stuffs.
is there a way to merge them?
thanks