the.nerdy.talk

Crafting Bytes for Better Lives

Home Assistant Core on Raspberry Pi Setup Guide

Published on 12/27/2021

Home Assistant Core on Raspberry Pi Setup Guide

Home Assistant is a very powerful, open source, community-driven smart home management platform. It can be run as a containerized solution, from a standalone device with pre-built images or as a software installation on a root-access linux device. This guide provides an introduction to the system and how to set up Home Assistant on a Raspberry Pi 3 Model B+ (Home Assistant Core method).

Prerequisites

To follow this guide, you will need:

  • Raspberry Pi 3 Model B+ (1.4GHz 64-bit quad-core processor, 1GB RAM, wireless LAN, PoE supported)
  • 5V 2.5A USB charger
  • microUSB charging cable
  • 16GB+ microSD card (Class 10 or faster)
  • LAN or wireless LAN
  • Internet connection
  • microSD card reader
  • Flashing software, i.e., balenaEtcher

Setup of Raspberry Pi OS

First, install Raspberry Pi OS onto your device. Download the latest RasPiOS 'Bullseye' for armhf architecture (32-bit) in the lite version from Raspberry Pi's official website.

After downloading, extract the zip file and verify its checksum. Example for macOS:

user $ shasum Downloads/2021-10-30-raspios-bullseye-armhf-lite.img
ef38d8556924a3c758c00dcd10d71034884409c2  Downloads/2021-10-30-raspios-bullseye-armhf-lite.img

Insert your microSD card and use balenaEtcher to flash the image.

After flashing, enable SSH access by creating an empty ssh file on the boot partition:

user $ touch /Volumes/boot/ssh

Insert the microSD card into the Raspberry Pi, connect it to LAN, and power it up. Find the IP address through your router or using nmap. Connect via SSH:

user $ ssh pi@192.168.1.126
# default password is 'raspberry'
pi $ sudo raspi-config
# select 5 Localization Options
# select L4 WLAN Country
# select your country code (i.e., DE for Germany)

Update the OS and install required packages:

pi $ sudo apt-get update
pi $ sudo apt-get dist-upgrade
pi $ sudo reboot

# After reboot, reconnect via SSH
pi $ sudo apt-get autoclean
pi $ sudo apt-get autoremove
pi $ sudo apt-get install liblzma-dev libncurses5-dev libreadline-dev libsqlite3-dev libssl-dev libffi-dev tk-dev libbz2-dev libgdbm-dev libgdbm-compat-dev
pi $ sudo apt-get install rustc
pi $ sudo apt-get install libjpeg-dev
pi $ sudo apt-get install ffmpeg

Installation of Python 3.11

Download and compile Python 3.11 from source:

pi $ wget https://www.python.org/ftp/python/3.11.3/Python-3.11.3.tgz
pi $ tar zxvf Python-3.11.3.tgz
pi $ cd Python-3.11.3
pi $ ./configure --enable-optimizations
pi $ make -j -l 4
pi $ sudo make altinstall

Installation of Home Assistant

Create a dedicated user and set up the virtual environment:

pi $ sudo useradd -rm homeassistant -G dialout,gpio,i2c
pi $ sudo mkdir /srv/homeassistant
pi $ sudo chown homeassistant:homeassistant /srv/homeassistant
pi $ sudo -u homeassistant -H -s
homeassistant $ cd /srv/homeassistant
homeassistant $ python3.11 -m venv .
homeassistant $ source bin/activate
(homeassistant) homeassistant $ python3 -m pip install wheel
(homeassistant) homeassistant $ pip3 install homeassistant

Getting Started with Home Assistant

Launch Home Assistant for the first time:

(homeassistant) homeassistant $ hass

For enhanced logging, edit the configuration file in a separate SSH session:

pi $ sudo nano /home/homeassistant/.homeassistant/configuration.yaml

Add the following to enable detailed logging:

logger:
  default: info

Access Home Assistant through your web browser at http://your_ip:8123 and complete the initial setup.

Configuring Start on Boot

Create a systemd service file for automatic startup:

pi $ sudo nano /etc/systemd/system/homeassistant@homeassistant.service

Add the following configuration:

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

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

[Install]
WantedBy=multi-user.target

Enable and start the service:

pi $ sudo systemctl daemon-reload
pi $ sudo systemctl enable homeassistant@homeassistant
pi $ sudo systemctl start homeassistant@homeassistant
pi $ sudo systemctl status homeassistant@homeassistant

Updating Home Assistant Core

To update Home Assistant:

pi $ sudo systemctl stop homeassistant@homeassistant
pi $ sudo -u homeassistant -H -s
homeassistant $ source /srv/homeassistant/bin/activate
(homeassistant) homeassistant $ pip3 install --upgrade homeassistant
(homeassistant) homeassistant $ exit
pi $ sudo systemctl start homeassistant@homeassistant

Note: If Python upgrades are required, e.g. switching from Python3.10 to Python3.11, delete the /srv/homeassistant folder and repeat the installation process. Your configuration and database in /home/homeassistant/.homeassistant/ will be preserved.

Future Enhancements

Planned future additions to this guide:

  • Adding SSL certificate for HTTPS access (required for features using callback authorization)
  • Adding mDNS service for easier access (e.g., http://smarthome.local:8123)
Last built: December 24, 2024
Built with SvelteKit + Tailwind