docs/en/10-third-party/09-iot/01-Node-RED.md
Node-RED is an open-source visual programming tool developed by IBM based on Node.js. It enables users to assemble and connect various nodes via a graphical interface to create connections for IoT devices, APIs, and online services. It supports multiple protocols and is cross-platform, has an active community, and is ideal for event-driven application development in smart home, industrial automation, and other scenarios. Its main strengths are low-code and visual programming.
The deep integration between TDengine and Node-RED provides a comprehensive solution for industrial IoT scenarios. Through Node-RED's MQTT/OPC UA/Modbus protocol nodes, data from PLCs, sensors, and other devices can be collected with millisecond-level latency. Real-time queries in TDengine can trigger physical control actions such as relay operations and valve switching, enabling immediate command execution.
node-red-node-tdengine is the official plugin developed by TDengine for Node-RED. It is composed of two nodes:
Prepare the following environment components:
Component interaction diagram:
Plugin data sources are configured in the node properties using the Node.js connector:
ws://user:password@host:port.ws://host:port.A workshop has multiple smart power meters, each generating a data record every second. Data is stored in TDengine, with real-time calculations performed every minute showing:
Implementation uses Node-RED + TDengine:
Assumptions:
www.example.com.Use taos-CLI to manually create the data model:
create database test;
create stable test.meters (ts timestamp, current float, voltage int, phase float)
tags (groupid int, location varchar(24));
create table test.d0 using test.meters tags(1, 'workshop1');
create table test.d1 using test.meters tags(2, 'workshop1');
create table test.d2 using test.meters tags(2, 'workshop2');
This example uses randomly generated numbers to simulate real device data. The tdengine-operator node is configured with TDengine data source connection information, writes data to TDengine, and uses the debug node to monitor the number of successfully written records displayed on the interface.
Steps:
Add Writer Node
Select the tdengine-operator node in the node palette and drag it to the canvas.
Double-click the node to open property settings, fill in the name as 'td-writer', and click the "+" icon to the right of the database field.
In the pop-up window:
ws://root:taosdata@www.example.com:6041
Click "Add" and return.
Simulate Device Data
Select the 'function' node from the palette and drag it before 'td-writer' on the canvas.
Double-click the node:
// Generate random values
const current = Math.floor(Math.random() * (30 - 5 + 1)) + 5; // 5-30A
const voltage = Math.floor(Math.random() * (240 - 198 + 1)) + 198; // 198-240V
const phase = Math.floor(Math.random() * (3 - 1 + 1)) + 1; // 1-3
// Create SQL
msg.topic = `insert into test.d0 values (now, ${current}, ${voltage}, ${phase});`;
return msg;
Drag an "inject" node before 'write d0'.
Configure the inject node:
Repeat steps 1-4 for other devices (d1, d2).
Add Output Monitor
After adding all nodes, connect them in sequence to form a pipeline. Click "Deploy" to publish changes. When running successfully:
Successful write output:
{
"topic": "insert into test.d1 values (now, 20, 203, 2);",
"isQuery": false,
"payload": {
"affectRows": 1,
"totalTime": 2,
"timing": "961982"
}
}
The data query workflow consists of three nodes (inject/tdengine-operator/debug) designed to calculate the average current, voltage, and power consumption per minute for each smart meter.
The inject node triggers the query request every minute. The results are sent to the downstream debug node, which displays the count of successful query executions.
Steps:
Drag an inject node to the canvas:
select tbname, avg(current), avg(voltage), sum(p)
from ( select tbname,current,voltage,current*voltage/60 as p from test.meters
where ts > now-60s partition by tbname)
group by tbname;
Drag tdengine-operator node to canvas:
Drag debug node to canvas and configure it:
Connect nodes sequentially and click "Deploy".
When the flow is successfully started:
Output from 'td-reader' (exceptions thrown on failure):
{
"topic": "select tbname,avg(current) ...",
"isQuery": true,
"payload": [
{
"tbname": "d2",
"avg(current)": 26.7,
"avg(voltage)": 235,
"sum(p)": 6329
},
{
"tbname": "d0",
"avg(current)": 16.5,
"avg(voltage)": 222,
"sum(p)": 121
},
{
"tbname": "d1",
"avg(current)": 29,
"avg(voltage)": 202,
"sum(p)": 5833
}
]
}
The data subscription workflow consists of two nodes (tdengine-consumer/debug) that provide equipment overload alert functionality.
The debug node visually displays the count of subscription messages pushed downstream. In production, replace it with functional nodes to process the subscription data.
Steps:
Manually create a subscription topic "topic_overload" using taos-CLI:
create topic topic_overload as
select tbname,* from test.meters
where current > 25 or voltage > 230;
Drag tdengine-consumer node to canvas:
ws://www.example.com:6041.Drag debug node to canvas and configure it:
Connect nodes sequentially and click "Deploy".
When operational:
Alert output from 'td-consumer':
{
"topic": "topic_overload",
"payload": [
{
"tbname": "d1",
"ts": "1750140456777",
"current": 31,
"voltage": 217,
"phase": 2,
"groupid": 4,
"location": "California.MountainView"
}
],
"database": "test",
"vgroup_id": 4,
"precision": 0
}
Errors in data collection, querying, and subscription workflows are routed to catch nodes for handling in Node-RED. To implement error monitoring:
When errors occur:
Complete workflow overview after deployment:
This article demonstrates, through an industrial monitoring scenario:
Three integration patterns between Node-RED and TDengine:
Complete error handling mechanisms.
Production-ready deployment reference architecture.
This article focuses on an example-based introduction. For complete documentation, refer to the Node-RED node's online documentation.