Getting Started with Home Assistant Webhooks
Aug 07, 2023
This post was written by Suleiman Abubakar Sadeeq, an ambitious react developer learning and helping to build enterprise apps.
For someone already familiar with Home Assistant, you may know how much ease it brings to a lot of our daily activities through automation. You may also wonder how to send out information after an event trigger. Let’s say you stepped out of the house, and you want the door unlocked when you reach a certain distance from your home. In this case, you can use a webhook to send your location data to your Home Assistant instance. That way, it can use it to unlock the door for you. In this post, we’re going over Home Assistant webhooks, and how you can get started with them. Let’s dive in!
How cool would it be to have all your morning routines automated? From your morning coffee, to temperature adjustment, to your TV, to your alarms. Mind you, it doesn’t stop with your morning routines — you can also automate your whole day with Home Assistant if you want to.
Home Assistant is open-source software that allows the automation of homes. It provides a way for you to locally control all the smart devices in your home from one source (Home Assistant mobile app, desktop UI, or desktop companion app) without having to use multiple devices separately. It offers more flexibility and control over how you want your automation to look and feel.
A webhook is an event-driven one-way request, this implies that it isn’t bidirectional, and it can only send out information. If you’re familiar with APIs, a webhook is similar to it but more lightweight, and it can only send data in response to an event.
How do I use webhooks in Home Assistant?
Now that we know what Home Assistant and webhook are, let’s explore how you can use webhooks in Home Assistant. The first step will be creating automation in your Home Assistant instance. To create automation, go to Settings and select Automation and Scenes. After it redirects you to the automation page, click on the Create Automation button at the bottom right corner.
To create new automation, select Webhook as the Trigger. Home Assistant will automatically assign it a unique ID. This ID has to be unique, in case you want to edit yours, and it should also be treated like a secret key. The reason for that is: if your Home Assistant exists on a public network and then someone gets a hold of your Webhook ID, they might be able to create automation for your instance from anywhere. And this is possible because webhooks do not have any authentication placed on them, so anybody with access to your Webhook ID and instance can make a call and create automation.
Now we can create our very first webhook. This example is going to be very simple and basic, just so you can have an idea of how to use webhooks on Home Assistant. We’re going to send a “find my device” message to your phone using a webhook from Home Assistant.
Add an action and select Call service, as seen in the image above. Now, select the service Apple iCloud: Display message. This service will appear if you have your iPhone connected to your Home Assistant instance. Input your iCloud username, device name, and the message you want to send, and hit save. With that, you’ve created your first webhook. The next part is going to be how to test to see if the webhook works.
Testing a webhook
To test your Home Assistant webhook, you need a way to send a POST request. There’s more than one way to do it (Python script, Zapier, shortcut on iPhones, etc.), but in this post, we’ll be using Postman. For those not familiar with Postman, it’s a service that allows you to make API calls through endpoints. You can download Postman desktop if you want, or you can use their web interface on the Postman site.
Keep in mind that you can only test a Home Assistant webhook using a POST, PUT, or the other method mentioned on the Home Assistant website.
On Postman, create a new request and have your request method set to POST. Copy the webhook’s unique ID from your Home Assistant instance and paste it into Postman. Notice how the Webhook URL is formatted just like you see in the image above. Don’t worry about my URL being public, because I’ll delete it afterward.
Let’s dissect the URL pattern of the Home Assistant webhook: HTTP://Instance_IP:PORT/api/webhook/ID
Instance_IP: In my case, 172.20.10.2 is the network’s IP address.
PORT: This is the Home Assistant port.
api/webhook: These are prefix words that Home Assistant attaches to the URL to identify it. Both of these words are constant and come attached to all of your webhook automation.
ID: This is the unique ID of your webhook that Home Assistant uses to identify the webhook being triggered. Do not share or allow this to go public to prevent risks to your privacy.
Now that you know why the URL looks like that, you can hit the Send button on Postman to send the request. After a few seconds, it’ll send you a 200 OK response, and simultaneously, you should also receive a notification on your phone with the message you assigned to it (just as in the image above).
More use cases of webhooks
Smart home automation: This is one of the most famous use cases of webhooks. You can leverage webhooks on Home Assistant to create advanced automation in your home. You can create a webhook that executes automation when a condition is either true or false. For instance, turning off the light in the seating room when nobody is in. This webhook use case can help you simplify your daily customs and routines.
Mobile notification: You can create webhooks that send out a notification to your phone whenever automation happens in your Home Assistant. For instance, when someone triggers a motion sensor, or when the doorbell is ringing while you’re away. You can create a webhook that listens to such events and sends you an alarm notification on your phone. That way, you’ll stay in sync with all activities in your home even while on your commute, at work, or away on a trip.
Third-party services: You can also leverage webhooks in Home Assistant to integrate third-party services. For instance, using a webhook to create automation in another application or service (Slack, WhatsApp, Telegram, etc.) after an event occurs in your Home Assistant.
Data analytics: With webhooks, you can log data from external sources to your instance to create automation by analyzing data. This is good, because you can analyze the behavior of a service and gain insight before creating your automation. That way, all your automation is data-driven. A good example is when creating weather automation, you can use data you have access to in order to trigger certain actions or events.
The list of use cases for Home Assistant webhooks is endless because of its adaptability when it comes to smart home automation. But above are a few examples that give you an idea of what you may want to use Home Assistant webhooks for.
It’s amazing, knowing all the cool things you can do with the power of webhooks on Home Assistant. I hope this post has done justice in providing you with the basic knowledge of using webhooks in Home Assistant. And I hope it’ll inspire you to create more automation that will make your life easy.
Check out InfluxData if you want to learn more about how you can benefit in collecting your smart device’s sensor and metrics.
About the author
This post was written by Suleiman Abubakar Sadeeq. Suleiman Abubakar Sadeeq is an ambitious react developer learning and helping to build enterprise apps. In his free time, he plays football, watches soccer, and enjoys playing video games.