• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

Build a Real-Time Home Automation Monitoring API with Tinybird

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
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

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

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

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

. 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

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

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 "

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

" \
-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

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

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 (

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

), 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 "

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

"
Device History and Status Endpoints


Similar processes are followed to create

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

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 "

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

"
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.

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

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.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу