Home automation systems are integral to modern smart homes, bringing convenience and efficiency to daily life. However, managing and monitoring these systems effectively can be a challenge due to the volume and velocity of data generated by various devices like thermostats, lights, and sensors. This tutorial will guide you through building a real-time API for monitoring such home automation systems using Tinybird. This API will enable you to monitor room temperatures, device history, and current device statuses in real-time. Tinybird is a data analytics backend for software developers. You use Tinybird to build real-time analytics APIs without needing to set up or manage the underlying infrastructure. Tinybird offers a local-first development workflow, git-based deployments, resource definitions as code, and features for AI-native developers. It simplifies the ingestion, transformation, and delivery of real-time data through APIs, leveraging data sources and pipes to process and expose the data efficiently. Let's dive into how you can leverage Tinybird's capabilities to implement a solution for home automation monitoring.
Understanding the data
Imagine your data looks like this, reflecting events from various home automation devices:
{
"device_id": "dev_909",
"device_type": "window_sensor",
"room": "kitchen",
"event_type": "measurement",
"status": "off",
"value": 163950490900,
"battery_level": 163950490900,
"timestamp": "2025-05-11 23:48:22"
}
This data represents events from devices such as thermostats and sensors, including information about the device type, location, event type, status, and various measurements. To store this data in Tinybird, you need to create data sources. Here's how you define a data source for these events:
DESCRIPTION >
Stores all events from home automation devices such as thermostats, lights, motion sensors, and door sensors. SCHEMA >
`device_id` String `json:$.device_id`,
`device_type` String `json:$.device_type`,
`room` String `json:$.room`,
`event_type` String `json:$.event_type`,
`status` String `json:$.status`,
`value` Float64 `json:$.value`,
`battery_level` Float64 `json:$.battery_level`,
`timestamp` DateTime `json:$.timestamp`
ENGINE "MergeTree"
ENGINE_PARTITION_KEY "toYYYYMM(timestamp)"
ENGINE_SORTING_KEY "device_id, timestamp"
This schema design prioritizes fast querying by device ID and timestamp, crucial for real-time analytics. For data ingestion, Tinybird's Events API allows you to stream JSON/NDJSON events from your application frontend or backend with a simple HTTP request. This feature is especially useful for real-time data like that from home automation systems due to its low latency. Here's how you can ingest event data:
curl -X POST "https://api.europe-west2.gcp.tinybird.co/v0/events?name=home_automation_events&utm_source=DEV&utm_campaign=tb+create+--prompt+DEV" \
-H "Authorization: Bearer $TB_ADMIN_TOKEN" \
-d '{
"device_id": "thermo_123",
"device_type": "thermostat",
"room": "living_room",
"event_type": "temperature_reading",
"status": "active",
"value": 22.5,
"battery_level": 85.0,
"timestamp": "2023-06-15 14:30:00"
}'
Other relevant ingestion methods include the Kafka connector for event/streaming data and the Data Sources API and S3 connector for batch/file data.
Transforming data and publishing APIs
With the data ingested, the next step is to transform this data and publish APIs using pipes. Pipes in Tinybird allow for batch transformations (copies), real-time transformations (Materialized views), and creating API endpoints.
Room Temperature Endpoint
Let's start with an endpoint that returns the average temperature in each room with thermostats:
DESCRIPTION >
Returns the average temperature in each room that has thermostats
NODE room_temperature_node
SQL >
SELECT
room,
avg(value) as avg_temperature,
min(value) as min_temperature,
max(value) as max_temperature,
max(timestamp) as last_updated
FROM home_automation_events
WHERE device_type = 'thermostat'
AND timestamp >= now() - interval 1 day
GROUP BY room
ORDER BY room
TYPE endpoint
This SQL logic calculates the average, minimum, and maximum temperatures for each room, only considering thermostat devices. The API endpoint is flexible, allowing for date range filtering through query parameters. Example API call:
curl -X GET "https://api.europe-west2.gcp.tinybird.co/v0/pipes/room_temperature.json?token=%24TB_ADMIN_TOKEN&start_date=2023-06-01+00%3A00%3A00&end_date=2023-06-15+23%3A59%3A59&utm_source=DEV&utm_campaign=tb+create+--prompt+DEV"
Device History and Status Endpoints
Similar processes are followed to create Endpoints for device_history
and device_status
, which return historical data for a specific device and the current status of devices, respectively. Each endpoint uses a SQL query to filter and sort the data according to the request parameters.
Deploying to Production
To deploy your project to Tinybird Cloud, use the command:
tb --cloud deploy
This command creates production-ready, scalable API endpoints. Tinybird manages resources as code, which facilitates integration with CI/CD pipelines and ensures secure, token-based authentication for accessing the APIs. Example command to call the deployed endpoint:
curl -X GET "https://api.tinybird.co/v0/pipes/device_status.json?token=%24TB_ADMIN_TOKEN&device_type=thermostat&room=living_room&utm_source=DEV&utm_campaign=tb+create+--prompt+DEV"
Conclusion
In this tutorial, you've learned how to build a real-time API for monitoring home automation systems using Tinybird. You've seen how to ingest event data, transform it, and publish flexible, scalable APIs. Tinybird's capabilities enable you to manage and analyze real-time data efficiently, making it an excellent tool for developers looking to implement similar solutions. Sign up for Tinybird to build and deploy your first real-time data APIs in a few minutes. Tinybird is free to start, with no time limit and no credit card required, allowing you to explore its features and build powerful data-driven applications effortlessly.
Top comments (0)