Set Up Home Assistant With A Raspberry Pi
Set Up Home Assistant With A Raspberry Pi
Pi
Created by Richard Albritton
https://learn.adafruit.com/set-up-home-assistant-with-a-raspberry-pi
Intro 3
But Why? 4
HASSio Setup 8
MQTT Setup 10
• What it MQTT?
• Install Mosquitto Broker
Configurator 14
Node Red 18
• Add Node-RED
• Setup Node-RED MQTT Client
Support for over 1400 devices means that just about every type of Smart Home
device will likely work from consumer products like Hue, Lifx, Google Home, Alexa,
Ecobee, Z-Wave, WeMo, IKEA Trådfri, and so many DIY devices that use things like
Python or MQTT. Most IOT projects that use the ESP8266 or ESP32 can be tied into
this system as easily as connecting to Adafruit.io.
This guide will walk you though setting up the Home Assistant server onto a
Raspberry Pi using Hass.io.
Last we will go over some things to look for in devices that work best with this
kind of system.
Let's start by showing the network map of a typical Smart Home with no Hub.
Now here is an example of the same network using a local Hub that still allows
remote access:
Choosing devices that support local control and allow the option of an API will make
things work smoothly down the road while API dependent devices will limit your
options. The real magic happens when you get communications standards involved
like Z-Wave, MQTT, and Zigbee.
Basically this is the easiest and best way to set up a Smart Home Hub like this as the
Disk Image used automatically configures the server when connected to the internet
through Ethernet. By using a wired connection for the server, you can be sure that
you get the fastest and most reliable connection possible for your Smart Home.
To get this going, you will create a boot disk using a MicroSD card. This MicroSD card
will then go into the Raspberry Pi, which is then connected to your home Router via
Ethernet. After about 20 minutes, you should be able to access the user interface
from any device connected to your home WiFi by simply going to http://hassio.local:
8123
Here are the things we will need to get your server up and running:
This setup will also work with other Raspberry Pi versions, but it will run much
better on the Pi 3B and Pi 3B+ New Raspberry Pi computers are coming out all
the time, feel free to use the latest one that is supported by HASS.io!
Other MicroSD cards will work as well, but this is a good baceline MicroSD card.
For better operation it is recommended that you use a 64GB Application Class 2
MicroSD card.
Etcher
If needed, use the SD Card Formatter to format the MicroSD Card and remove any
partitions.
Use Etcher to install the boot disk image onto your MicroSD card.
Once finished, eject the MicroSD card and put it into the MicroSD slot for the
Raspberry Pi.
Use the Ethernet cable to connect your Raspberry Pi into an available network port on
the back of your WiFi Router.
Plug the Raspberry into power using the Micro USB connector.
It can take up to 20 min for Hassio to install. After that time you should be able to
access the user interface by going to http://hassio.local:8123 from and web browser
on your local network WiFi or Ethernet connection.
You can connect the Raspberry Pi to your internet using WiFi if you do not have
access to an Ethernet connection. However this process is much more complex
and should be avoided if possible.
The Configuration menu is where you can add many supported devices and services
using Integrations. You can also add new user accounts including non Admin users
that can not access the full menu structure.
The General menu is where you can change location info along with reloading and
restarting the core services if needed. Area Registry lets you define rooms for better
use with Google Home, Alexa, or Home Kit. Automation allows you to create complex
automations using a fairly easy form based GUI. Script is similar to Automation, but
there is no event trigger. You use Script mostly to create complex combinations of
tasks that you want to call repeatedly with an automation or with a button in the user
interface.
MQTT Clients can Subscribe to a Feed on the MQTT Broker and will receive an
update if the content of the Feed it is Subscribed to changes. Publishing to a Feed is
also allowed by the MQTT Client and the MQTT Broker will pass that published data
along to any device that is Subscribed to that particular Feed. Any MQTT Client can
Subscribe and Publish to a Feed, but they can also just Subscribe or Publish
depending on what your device needs to do.
Adafruit.io hosts an MQTT server, as do many other companies, and they work very
well for testing out devices or creating global networks of devices. However, these
servers typically have Feed limits or subscription fees that may not always jell with
what you want to do.
So why not run your own MQTT Broker using Mosquitto? It is not quite as powerful as
Adafruit.io, but it works just as well for most non-industrial projects. Plus it is free and
secure.
Click on INSTALL.
{
"logins": [
{
"username": "yourMQTT",
"password": "yourMQTTpass"
}
],
Change the username and password to whatever you want to use for connecting to
the MQTT Broker.
If you see MQTT in this list, click CONFIGURE. If MQTT is not in the list, go to Configur
ation -> General -> RESTART. Once reconnected, go back to Integrations and MQTT
should be there.
Click SUBMIT
Click FINISH
This will only work within your local network unless you use Port forwarding using the
MQTT port to point internet traffic to your Home Assistant IP address. I do not
recommend this unless you have some experience with network administration as it
could pose a security risk if not set up properly.
Configurator
While many consumer products can be set up using the Interactions tool, other
devices like those that use MQTT will need to be set up in the Configuration.YAML
file. The code that goes in there will depend on what you are setting up and we will
get to that after setting up the Configurator add-on.
1. From the Configurator panel, click on the folder Icon on the top left.
2. This should show a list of files from the config/ directory.
3. Click the configuration.yaml file to open it.
◦ Note: If you get a popup titled Unsaved Changes, click the CLOSE FILE
option.
There is not much here but you can add things like lights, switches, and sensors that
use MQTT or other communication methods to send data. You can find some
examples of code to add to this in most of the documentation for Home Assistants
many available Components.
Here is an example of what the code looks like with a few MQTT devices added to it:
# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
# Text to speech
tts:
- platform: google_translate
sensor:
# Weather prediction
- platform: yr
- platform: mqtt
name: "Light Sensor"
state_topic: "house/lux"
unit_of_measurement: 'Lux'
icon: mdi:brightness-6
- platform: mqtt
name: "Door Sensor"
state_topic: "house/door"
icon: mdi:door
fan:
- platform: mqtt
name: "Fan"
state_topic: "house/fan"
command_topic: "house/fan"
speed_state_topic: "house/fan/speed"
speed_command_topic: "house/fan/speed"
qos: 0
payload_on: "ON"
payload_off: "OFF"
payload_low_speed: "low"
payload_medium_speed: "medium"
payload_high_speed: "high"
speeds:
- low
- medium
- high
light:
- platform: mqtt
name: "Light 1"
state_topic: "house/led/one"
command_topic: "house/led/one"
brightness_state_topic: "house/led/one/brightness"
brightness_command_topic: "house/led/one/brightness"
rgb_state_topic: "house/led/one/color"
rgb_command_topic: "house/led/one/color"
on_command_type: first
state_value_template: "{{ value_json.state }}"
brightness_value_template: "{{ value_json.brightness }}"
rgb_value_template: "{{ value_json.rgb | join(',') }}"
qos: 0
payload_on: "ON"
payload_off: "OFF"
optimistic: false
lock:
- platform: mqtt
name: Frontdoor
state_topic: "house/lock"
command_topic: "house/lock"
payload_lock: "LOCK"
payload_unlock: "UNLOCK"
optimistic: true
qos: 1
#retain: true
value_template: '{{ value.x }}'
This tool will verify your YAML code is formatted correctly by displaying a green
check mark near the top right corner of the screen. A red ! in the top right corner will
tell you that something is wrong with your code and you can click on it to see what
will need to be fixed before you are finished.
Do not restart Home Assistant if you have any errors in your YAML files. Home
Assistant will not restart if it finds errors because these errors will prevent things
from starting up properly. Do not try to force a restart unless you are sure that
your YAML files are error free because they are hard to fix without the Home
Assistance UI unless you have added and configured Samba or SSH.
When you have checked your YAML code and saved, go to Server Control from the
Configuration menu and click RESTART.
After a few seconds, you will see some text appear that will say Connection lost.
Reconnecting... at the bottom left of the window. When that text disappears, the
server is back online and your changes have been loaded.
Node Red
Node RED is a Flow based programming interface that is somewhere between block
and text based coding. It can be an easy way to program automations for Home
Assistant that are a bit more advanced than what can be done with the Automations
menu.
If you misplaced your MQTT credentials, they can be found by going to the
Hass.io menu and click on Mosquitto broker. The Password and Username
should be in the Config field.
That will add your connection and your can now set a topic to interact with. As a test, I
will usually add and inject node to send a time stamp to the mqtt node under output
with a Topic of "test". Then I will get another mqtt node from the input section, set it's
Topic to "test" and connect it to a debug output.
Lovelace UI
The user interface that is used for Home Assistant is called Lovelace and not only is it
nice looking, but it easy easy to customize through any computer web browser.
Automations UI
There is a tool that can be used to create some rather complex Automations in the
Configuration menu. This is the easiest way to create some rather complex
automations without using Node-RED or editing YAML files in the automations.yaml
file.
Scripts UI
Scripts will allow you to use one command to do a bunch of things at once. This can
be handy if you have a series of actions that will be reused in automations.
Script Editor
Development Tools
These tools are very useful for testing and troubleshooting devices, but also for
seeing what data is needed for automations and scripts. The Services tool is very
helpful for creating and testing the JSON formatted Service Data used with more
complex devices like lights and media players.
Development Tools