What You'll Learn

What You'll Need

What You'll Build

Watch this video to follow along at each step:

REST and WebSockets for connected home

When it comes to transmitting data in a connected home environment, both REST and WebSockets are commonly used protocols, but they have different characteristics and use cases.

REST follows a request-response pattern, where a client sends a request to a server, and the server responds with the requested data. This is useful for accessing and controlling smart devices and services, and works well for scenarios where data updates are not required in real-time. For example, REST is suitable for a one-off request to turn on a smart light.

On the other hand, WebSockets enables bidirectional communication between a client and server, enabling real-time data transmission. This is useful for applications that require continuous data updates, such as real-time monitoring of sensor data and displaying live dashboards. For example, WebSockets is suitable for continuously monitoring the temperature in a room over a persistent connection.

In the next section, let's take a look at a popular home automation platform that provides both REST and WebSocket APIs.

Home Assistant for home automation

Home Assistant is a popular open-source home automation platform that lets you control and monitor smart devices from different brands using a unified interface. Instead of using separate mobile apps to control your kitchen lights, thermostat, and smart coffee maker produced by different manufacturers, you can manage everything from a single Home Assistant dashboard running on a Raspberry Pi or other dedicated server within your local network.

home assistant dashboard

Home Assistant is ideal for DIY smart home tinkerers because it supports a wide range of integrations and protocols, allowing you to create automation scenarios based on events, schedules, and sensor readings.

Next, let's take a look at Home Assistant's WebSocket API.

In addition to a REST API, Home Assistant also contains a WebSocket API to stream information. Using a long-lived token, you can use Postman to establish a connection with our Home Assistant server running locally, and then send and receive messages using the WebSocket API.

Fork the Postman collection

  1. Find the sample collection, Home Assistant WebSockets, and fork the collection to your own Postman workspace. fork icon
  2. Enter a label for your fork and select the workspace to fork the collection: fork label

Create Home Assistant long-lived token

  1. Log in to Home Assistant
  2. Create a long-lived access token from the Home Assistant profile page. long lived token
  3. Let's save our token as a Postman variable, so that we can use it in our API requests. Select the Postman collection that you forked to your workspace. Find the Variables tab, and paste your long-lived access token as the token variable value. save token as variable

Authenticate the WebSockets connection

  1. Select the Postman collection that you forked to your workspace. Open the "Home Assistant WebSocket API" request, and click the Connect button.
  2. When a client connects to the server, the server sends out auth_required.In the Response pane, you can inspect the message returned from the WebSockets server. You can further inspect the connection details by clicking on "Connected to ws://homeassistant.local:8123/api/websocket". The connection established between the Postman client and local server is bidirectional. In addition to receiving messages, Postman can also send them. network connection details
  3. The first message from the client should be an auth message that includes an access token. Under the Message tab, under Saved messages, select a message called auth to display a JSON message that includes your token. If you were busy inspecting, the server may have disconnected from Postman. Connect once again, and then Send the auth message. auth ok

Send and receive messages

Once the connection is authenticated, you can send and receive messages. Select from Saved messages to initiate commands.

  1. The command subscribe_events will subscribe your client to the event bus. You can either listen to all events or to a specific event type. Find a Saved message called subscribe_events that contains the following message, and Send.
  "id": 18,
  "type": "subscribe_events",
  "event_type": "state_changed"
  1. Once you subscribe to listen to all events, you will receive a message when the state changes on any device connected to Home Assistant. show state changes
  2. For each message that you send or receive, you can expand the message details. show message details
  3. If the Response pane becomes too busy, you can Clear messages, and filter the messages by text or sent or received. show message details

Continue exploring the other Saved messages within this collection. Once you learn how to work with Home Assistant WebSockets APIs, you can build your own custom integrations and notifications.

Watch this video to recap the steps:

What we've covered

For more hands-on tutorials, check out these resources.

Additional resources