-
Notifications
You must be signed in to change notification settings - Fork 3
Home Assistant
This article looks specifically at the home automation software called Home Assistant (HA or Hass).
See also:
-
https://github.com/artmg/MuGammaPi/wiki/home-assistant-docker
- deploying Home Assistant using Docker
-
https://github.com/artmg/MuGammaPi/wiki/home-assistant-venv
- deploying Home Assistant on Raspbian with a Python Virtual Environment
-
https://github.com/artmg/MuGammaPi/wiki/home-automation
- For other home automation software products, such as Domoticz
- For accompanying hardware products like radios
-
https://github.com/artmg/MuGammaPi/wiki/other-devices#how-to-outgrow-the-pi-hardware
- How to outgrow the Pi hardware
-
https://github.com/artmg/MuGammaPi/wiki/audio-hub
- for audio systems you can integrate and control using
-
https://github.com/artmg/MuGammaPi/wiki/Raspbian-basics
- for setting up an OS ready if you are using a Pi to host Hass
Home Assistant (HA) was started by Paulus Schoutsen in Sep 2013 when he posted his automation script, turning on the lights at sunset, onto github. In April 2018 Ubiquiti hired Paulus as a dedicated employee to work full time on HA, ostensibly to move it closer to being 'production-ready' as well as capable of integrating with its own products. Although community members might be suspicious of such a move, there is no sign of undue influence over the project by this vendor - I had been a HA user for nearly six months before I discovered the Ubiquiti link.
Paulus and the founder of Hass.IO, Pascal Vizeli, formed Nahu Casa Inc in Sep 2018 to run Home Assistant Cloud services. This is an alternative to self-hosting components on premises, especially to allow a secure ingress for webhooks and UIs. The service charge is used to host not only HAC, but also to cover the costs of hosting the community infrastructure.
Home Assistant is software that can be installed on a variety of operating systems. There are three basic modes of deployment:
- python venv - install Home Assistant on an existing device to try it out
- Docker - fully flexible containerised services
- Hass.io - dedicated device using custom OS and basic containers
Installing Home Assistant into a Python Venv (virtualisation at the python layer) is an easy and efficient way to isolate a single python application on a machine.
The method is covered in detail in the article:
https://github.com/artmg/MuGammaPi/wiki/home-assistant-venv
- you need to set up your base OS and install docker
- you have full control and full access to the OS, with a little more overhead perhaps
- add the home-assistant image to get started
- add an additional container for each additional third party features you want
- of course you have to configure all this yourself
- but you can drive this through version-controlled config files for easy restore and rollback
Despite it seeming like a lot to take in, it can actually be very quick and straightforward to get going
https://github.com/artmg/MuGammaPi/wiki/Home-Assistant-Docker
- Hass.io images are available for a quick, turnkey solution to set up a dedicated device
- they are based on HassOS
- using buildroot, this has some hardware integration built-in
- it has a basic environment for running Docker containers
- containers: home-assistant, supervisor, plus one for each add-on
- although there is an 'ssh add on' it's limited to application executables
- The Hass.io Supervisor container has it's own specific GUI
- this is how you manage it's own specific Add-ons ecosystem
- Home Assistant cannot use Hass.io add-ons
- however you can sually install the software features yourself 'the hard way'
- this is how you manage it's own specific Add-ons ecosystem
- there is no command line access to the HassOS - the supervisor GUI is the only control
- If you have a regular OS and really want to install Hass.io, you can simply follow the Generic Linux Installer instructions
This is intended as the simplest method, which also has the widest range of built-in features.
-
https://home-assistant.io/components/hassio/
- find your platform's download
- use an image burning tool
- etcher works well
- start up the device and wait '20 minutes' for iniatialisation
- perhaps on older models it might be much longer than this
- log into http://hassio.local:8123/
-
upon boot, the ethernet lights should come on within 20 seconds or so (otherwise it might be jammed at startup)
-
using the physical console (directly connected screen and keyboard) you can log on as root with no password
- this gives the Hass.io CLI
- commands such as
hw info
-
login
gives access to HassOS host- this might pause as it spins up a docker container
- you can see the complex 8 partitions
- with the boot partition /mnt/boot/config.txt
- the host has limited features, enough to host the docker containers
-
Hass.io Add-ons:
-
Configurator
- Set password, Save and Start
- browse to http://hassio.local:3218
- add panel_iframe in config
- reload config
- SSH Server
-
Configurator
-
Enable Host SSH
- import
authorized_keys
file from CONFIG usb volume (Hass.io / System) - see https://developers.home-assistant.io/docs/en/hassio_debugging.html
- import
Although the simplicity and container separation is good, it did give me problems with DNS resolution in my network.
Inside containers cannot resolve!
- check in hassos ( -p 22222 ) vs ssh container
- cat /etc/resolv.conf
- vi /etc/docker/key.json
- "dns":"192.168.1.254", before "crv" after {
- echo '{"dns": ["192.168.1.254", "192.168.1.2"]}' > /etc/docker/daemon.json
- diabling DHCPv6 on router
- setting static IP
vi /etc/NetworkManager/system-connections/default
[ipv4]
method=manual
address=192.168.1.28/24,192.168.1.254
dns=192.168.1.254
According to Hass.io devs this is a 'docker issue', so perhaps I'll leave Hass.io alone for now, and come back and try again in a few months. In the meantime, plain old Home Assistant works well for me.
Your overall steps in building out your configuration may involve:
- Configuration tweaks:
- Begin editing entities in web UI
- Check the dev_state to see the settings
- move important settings into configuration files
Environments:
- set up a 'dev' instance in a virtual machine (e.g. Lubuntu on Virtualbox)
- save your config changes to GitLab (private repos for free)
- when tested, pull this config to your Live device
Although your main means of controlling the HA config is via the configuration.yaml file and its subordinates, there is also some data from the GUI that goes into other places:
- .storage subfolder
- auth
- user accounts for GUI
- auth_provider.homeassistant
- hashed passwords for GUI users
- core.entity_registry
- discovered devices that have had custom attributes entered in the GUI
- e.g. entity_id renamed to something more friendly
- auth
- home-assistant_v2.db
- this is generally the historical data recored
see https://github.com/artmg/lubuild/blob/master/help/use/git-source-control.md#workflow-examples
- home assistant automatically creates a default 'starting' configuration file-set on first run
- ensure you are in the folder containing these
- follow process for creating a new repo
- use the following for the initial load
tee -a .gitignore << EOF!
/secrets.yaml
/home-assistant_v2.db*
/home-assistant.log
/.uuid
/.storage
/.HA_VERSION
EOF!
Custom Components are Python projects that can be called
as components in your configuration.yaml
file to extend core features.
If you are trying to use custom components manually then you will
need to put them into the custom_components
subfolder of your config.
You will find insight into the structure in
https://community.home-assistant.io/t/how-to-package-custom-components/107475/10
To enable the component simply add a section into your configuration yaml
component_name:
but beware as they are not supported - you might want to keep them in a separate section of your config file so you can easily disable them if you want to diagnose an issue with the supported core of HA.
This is a Community Store allowing you to browse, install and update custom components from github
sudo -u homeassistant -H -s
HACS_DIR=/home/homeassistant/.homeassistant/custom_components
mkdir $HACS_DIR
cd $HACS_DIR
- download and install
- https://custom-components.github.io/hacs/installation/terminal/
- now
exit
from HA user context
- create a minimally privleged github access token
- https://custom-components.github.io/hacs/installation/configuration/
- this just allows HACS to browse github as you, not to modify any repos
- add this to your configuration.yaml
hacs:
token:
- pull the latest config version
- restart the server
- ensure you have added firewall allow rules for github.com
NB: if you wish you can still use custom components manually without HACS (see below)
Custom Cards are extensions to the Lovelace UI system which come as JavaScript files that need to be served by the webserver.
The folder containing the HA configuration files is
considered as an alias to /config
so a ./www/
subfolder of it
will be available at the front-end.
Lovelace configuration has an internal alias to www
called local
where you must supply explicit filenames
including versions. There is also the customcards
alias,
where no version is needed.
https://github.com/kalkih/mini-media-player
sudo -u homeassistant -H -s
mkdir -p www
cd www
wget https://github.com/kalkih/mini-media-player/releases/download/v1.2.2/mini-media-player-bundle.js
- add this to your ui-lovelace.yaml
resources:
- url: /local/mini-media-player-bundle.js?v=1.2.2
type: module
Using the manual approach to custom components will keep all files within the configuration folder and its subfolders. This makes them easy to manage, even in a Docker deployment.
You may choose to leave all custom components to be stored and managed within your own git config repository. However you might consider that this constitutes unnecessary duplication and decide instead to exclude the content but reference the source as follows.
- in your configuration folder...
- add an exclusion into your
.gitignore
filewww
- build a script to pull all external components upon demand
- e.g.
www-get-components.sh
- e.g.
- add an exclusion into your
# pull external components into www subfolder of configuration directory
(
cd "$(dirname "$0")"
mkdir -p www
cd www
wget https://github.com/kalkih/mini-media-player/releases/download/v1.4.1/mini-media-player-bundle.js
)
Although this might be a little hard to get your head around at first, you can abstract entities in HA to make things easier to use. For example you can create groups of objects, so that several items can be controlled simultaneously. Or you can use automations to define behaviours, that can then be controlled via the interface or by other automations.
Here is a simple abstraction, but useful in a room with many identical lights.
https://www.home-assistant.io/components/light.group/
Note that the resulting entity ids will be based on the name
but using the typical entity ID format, lowercase, underscores for spaces, dashes and non-alphnums
e.g. Kitchen Lights
becomes light.kitchen_lights
For some background on alternatives to Light Groups with pros and cons see
- https://community.home-assistant.io/t/new-light-groups-why-migrate/46811
- https://bonani.tech/three-different-approaches-for-grouping-light-entities-in-home-assistant/
- see also different approaches in
Entity IDs for many devices are automatically generated upon discovery. Although these could be modified through the front end, which makes changes in the Entity Registry, there is another way to rename entity IDs purely through configuration:
- Using Groups that contain a single entity
Validate that nested groups are acceptable
Thanks to @Tinkerer in HA Discord channels for these suggestions
-
Overview:
- the first decision point is - is the room in use
- Then the second is - if so, how bright is it/what state should the room be in
- Then the third is - make it so!
-
"is the living room occupied"
- https://github.com/DubhAd/Home-AssistantConfig/blob/master/automation/living_room/living_room_occupied.yaml
- https://github.com/DubhAd/Home-AssistantConfig/blob/master/automation/living_room/living_room_not_occupied.yaml
- Those look purely at whether the media devices in the room are being used - regardless of whether anybody is home, guests are over, etc
-
define typical States for the room
-
choose which state is the most appropriate
- https://github.com/DubhAd/Home-AssistantConfig/blob/master/automation/living_room/living_room_day_on.yaml
- uses the light level and whether the room is occupied to map the room to one of a number of states
-
Use an automation to set the lights to the desired state
- https://github.com/DubhAd/Home-AssistantConfig/blob/master/automation/living_room/living_room_lights_day_on.yaml
- this is one of many examples
- assumes that lighting automations are enabled
A scene is a desired state that you would like to reproduce
- Example of equivalent Scene vs Script
- Scene is the end state, script is specific steps to get there reply by Robban
- From thread Difference between scene and script?
-
123 Tara says HA waits for acknowledgement from each light before transmitting the command to the next light
- Q: is that the case for both script and scene?
- However a recent PR Switch on/off all lights, and wait for the result suggests that groups should be concurrent
- using sequences to execute changes in parallel instead https://github.com/home-assistant/home-assistant/issues/17477
- You can put multiple entity IDs in a single turn_on call in a script. Just make it a list. balloob comment Nov'19
- if you are bothered, see how light states are reproduced
Scenegen to capture current settings into yaml files for Scenes
https://github.com/home-assistant/scenegen
# credit https://www.home-assistant.io/docs/ecosystem/scenegen/
git clone https://github.com/home-assistant/scenegen.git
cd scenegen
sudo pip3 install configparser
- Create a Long Lived Access Token (LLAT):
- log onto instance
- show the Hass Menu (top left)
- click on the (i) account info symbol to see your Profile
- Long-Lived Access Tokens / Create Token
- copy the token and save somewhere safe
LLAT=...
PORT=443
./scenegen.py https://server.domain.name:$PORT/api/states -k $LLAT
[certificate error]
tried:
sudo pip3 install certifi
sudo pip3 install requests[security]
requests.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",)
If you want to group items from different domains, say lights and switches, but have them work as a single group, you can use a template switch - see some examples
To permit this server to remotely operate other systems, for example rebooting or shutting them down, you first of all need to be in the context of the service that will make the remote calls, e.g.
# in a venv install
#
# HA will act in the following context,
# so become that user before creating the keys
sudo -u homeassistant -H -s
# otherwise
pushd /home/$LOCAL_SERVICE_USER
# Exit the HA user context
exit
# OR the folder
popd
example of using this
reboot_test:
alias: Reboot Test Script
sequence:
- service: shell_command.reboot_test
shell_command:
myserver_reboot: 'ssh -i /home/homeassistant/.ssh/sysmain_reboot_key sysmain@my_server'
If you use Home Assistant and have access to a console or SSH, you can check out which device is being used
# discover serial device
ls -la /dev/serial*
# or
ls -la /sys/class/tty/ | grep serial
or
ls -la /sys/class/tty/*/device/driver
If you get permissions errors in the logs, it may be because Raspbian defaults to making a console available via GPIOs.
# check whether root can use the serial device
sudo echo AT >/dev/ttyXXXX
# a Permission Denied error with sudo indicates the device is in use
You can disable this to allow your device integration full access to the port:
sudo cp /boot/cmdline.txt /boot/cmdline.txt.original
# remove option console=serial0,115200
sudo editor /boot/cmdline.txt
Using a tablet as a Home Assistant display should have been a great idea. Unfortunately the default power management of a tablet expects you to either have it in your hand, fondling it, or being left ununsed. This made the Family User Experience quite poor.
Upon many a recommendation in the HA community I tried Fully Kiosk Browser and all of a sudden the tablet display was worked as I had always dreamed it should.
The steps to setting it up were pretty simple:
- install Fully Kiosk
- Play Store
- search Fully Kiosk
- ensure you got Fully Kiosk Browser & App Lockdown by Fully Factory Kiosk Solutions
- Install
- Allow all the permissions it asks for (virtual omnipotence)
- Remote Admin - ON
- Set the password
- Play Store
Note that I chose the Remote Admin which is a PLUS (paid) option. I would rather be sitting at my PC to set it up, rather than standing by the kitchen wall. And one of my key drivers for using Fully is the Device Wakeup Automation, which is another PLUS option. And at the end of the day, 6 euros is a small price for a happy family.
Note the flip-side of the display always being ready to spring into action is an increase in power usage. This means I'm having to enable the switch controlling it's power supply almost daily instead of weekly or fortnightly.
There is a custom component that allows control of the display https://github.com/daemondazz/homeassistant-displays but if you simply want to read the battery sensor see https://community.home-assistant.io/t/fully-kiosk-browser/41675
See some example Automations for battery sensors https://community.home-assistant.io/t/charge-my-android-tablet-automatically-when-battery-goes-less-than-10-stop-charging-when-100/61296 and how to use the sensor states https://community.home-assistant.io/t/automation-based-on-value-template/126670/2
Many smart device systems are built around a proprietary gateway device. Examples include Ikea Trådfri and Philips Hue lighting systems. However as these are all simply Zigbee meshes where the gateway provides an API for application-level access, it should be feasible to avoid the gateway.
Candidates:
- ZHA
- Zigbee Home Automation
- built in and works pretty well out of the box
- Supports a variety of hardware
- https://www.home-assistant.io/components/zha/
- Zigbee2MQTT
- offloads the device code from HA to MQTT connectors
- https://github.com/Koenkk/zigbee2mqtt
- https://github.com/danielwelch/hassio-zigbee2mqtt
- ZiGate
- This is actually a hardware device https://zigate.fr/
- https://community.home-assistant.io/t/36820/281
NB: Early versions of some hardware may not be fully compatible, and may require the proprietary gateway to upgrade their firmware. https://community.home-assistant.io/t/52108/158
As I could not reset Hue bulbs with an on/off light sequence, I set up a separate python venv for huethief
#### Set up the venv and software
# install system packages if not already installed
sudo apt install -y git python3-venv
# create the folder and permission it for interactive use
sudo mkdir /srv/huethief
sudo chown pi:pi /srv/huethief
# create the venv, add the software and the python prereqs
python3 -m venv /srv/huethief
cd /srv/huethief
git clone https://github.com/vanviegen/hue-thief
cd /srv/huethief/hue-thief
source /srv/huethief/bin/activate
pip3 install wheel
pip3 install -r requirements.txt
#### run the software
cd /srv/huethief/hue-thief
source /srv/huethief/bin/activate
sudo systemctl stop home-assistant@homeassistant
# power on the devices and immediately run...
python3 hue-thief.py /dev/ttyAMA0
If have installed docker, then there is a really easy to use Dockerfile at https://github.com/vanviegen/hue-thief#docker-image
- Tried with Tuya app
- device would not connect to wifi
- Tried with eWeLink app
- device recognised and connected to wifi
- despite plenty of allow rules, could not get it functioning
- anyhow the solution with the stock firmware requires an unsupported custom component
so...
There is extremely detailed documentation at https://github.com/arendst/Sonoff-Tasmota/wiki/Prerequisites but here is an overview
If you
-
backing up the old firmware
-
flashing
-
connecting to the device
- you can tinker to use a Raspberry Pi
- or you can get a dedicated USB-TTL Serial adapter device
- normally using FTDI FT232RL
- https://www.amazon.co.uk/gp/product/B07BBPX8B8/ for under a tenner
- Test your device
- echo hello >>/dev/cu.usbserial-AXXXXXXXX
-
Software to perform the flash
- NodeMCU-py with wxPython is cross-platform
- prebuilt for Win/OSX - compile it for Linux
- https://github.com/marcelstoer/nodemcu-pyflasher/releases
-
Get the release
- check which firmware variant you need
- get the file
-
Flash
- Match the pins on the USB-TTL device (NB: TX->RX both ways)
- prepare the options in the software
- Ensure the breakout pins have firm electrical contact (ideally soldered)
- Hold the button during power up - you can probably keep it held
- Connect the USB adapter to the computer
- wait a few seconds
- Click the Flash NodeMCU button
-
Configure
- power on the switch
- connect to the wifi point sonoff-xxxx
- browse to http://192.168.4.1/
- enter the wireless config and save
-
Restart device so it appears on designated network
-
Ready to configure MQTT
-
Alternative firmware aimed at arduino flashing
- KmanSonoff - https://github.com/KmanOz/KmanSonoff - appears to use MQTT
- Homie-ESP8266 - https://github.com/homieiot/homie-esp8266
- Sonoff-MQTT-OTA-Arduino - https://github.com/arendst/Sonoff-MQTT-OTA-Arduino - became Tasmota
See also firmware listed at https://www.home-assistant.io/docs/mqtt/discovery/#support-by-third-party-tools
- Door strikes
- How to design in a linear actuator
Sometimes issues arise simply because of yaml file formatting. If you're still new-ish to yaml then this complete idoit's guide will help a lot, and even seasoned yaml-users might get something from this whirlwind explanation of yaml syntax.
- check the configuration.yaml has a valid automation section
- it could use an automations.yaml file
- or an automations folder with include dir merge list
- ensure that the automation is registered at startup
- set the automation log level to INFO
- https://www.home-assistant.io/docs/automation/troubleshooting/
- If you are using templates for values try them out manually
- Developer Tools / Template
- Paste your template into the editor to see the resulting value
This example is for venv
sudo -u homeassistant -H -s
source /srv/homeassistant/bin/activate
# is this needed?
export EZSP_DEVICE=/dev/ttyAMA0
bellows devices -D /srv/Data/zigbee.db
# or
bellows -d /dev/null devices -D /srv/Data/zigbee.db
If you have issues with ZHA devices then remove them using the Configuration / ZHA pane. Dropping down the list and choosing the name is now simpler than the old method of using the service interface (dev-service) and selecting:
zha.remove {"ieee_addr": "...."}
It is now easier as you can simply Look at the object in the Entity Registry, although the 'Cog' settings menu still tells you the last few bytes.
If you still get errors you might try manual removal. Get the IEEE address from the GUI and then follow this.
WARNING: this risks messing up your database, especially if you have not already removed the item as well as you can via the interface. Be Warned! And make sure you hang onto the backup made below.
# install sqlite3 if you don't have it yet
# move to the folder containing your zigbee.db,
# e.g. where your configuration.yaml is
cd path
ieee_addr=00:15:8d:00:02:9a:78:82
sql_db=zigbee.db
# back up the database just in case
sudo cp $sql_db{,.`date +%y%m%d.%H%M%S`}
sudo sqlite3 $sql_db "delete from attributes where ieee=\""$ieee_addr"\""
sudo sqlite3 $sql_db "delete from devices where ieee=\""$ieee_addr"\""
sudo sqlite3 $sql_db "delete from output_clusters where ieee=\""$ieee_addr"\""
sudo sqlite3 $sql_db "delete from clusters where ieee=\""$ieee_addr"\""
sudo sqlite3 $sql_db "delete from endpoints where ieee=\""$ieee_addr"\""
Once you have configured your Zigbee devices, you might consider backing up the database file specified in the zha section of configuration.yaml
# as main (admin) user:
# You should quiesce your services
# to maintain db integrity during the copy
sudo service home-assistant@homeassistant stop
cd $HOME
STAMP=`date +%y%m%d-%H%M%S`
mkdir backup.$STAMP
cd backup.$STAMP
mkdir data
cp -p ../hadata/* data/
mkdir storage
sudo cp -p ../haconf/.storage/* storage/
cd ..
sudo service home-assistant@homeassistant start