How to Control Home Assistant on Your Raspberry Pi with Twilio SMS
source link: https://www.twilio.com/blog/control-home-assistant-raspberry-pi-twilio-sms
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to Control Home Assistant on Your Raspberry Pi with Twilio SMS
The tutorial How to Control Your Raspberry Pi with Twilio SMS shows that it is possible to remotely control your Raspberry Pi with SMS.
This is done by using an SMS message as a set of commands defined by a protocol.
Everything we want to control on the Raspberry Pi is a controlled object, and it is associated with a GPIO pin.
What if we can do better?
What if we can control more than a simple device connected to a GPIO?
What if we can control a home automation system with SMS?
In this tutorial, you will learn how you can control your Home Assistant on your Raspberry Pi with Twilio Programmable SMS. But first, let’s explore what Home Assistant is.
What is Home Assistant?
Home Assistant is an open-source home automation tool that allows you to control many electronic devices with a single web interface.
It was created by Paulus Schoutsen in September 2013.
It’s a free software written with Python as its programming language.
Home Assistant can run on different hardware and platforms. You can install and run it on operating systems like Linux, macOS, and Windows with WSL.
You can also run it on a single-board computer like a Raspberry Pi.
Take a look at the official web site for more information. Sometimes we refer to Home Assistant as HASS.
Why choose Home Assistant?
There are many advantages that can drive someone to choose Home Assistant, but for the purpose of this tutorial there are mainly three reasons:
- Home Assistant can be integrated with many controllable devices: It integrates with over a thousand devices and services. This will allow you to control thousands of objects with SMS.
- Home Assistant can run on Raspberry Pi: It is recommended to run Home Assistant on a Raspberry Pi, and the Home Assistant OS can be run directly on the board.
- You can control a device or an object on Home Assistant via REST API: This is one of the keys to this tutorial. The Home Assistant REST API allows you to manipulate entities represented generally by a device from an external program.
How to associate a controlled object with the Home Assistant system
In Home Assistant, any controllable data point in a device is represented by an entity.
In other words, an entity is a controllable object in Home Assistant.
An entity can be controlled by calling an action on it. In Home Assistant terminology, the action is called a "service", and a service belongs to a domain. See the glossary on the official website for more information about each definition.
If we compare the similarities to the system described in the tutorial How to Control Your Raspberry Pi with Twilio SMS we have:
- Entity is a Controlled object
- Service is an action
This allows us to control entities with the same protocol in Home Assistant by SMS.
But how can we send the command to Home Assistant?
HASS has a rest API that allows external tools to interact with it. It accepts JSON data format and allows you to control an entity by calling a service.
The service can be called by using a URL like the following:
http://<your_raspberry_pi_ip_address>:8123/api/services/<domain>/<service>
For example, to switch on a light, you can make a POST request to the following URL by sending an entity_id
as data:
http://<your_raspberry_pi_ip_address>:8123/api/services/switch/turn_on
The idea here is that in order to control an object in Home Assistant with SMS, you need to translate the SMS message to a command, then the command to a REST API call.
Here is a schema showing the interaction between every part of the system:
This is how the system works:
- An SMS is received on your Twilio number.
- Twilio calls a webhook that is pointing to the web application hosted on your Raspberry Pi.
- The web application then extracts the command from the SMS content, and translates it to a controlled object according to the protocol.
- The web application calls the corresponding URL to the REST API.
That is how the whole system works. Now let’s put it into practice.
Implementation of controlled object with LED
In this section, you will learn how to define a light object in your Home Assistant and how you can control it with Twilio SMS. An LED will be used as a light.
Requirements
- Read the article How to Control Your Raspberry Pi with Twilio SMS
- One Raspberry Pi with Home Assistant installed on it. Check out this link to install Home Assistant. You can install it with Docker or a Python virtualenv. The virtualenv installation is used in this article. A Raspberry Pi 3 Model B is used in this tutorial, but you can use any model of the board.
- Two LEDs
- Two 330 Ohm resistors
- One breadboard
- Some jumper wires
- A Twilio phone number: Here is a link to create an account if you don’t have one already: Twilio | Try Twilio Free.
- Ngrok. You can learn more about using Ngrok in 2022 here.
Build the circuit
To realize the circuit, you need to connect your two LEDs to your Raspberry Pi.
Follow the schematic below to connect each part. Note that GPIO 18 and 12 are each connected to an LED.
Add LEDs as light switches to Home Assistant
In Home Assistant, you need to add the corresponding integration before being able to interact with any kind of device.
For the LED switches connected to the GPIO, we will use the remote_rpi_gpio integration.
This integration needs the pigpio daemon to perform any action on the GPIO pins, so let’s install it first.
Install pigpiod
Run this command to install pigpiod:
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK