In this beginner’s guide, I’ll show you how to build a home automation hub using Home Assistant on Raspberry Pi 4.
You’ll learn how to:
– Set up Home Assistant on Raspberry Pi
– Choose proven smart home gear
– Integrate your devices with the platform
– Create helpful home automations
– Create an easy user interface
– Lots more
Let’s get started.
Contents
Chapter 1: What is Home Assistant
Chapter 2: Recommended Devices
Chapter 3: How to Install Home Assistant on Raspberry Pi 4
– Integrate Philips Hue lights
Chapter 4: Add-ons Installation
– Install File Editor User Interface
Chapter 5: Device Integration
– Integrate smart plugs
– Integrate Sensibo Sky air conditioning controls
– Integrate Harmony Hub
– Integrate Switchbot
Chapter 6: Home Assistant Automation Ideas
– Change the temperature of lights using circadian rhythm
– Set the dimmed scene in the evening
– Turn the lights and/or other devices on when motion is detected
– Turn off socket after the battery has been charged
– Play favorite playlist from Spotify
Chapter 7: User Interface Configuration
– Use the mobile companion app
Chapter 8: Remote Access
In my earlier guide to home automation basics, I showed you the devices needed to build your own smart home.
In this more advanced guide, I will show you how to create a localized Smart Home Automation Hub using Home Assistant, which is an open source software and operating system.
There’s lots of information about Home Assistant (HA) from disparate sources but no straightforward and easy to understand complete guide.
The problem is that many websites just hint at the capabilities but leave you wondering about the details of how to put a full and well-thought-out system together.
In this proven guide, you will learn how to build your own smart home hub to create a seamless integration between devices using advanced scripts.
I use my own smart devices and home automation ideas to illustrate how different features of HA work.
Chapter 1: What is Home Assistant
What is the Home Assistant platform and do you really need it?
The Samsung SmartThings Hub allows you to hook into other smart devices and set up schedules or commands driven by an event and smart devices can then work together to execute specific routine.
You can also do more and more home automation routines with Google Home and Alexa.
However, what really sets Home Assistant apart from other hubs is its power and privacy.
Power: Home Assistant doesn’t focus on a single brand of smart devices, and as such nearly any device or sensor you use will work with it. If you have invested in several smart devices, Home Assistant is an excellent way to extend the functionality and control them all together via a customizable user interface or voice.
Privacy: It’s a local service, which means it’s not going to send any data to the cloud, and you won’t depend on an internet connection or remote servers. Your commands and schedules are for your eyes only.
I have been experimenting with HA for over a year and I’m convinced that you should consider using it as a home automation hub together with Amazon Alexa or Google Home voice assistant.
To illustrate how I have set up Home Assistant and all my devices, here is a diagram of my smart home structure:
Next, you’ll learn more about smart home gear I use in my home.
Chapter 2: Recommended Devices
In order to set up Home Assistant, you will need a computer to run its software. When you get started, I would recommend using Raspberry Pi 4.
Later, if you feel you need more power and responsiveness, Intel NUC minicomputer is an ideal hardware for power users.
Here are my tested recommendations for smart home devices that I use every day in my home.
This step-by-step guide is based on the following devices.
Category | Product | Description | Price | |
Voice Assistant (Option 1) | Google Home can help you to effectively run your smart home using your voice. | |||
Voice Assistant | Amazon Echo is the de facto standard smart speaker and is widely supported. | |||
Mini Computer | Computer to run Home Assistant software, turning it into an automation hub. | |||
Smart Lighting | Starter kit including a bridge and two light bulbs to easily control your lights. | |||
Smart Lighting | Use Go light to enhance the atmosphere or as a night lamp. It is also portable. | |||
Smart Lighting | Place Lightstrip under cabinets or behind entertainment systems. | |||
Motion Sensor | Turn on and off your lights automatically with the Philips Hue motion sensor. | |||
Smart Plug | Turn any light even one in which you can't place a Hue bulb into a smart light. | |||
Smart Plug | Turn your appliances (e.g. cooling fans) ON/OFF and get insight into energy usage. | |||
Universal Remote | With Logitech Harmony you can control all of your entertainment devices. | |||
Smart WiFi A/C | You can control your A/C and set automation rules with this WiFi controller. | |||
Switch | It mechanically turns rocker switch or button ON/OFF. Hub Plus required. | |||
Bluetooth to Wi-Fi Gateway | Make all your nearby SwitchBots (the Bots) access to Wi-Fi + IR remote control. |
Chapter 3: How to Install Home Assistant on Raspberry Pi 4
These steps follow the instructions from the Home Assistant’s installation page.
This method assumes you already have a working ethernet connection. Wi-Fi is much more unreliable than Ethernet and not recommended at all.
- Download the latest Home Assistant image for the Raspberry Pi 4.
- Download and run BalenaEtcher.
- Select the downloaded Home Assistant image from your computer.
- Click Flash! button.
- Insert the micro SD card into the Raspberry Pi.
Turn on your Raspberry Pi. On first boot, the system performs initialization routines and downloads the latest version of Home Assistant (which took around 20 minutes using my internet connection).
- On your computer or laptop’s browser, go to http://homeassistant.local:8123. If your router doesn’t support mDNS, then you’ll have to use the IP address of your Pi instead of homeassistant.local. For example, http://192.168.0.9:8123.
- You will be prompted to create a new account.
- Next, you will be asked to name your home and set up your time zone and unit system. Click next.
Integrate Philips Hue lights
Next, the on-boarding process gives you an option to integrate your first device. Let’s give a try integrating Philips Hue lights.
- As prompted by the instructions on the screen, press the button on top of your Philips Hue hub and click “Submit.”
- Next, you will be presented the list of found devices.
- Designate the correct area for each light. If you need to add an area, just click “Add area” and type the name of the area.
- Once you are done, click “Finish.“
Please note that if you have added other Hue accessories such as smart plugs for Hue, they will also be automatically added to HA.
Let’s leave the on boarding process as we will later add more devices.
If you cannot add Philips Hue bridge automatically using the on-boarding process, you can add the following code to configuration.yaml file:
#Philips Hue hue: bridges: - host: YOUR_HUE_IP_ADDRESS_HERE
Please refer to the next chapter on how to add code to the configuration.yaml file.
Chapter 4: Add-ons Installation
Install File Editor User Interface
File editor (formerly know as Configurator) add-on is a browser-based alternative to selecting and modifying your YAML configuration files.
Let’s first install the File editor add-on and then in chapter 5, you’ll learn how to add code to configuration files.
- Then, go to the Supervisor tab and select File editor from the official add-ons list. Click “Install.”
- Then, toggle “Show in sidebar.“
- Click “Start.”
- Now, you can find the “File editor” tab on the left-hand side of the screen.
- In the File editor tab, click the “Browse Filesystem” icon at the top of the screen.
- Select configuration.yaml to add code for various integrations.
- Once you are done, remember to click the “Save” button.
Learn how to check configuration validity
Before we go further, it is useful for you to know how to validate your configuration.yaml’s validity before rebooting the system for any changes to take effect.
In order to do that, we need to change your user’s permissions.
- Select your user Profile by clicking your username on the menu.
- Scroll down and toggle “Advanced Mode” on.
- Then go to the “Configuration” menu.
- Click “Check Configuration” to validate your configuration.
- Then, under Server Management, select “Restart.”
There are other powerful add-ons you could install like samba, mqtt, and node red.
However, you don’t need them when you are getting started with HA. Once you master the basics, you can start exploring more advanced functionalities.
Chapter 5: Device Integration
Home Assistant will be able to automatically discover many devices and services available on your network.
You can also manually integrate more devices and services.
In this chapter, you will learn how to integrate your existing devices with Home Assistant.
Here’s how exactly to do it.
Integrate smart plugs
Smart plugs are a great way to turn “dumb” devices such as lamps or fans to “smart.”
I’ve been using WeMo smart plugs for a while now and I recently started testing TP Link smart plugs and the Philips Hue smart plugs.
If you use smart plugs for Hue, they have already been automatically added when you integrated Philips Hue bridge with HA.
I use smart plugs to control
- lights that do not support Philips Hue lamps (e.g. Christmas lights)
- cooling fans
- battery chargers
- balcony heater
WeMo smart plugs
I use both WeMo Insight (great for energy monitoring) and WeMo socket smart plugs.
This is how you can add WeMo smart plugs. Add each WeMo smart plug with their own IP address to the configuration.yaml file.
# Wemo smart plugs wemo: discovery: false static: - YOUR_FIRST_WEMO_PLUG_IP_ADDRESS - YOUR_SECOND_WEMO_PLUG_IP_ADDRESS
TP Link Smart Plug
If you prefer using TP Link Smart Plug, you will need to add the following code to the configuration.yaml
# TPLink smart plug tplink: switch: - host: YOUR_FIRST_TP_LINK_PLUG_IP_ADDRESS
Unfortunately, I kept having problems with TP Link with Home Assistant. It became unavailable occasionally and never recovered.
Integrate Sensibo Sky air conditioning controls
If you have an air conditioning device that uses a remote control with a display, I would recommend using Sensibo Sky to control your AC.
This is how you can integrate Sensibo Sky with your HA.
- First, login to https://home.sensibo.com/me/api to get your Sensibo API key. Type any name for your API key and click “Add API Key.”
- Select the File editor tab from the menu.
- Select the Browse Filesystem icon and locate configuration.yaml file.
- Paste the following code into the next empty line on configuration.yaml (change YOUR_API_KEY into the one you got from home.sensibo.com/me/api):
- Click the “Save” icon and then close the file by clicking the X icon.
# Sensibo Sky climate control climate: - platform: sensibo api_key: YOUR_API_KEY switch climate: - platform: template switches: ac: friendly_name: "AC" value_template: "{{ is_state('climate.ac', 'cool') or is_state('climate.ac', 'heat') or is_state('climate.ac', 'dry') or is_state('climate.ac', 'fan_only') }}" turn_on: service: climate.set_hvac_mode data: entity_id: climate.ac hvac_mode: cool turn_off: service: climate.set_hvac_mode data: entity_id: climate.ac hvac_mode: off
Create custom sensors for temperature values
You may want to create visual graphs into a user interface on how the temperatures are developing.
Next, I will show you how to create a template platform that supports custom sensors. These custom sensors get their values from other entities such as climate control.
In order to create custom sensors, add the following code to the configuration.yaml:
# Create a custom sensor for Wemo Insight sensor: - platform: template sensors: livingtemp: friendly_name: "Living room thermostat" value_template: "{{ state_attr('climate.living_room', 'current_temperature') }}" unit_of_measurement: "°C" device_class: temperature kidstemp: friendly_name: "Kids room thermostat" value_template: "{{ state_attr('climate.kids_room', 'current_temperature') }}" unit_of_measurement: "°C" device_class: temperature bedroomtemp: friendly_name: "Bedroom thermostat" value_template: "{{ state_attr('climate.bedroom', 'current_temperature') }}" unit_of_measurement: "°C" device_class: temperature studytemp: friendly_name: "Study room thermostat" value_template: "{{ state_attr('climate.study_room', 'current_temperature') }}" unit_of_measurement: "°C" device_class: temperature
Now we need to restart the HA operating system.
After the operating system has been rebooted, you can configure the user interface to create a thermostat card.
- On the right-hand upper corner, go to the three dots area and select “Configure UI.” Confirm by selecting “Take control.”
- Press the orange + icon.
- Select the Thermostat card from the list.
- Then select which Sensibo Sky device you want to add as a card view.
Integrate Harmony Hub
Next, we’ll integrate Logitech Harmony Hub with Home Assistant.
- Select the File editor tab from the menu.
- Select the Browse Filesystem icon and locate configuration.yaml file.
- Paste the following code to the next empty line on configuration.yaml (change HARMONY_HUB_IP into the one you got from your router):
- Then go to “Configuration” > “Server Controls” and “Check Config.” Afterward go to Server Management and click “Restart”.
# Logitech Harmony Hub remote: - platform: harmony name: Living Room host: YOUR_HARMONY_IP_ADDRESS_HERE
Create a script to turn on and off Harmony activities
Scripts will tie several actions together.
We’ll create scripts that allow you to turn on and off Harmony activities such as Turn on AV Receiver.
- Go to “Configuration” > “Scripts” and press the orange plus sign to create a new script.
- Write a name for the script. For example, Turn on TV.
- Add the following code to the sequence:
data: activity: YOUR ACTIVITY NAME entity_id: remote.YOUR_HARMONY_NAME service: remote.turn_on
For the turn off script, you can follow the same steps, but this time select remote.turn_off as a service.
Finally, you can test these scripts in the Script Editor by hitting the play button next to the script name.
Integrate Switchbot
I live in an apartment where it is not feasible to replace light switches on walls.
Therefore, I use Switchbot Bots to turn mechanical light switches into smart ones. In addition to Switchbot Bots, you will also need a Switchbot Hub Plus that will translate Wi-Fi commands to Bluetooth commands for Bots.
- First, look for Switchbot’s MAC address by opening Switchbot’s own app.
- Click on the Bot settings icon and then the three dots at the right-hand top corner. Here you can see the device’s MAC address.
- Go to configuration.yaml file and add the MAC addresses of your Switchbots.
- Restart your system, and you are ready to add switches to your user interface or automations.
#Switchbot switch: - platform: switchbot name: 'Switchbot Bot 1' mac: 'XX:XX:XX:XX:XX:XX' - platform: switchbot name: 'Switchbot Bot 2' mac: 'YY:YY:YY:YY:YY:YY'
Now, we have added some example devices to the platform. Next, we’ll create some helpful automation rules to make your life more convenient.
Chapter 6: Home Assistant Automation Ideas
Automations allow you to create triggers, conditions and actions.
Change the temperature of lights using circadian rhythm
This nice automation uses the Flux switch platform to change the temperature of lights using circadian rhythm.
You don’t need to buy any specific lux sensors as Home Assistant has this feature built in.
I would not recommend adding a light group, but you should add the individual lights in the below Flux code.
The reason is that to change the color temperature, Flux will issue a light.turn_on service call to the whole group, which means that if one or more of the lights in the group are turned off, they will be turned on again.
- Add the following code to the configuration.yaml:
- Go to “Configuration” > “Automation” to create a little automation routine that will periodically check the circadian rhythm and adjust light colors accordingly.
- Create a new automation called, e.g., Update Fluxer.
- Set the following as trigger:
- I also created a condition that the Fluxer does not update lights after 11 p.m. as otherwise it may turn on lights that you have already turned off earlier:
- Lastly, add the following code to the action:
# Flux switch platform to change the temperature of lights using circadian rhythm switch flux: - platform: flux lights: - light.lightname_number_one - light.lightname_number_two - light.lightname_number_three name: Fluxer start_time: '7:00' stop_time: '23:00' mode: mired
platform: time_pattern seconds: '30'
after: '07:00' before: '23:00' condition: time
service: switch.fluxer_update
Set the dimmed scene in the evening
In addition to automatically adjusting color temperature, I like to dim the lights in my home at specific time in the evening to help my family start preparing to go to sleep.
- Let’s create a script for each room to set the lights to a specific scene. I have created a scene called Dimmed in my Philips Hue app, but you can use, e.g., the default Relax scene in the Philips Hue app.
- Add the following code to the Set the dimmed scene in a specific room (e.g., Bedroom):
- Go to Automations tab and create a new automation called e.g. Set the dimmed scene in the evening.
- Add trigger as follows to dim the lights, e.g., at 10 p.m.:
- Next, we’ll call the “Set the dimmed scene in bedroom” script that we created earlier (You can find your script number, e.g. by copying the end of the URL of your script’s page):
- Remember to add multiple scripts if you created the dim lights script for multiple rooms.
data: group_name: Bedroom scene_name: Dimmed service: hue.hue_activate_scene
at: '22:00' platform: time
data: {} service: script.YOUR_SCRIPT_NUMBER_HERE
Turn the lights and/or other devices on when motion is detected
Next, I’ll show you how to use the Raspberry Pi home automation routine to turn on a light when you walk into a room.
You can also add other devices into this automation such as turning on cooling fans using a smart plug.
- Create an automation called e.g. “Turn the study lights and fan on when motion is detected”.
- Add the following code as trigger (edit as YAML).
- Create action to add your light (repeat this step to add other lights).
- Create action to add other devices using a smart plug (repeat this step to add other smart plugs).
device_id: YOUR_HUE_DEVICE_ID domain: binary_sensor entity_id: binary_sensor.YOUR_HUE_MOTION_SENSOR_NAME for: hours: 0 minutes: 0 seconds: 0 platform: device type: motion
device_id: YOUR_HUE_DEVICE_ID domain: light entity_id: light.YOUR_LIGHT_NAME type: turn_on
type: turn_on device_id: YOUR_SMART_PLUG_ID entity_id: switch.YOUR_SMART_PLUG_NAME domain: switch
You can equally create another automation to turn off lights and other devices automatically, e.g., after no motion is detected for 10 minutes.
Simply use the above codes, but instead of “turn_on” write “turn_off” in your actions.
Turn off socket after the battery has been charged
I use WeMo Insight to turn off the socket after my electric bike’s battery has been successfully charged.
You can use this automation for your mobile charger’s or any other socket where you charge batteries.
I made a custom sensor that reads the current power reading from the WeMo Insight. Then I created an automation to turn off the smart plug after battery charging is finished.
- First add this code to the configuration.yaml:
- Restart your system.
- Go to the Automations page and create a new automation called, e.g., “Turn off battery charger when charging finished.”
- Add this code as the automation trigger to trigger when current power is below 5 W for 30 seconds:
- And add this code as an action to turn WeMo Insight switch off:
# Create a custom sensor for Wemo Insight sensor: - platform: template sensors: wemopower: friendly_name: "Battery Charger" value_template: "{{ states.switch.battery_carger.attributes.current_power_w }}" unit_of_measurement: "W"
below: '5' entity_id: sensor.wemopower for: '30' platform: numeric_state
entity_id: switch.battery_carger service: switch.turn_off
Play favorite playlist from Spotify
There are a couple of Spotify playlists that I like to listen to often. So, I created a script that turns on my receiver, selects correct audio input and uses Google Chromecast to play my Spotify playlist on my stereo.
- Make sure you have set up Logitech Harmony Hub with Home Assistant.
- Connect Google Chromecast to your A/V receiver and make sure Chromecast is set up properly using the Google Home app.
- If you have not already, create an activity in Harmony to turn on A/V receiver and set it to correct input.
- You need to create a developer application in https://developer.spotify.com/.
- Add Spotify IDs to configuration.yaml file.
- Go to the Integrations page in HA and search for Spotify integration.
- If you get an error message INVALID_CLIENT: Invalid redirect URI when adding Spotify integration, you need to add http://YOUR_HA_IP:8123/auth/external/callback to Spotify developer account’s redirect URIs.
- Insert your API codes and you are ready to create scripts to play music.
- Go to the Scripts page in HA and create a new script.
- Create a sequence to select the correct source.
- Then call the service to play your preferred playlist. You can copy the playlist ID from Spotify by selecting the link to the playlist. Note: I added the shuffle on by default so that the playlist does not always start with the same song.
- Finally, call the script that sets your A/V receiver to correct input.
# Add Spotify integration spotify: client_id: YOUR_SPOTIFY_ID client_secret: YOUR_SPOTIFY_SECRET_CODE
data: source: YOUR_SPEAKER_NAME entity_id: media_player.YOUR_SPEAKER_NAME service: media_player.select_source
data: shuffle: 'true' entity_id: media_player.spotify_YOU_NAME service: media_player.shuffle_set
Chapter 7: User Interface Configuration
Our homes are always unique in terms of devices we need and how our user interface should look like.
For this reason, it is good to take influence from my guide, but I would suggest playing around with user interface configuration until you are happy with your individual customization.
- On the right-hand upper corner, go to the three dots area and select “Configure UI.” If this is your first time editing UI, you need to confirm by selecting “Take control.”
- For example, I wanted to create a separate tab for lights. Press the + icon and give the name for the tab.
- Then you can add lights to the new Lights view.
Here are a couple of example configurations from my user interface.
Glance Card to show temperatures in different rooms
Horizontal Stack Card to control lights in different rooms
Entities Card to summarize different scripts
Use the mobile companion app
You can use either the iOS or Android companion app to control all your devices in your Home Assistant hub.
Next, I will show you how to access your hub remotely.
Chapter 8: Remote Access
Nabu Casa is a cloud service developed by the founders of Home Assistant.
It is an easy way to get remote access to your Home Assistant.
- Go to configuration and select Home Assistant Cloud.
- Next, click Start your free 1-month trial button.
- Create an account by inputting your email and password and clicking Start Trial.
- Once your account has been created, go to your email to confirm your email.
- Now you can go back to the Home Assistant Cloud Login page and login.
- It will take a while before remote access is available and you will get an email notification once it is ready.
- Once you can see a remote access link, remember to enable the Remote Control tab on the integrations section.
- You can enable remote access also on the mobile Home Assistant app.
- Go to App Configuration > Connection > Enable Connect via Cloud.
Conclusion and Next Steps
Thanks to this easy step-by-step guide, you don’t need to spend hours searching for instructions anymore.
You have everything you need to get started building your own smart home automation hub in one place.
As you saw in the guide, Home Assistant is a really powerful home automation software, but requires some practice before you can master it.
Once you master these basic Home Assistant skills, you can add more advanced automations.
I will cover the following more advanced features and automations in the upcoming guide:
- Reliable, multi-user presence detection
- Smart alarm clock routine
- Safety automations based on smoke and CO2 sensors and water leak sensors
- Security automations based on door and window sensors and cameras
- Automations for laundry
- And much more
Before you start building your own hub, please make sure to join the waitlist of my even more detailed smart home guide.
If you made it this far, congratulations! You are ready to build your own smart home hub.