Home Assistant

When using dietpi OS, Home Assistant can be installed directly by using the dietpi-software package, selecting Home Assistant (at the bottom of the list).
Installation is made automatically and Home Assistant files are accessible at /home/homeassistant/.homeassistant.

To Upgrade Home Assistant, run the following command in the folder /home/homeassistant

./homeassistant-update.sh 

To Downgrade Home Assistant, run the command

sudo pip3 install homeassistant==0.110.3 (for example)

When installing under Raspi OS, follow this procedure to install Home Assistant in a Virtual Environment:

This first command must be run from ‘root’ or with sudo command.

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git

Then change user to homeassistant.

sudo -u homeassistant -H -s

And run the following commands: (for later version of python replace 3.8.6 by the relevant version (ex 3.9.0))

curl https://pyenv.run | bash
pyenv install 3.8.6 (this will take a rather long time
pyenv rehash

At the end of python installation, the system will prompt to add 3 lines to the homeassistant .bashrc file. Copy the 3 lines and paste them at the end of the file (nano ~/.bashrc).
Then run the following commands: (for creating a virtual environment named ‘homeassistant’)

pyenv virtualenv 3.8.6 homeassistant (create the virtual environment)
pyenv activate homeassistant (activate the virtual environment)
pip install homeassistant (install home assistant)
hass (to launch homeassistant)

Once the system has started properly, stop it (CTRL+c).

Finally create a service file which will look like the following:

sudo nano /etc/systemd/system/home-assistant.service

and copy the following block into it. Then save it (CTRL+x -y).

[Unit]
Description=Home Assistant
After=network-online.target
[Service]
Type=simple
User=homeassistant
WorkingDirectory=/home/homeassistant/.homeassistant
ExecStart=/home/homeassistant/.pyenv/versions/homeassistant/bin/hass -c "/home/homeassistant/.homeassistant"

[Install]
WantedBy=multi-user.target

Then, run the following commands:

sudo systemctl enable home-assistant.service

sudo systemctl start home-assistant.service

To upgrade Home Assistant run the following commands:


- sudo su - homeassistant
- pyenv activate homeassistant
- pip install -U homeassistant
- exit
- sudo service homeassistant restart

To verify which version of Home Assistant is running, run the following commands:

- sudo su - homeassistant
- pyenv activate homeassistant
- pip freeze | grep homeassistant

In case there is an issue with the database (home-assistant_v2.db), it can be repaired by doing the following:

Stop Home Assistant:

sudo systemctl stop home-assistant.service

And run the following commands to repair and replace the database:

sudo slqite3 ./home-assistant_v2.db ".dump" | sudo sqlite3 ./home-assistant_v2.db.fix

sudo mv ./home-assistant_v2.db ./home-assistant_v2.db.broken

sudo mv ./home-assistant_v2.db.fix ./home-assistant_v2.db

Restart Home Assistant:

sudo systemctl start home-assistant.service

Check that the logs do not contain any more errors related to the database