Home Assistant

https://www.home-assistant.io/, the docs https://www.home-assistant.io/docs/ and the forum https://community.home-assistant.io/

When having dedicated Raspberry device for Home Assistant follow guides as https://www.home-assistant.io/installation/raspberrypi/ and get directly a Raspberry Home Assistant SD card image. Using this way the Home Assistant Operating System gets installed.

If the Raspberry is serving primary for other purposes than Home Assistant or if it is already there and running Home Assistant can be installed as package https://www.home-assistant.io/installation/ and therefore without Home Assistant OS:

It is recommended to run Home Assistant Core in a python virtual environment. This way dependency conflicts to the OS (as Raspberry OS) will be reduced. However this will not eliminate python version conflicts as if Home Assistant wants a newer(or older) python version as the OS. Debian uses rather old python versions

Install Home Assistant in a python virtual environment

Install all that is required:

sudo apt install python3 python3-dev python3-venv python3-pip libffi-dev libssl-dev autoconf build-essential libsqlite3-dev libjpeg-dev zlib1g-dev bluez -y

Create the home assistant user:

sudo useradd -rm homeassistant -G dialout,gpio,i2c

Important

This will not set a password for home assistant and does not enable home assistant to run the sudo command. It is not necessary to add the sudo rights to the home assistant user and it is recommended for cyber security reasons not to do it. If it is still required to work as home assistant do sudo -u homeassistant -H -s

Create a Python Virtual Environment

cd /srv

sudo mkdir homeassistant

give this directory ownership and group to home assistant

sudo chown homeassistant:homeassistant homeassistant

Verify that the ownership and group in this directory are for the home assistant user. ls -l

cd homeassistant

Change to the user home assistant sudo -u homeassistant -H -s

Now create the python virtual environment in this directory python3 -m venv .

source bin/activate

Install home assistant in the virtual python environment

pip install wheel

pip install homeassistant

Start home assistant

hass

Using a desktop PC access it http://<raspberry IP>:8123

to leave the virtual python environment Ctrl+Z and

deactivate

To have it started as systemd service do as sudo capable user:

Create the file sudo touch /etc/systemd/system/home-assistant@homeassistant.service

Put in:

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

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

[Install]
WantedBy=multi-user.target

Have it known and started

sudo systemctl --system daemon-reload

sudo systemctl enable home-assistant@homeassistant

sudo systemctl start home-assistant@homeassistant

Update Home Assistant in the virtual environment https://www.home-assistant.io/common-tasks/core/ might not end up with the desired version since Debian uses old python versions that the newest Home Assistant versions might not support.

sudo systemctl stop home-assistant@homeassistant

sudo -u homeassistant -H -s

source /srv/homeassistant/bin/activate

pip install --upgrade homeassistant

deactivate

sudo systemctl start home-assistant@homeassistant

Run Home Assistant

Once installed, it can be accessed as http://<ip address>:8123

There is also an app available for mobile devices

Some terms to know

  • Integrations are ways to communicate to devices or protocols

  • Entity ID are identifiers of states to be exchanged. States are values as on or off, or a numeric value and can be seen as data. State changes can be used in triggers as events to be processed.

  • Helpers can take their data from everywhere including Home Assistant User Interface. So helpers do not need hardware. Helpers can count

  • Dashboards can be created holding Cards and the cards hold Entities

  • Automations is the logic added to Home Assistant that connects input and outputs or events and actions, it is what Home Assistant does automatically. Automations have triggers.

  • Scripts are as automations but do not have triggers. Scripts have therefore be used in automations.

  • Scenes allow to send actions to multiple devices instead of sending actions to multiple times to individual devices

  • Blueprints are automation templates and scripts to not need ti start from zero

  • Tags are NFC or QR tags that can be used as triggers

  • Sensors are entities that provide a value

  • A switch can provide its value but can also be toggled, it is therefore bi-directional

  • A light is like a switch but has additional functionality, as brightness and RGB color

/home/homeassistant/.homeassistant hold the data including yaml files

Add a shelly to home assistant

Shelly's https://www.shelly.com/ appear as WLAN servers creating their own network and have ip addresses as http://192.168.33.1

The first step is using a WLAN capable PC and check for available WLAN networks as SSID: Shelly1PMMiniG3-<number> . Then connect to the network. After this open a browser with http://192.168.33.1 and the Shelly's device web page appears

Now configure the WLAN settings so it uses the same network as the home assistant device. A static address can be set to have a defined IP address. Then enable the network to have now the Shelly accessible to home assistant and it stops blinking its red LED. It however stays also in its SSID: Shelly1PMMiniG3-<number> using http://192.168.33.1. Everybody could connect to it, so it is a cyber security issue, set a user name and password and to be more safe disable the shelly's access point feature. This however has the risk that a factory reset might be required in the future, if password or user names will be lost or if the WLAN home network changes.

Important

Enable access point when modifying something on the WLAN home network. If it goes wrong then the Shelly can be accessed otherwise a factory reset is required.

Factory resets could be done by pressing the buttons on the shelly's for longer than 10s or if no button is present by multiple quick power on off cycles.

Since it is now connected to the Internet it is time to check for firmware updates.

The Shelly has lot of functions and features to configure and operate itself without using home assistant. If using home assistant, it needs therefore be taken care that no interference between the home assistant and the local Shelly configuration occurs.

The Shelly and home assistant must communicate themselves using one out of the many protocols available. However both must use and understand the same protocol.

Many Shelly's are directly found by Home Assistant and will be directly integrated in its GUI.

However some Shelly's as Generation 3 are not detected yet and can be used by defining their protocol. One common protocol (not just for Shelly's) is MQTT. If not having Home assistant OS or Supervised then a MQTT broker is missing so sudo apt install mosquitto mosquitto-clients and configure mosquitto.

configuration.yaml

The GUI has its limits for configuration especially when integrations are missing or generic communication protocols are used. In this case the configuration file is usually in /home/homeassistant/.homeassistant/configuration.yaml needs to be edited.

If working in a console work as user homeassistant:

sudo -u homeassistant -H -s

In Home Assistant Gui Developer Tools CHECK CONFIGURATION https://www.home-assistant.io/common-tasks/core/#configuration-check or source /srv/homeassistant/bin/activate and hass --script check_config

For new items a restart is necessary after that modifications need just a YAML reload, click an green tick should appear.

To get a modular configuration configuration.yaml can includes other yaml files. To move out the mqtt stuff in a separate mqtt.yaml file add to configuration.yaml

mqtt: !include mqtt.yaml

then create the mqtt.yaml without repeating mqtt again:

sensor:
  - name: "raspi_soc_temperature"
    state_topic: "raspi/soc/temperature"

Adding custom MQTT sensor

It is assumed that a MQTT broker as mosquitto is running and configured in Home Assistant running.

MQTT and subscribe values

Test first that MQTT is able to communicate.

Create a script that reads and publishes the raspberries SoC temperature:

#! /bin/bash
mosquitto_pub -t "raspi/soc/temperature" -m "$(/usr/bin/vcgencmd measure_temp | sed -E "s/temp=([0-9]+\.[0-9]+).*/\1/")" -u <username> -P <password>

sed is used to just get the numerical value. This script could be added to cron

Check in an other console that subscribing is working:

mosquitto_sub -t "raspi/soc/temperature" -u <username> -P <password>

Now it is time to edit /home/homeassistant/.homeassistant/configuration.yaml

mqtt:
  sensor:
    - name: "raspi_soc_temperature"
      state_topic: "raspi/soc/temperature"

In Home Assistant -> Developer Tools -> CHECK CONFIGURATION and reload the YAML

A new entity sensor.raspi_soc_temperature should now appear and be used as in a gauge card.

MQTT and subscribe Json

The payload might be json instead of single data. For every data structured in Json a separate MQTT entry must be done, some data is nested so the path needs to be included. The entries would look as:

sensor:
  - name: "raspi_soc_temperature"
    state_topic: "raspi/soc/temperature"
  - name: "raspi_manager_battery_soc"
    state_topic: "raspi/manager"
    value_template: "{{ value_json['battery' ].soc }}"
  - name: "raspi_manager_power"
    state_topic: "raspi/manager"
    value_template: "{{ value_json.power }}"

MQTT and send to Shelly Mini1PMG3

Shelly Mini1PMG3 is not yet directly supported in Home Assistant and has a rather complicated payload to be turned on. Everything is therefore be packed into payload_on and payload_off

switch:
  - name: "mini1pmg3"
    command_topic: "shelly1pmminig3-5432044f7414/rpc"
    payload_on:  '{"id": 1, "src": "hass", "method":"Switch.Set", "params":{"id":0,"on":true}}'
    payload_off: '{"id": 1, "src": "hass", "method":"Switch.Set", "params":{"id":0,"on":false}}'
    retain: false

There is no state_topic and value_template therefore it works just in the publishing direction. To have mosquitto know the payload in case the Shelly Mini1PMG3 is powered off and will be powered on later retain: true must be set.

To have a switch bidirectional, a state_topic must be added, since the switch status is inside json a value_template is required and since the state data is different from command data, state_on and state_off is defined. To work it well during power on an off retain is set to true:

switch:
  - name: "mini1pmg3"
    command_topic: "shelly1pmminig3-5432044f7414/rpc"
    payload_on:  '{"id": 1, "src": "hass", "method":"Switch.Set", "params":{"id":0,"on":true}}'
    payload_off: '{"id": 1, "src": "hass", "method":"Switch.Set", "params":{"id":0,"on":false}}'
    retain: true
    state_topic: "shelly1pmminig3-5432044f7414/status/switch:0"
    value_template: "{{ value_json.output }}"
    state_on: true
    state_off: false

Final settings

Give it a unique id so the gui editor can find it. It is recommended to put a MAC address in the identifier to assure it stays unique

unique_id: "raspi4_soc_temp-e45f016dc555"

Give a unit to the value

unit_of_measurement: "W"

Linurs startpage