Node-RED 物联网实践系列(4.1):核心节点精讲(Inject/Debug/Change/Switch/Function)

编辑器与消息调试,来源:https://nodered.org/docs/user-guide/messages

目标

  • 熟练掌握核心节点:Inject、Debug、Change、Switch、Function
  • 理解 msg 消息结构与常见路由/清洗模式
  • 通过可导入 Flow 快速上手并验证结果

消息结构与调试

  • 消息对象:msg,默认使用 msg.payload
  • 使用 Debug 节点观察结构与类型,支持查看完整消息

来源:https://nodered.org/docs/user-guide/messages

示例一:时间戳→清洗→调试

描述:每秒生成一个时间戳,将其转为可读字符串并输出到 Debug。

导入以下 JSON,Deploy 后观察右侧 Debug 面板:

json 复制代码
[
  {
    "id": "i1",
    "type": "inject",
    "z": "f1",
    "name": "every second",
    "props": [{ "p": "payload" }],
    "repeat": "1",
    "once": true,
    "payloadType": "date",
    "x": 170,
    "y": 120,
    "wires": [["c1"]]
  },
  {
    "id": "c1",
    "type": "change",
    "z": "f1",
    "name": "format date",
    "rules": [
      {
        "t": "set",
        "p": "payload",
        "pt": "msg",
        "to": "\"$millis := $.payload;\n$moment($millis).format('YYYY-MM-DD HH:mm:ss')\"",
        "tot": "jsonata"
      }
    ],
    "x": 360,
    "y": 120,
    "wires": [["d1"]]
  },
  {
    "id": "d1",
    "type": "debug",
    "z": "f1",
    "name": "out",
    "active": true,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "payload",
    "targetType": "msg",
    "x": 540,
    "y": 120,
    "wires": []
  }
]

示例二:Switch 条件路由与告警高亮

描述:温度数据 >= 30 走告警分支,否则走正常分支。

json 复制代码
[
  {
    "id": "inj",
    "type": "inject",
    "z": "f2",
    "name": "temp=28",
    "props": [{"p":"payload"}],
    "payload": "{\"temperature\":28}",
    "payloadType": "json",
    "x": 150,
    "y": 180,
    "wires": [["sw"]]
  },
  {
    "id": "sw",
    "type": "switch",
    "z": "f2",
    "name": ">=30?",
    "property": "payload.temperature",
    "rules": [
      {"t": ">=", "v": "30", "vt": "num"},
      {"t": "else"}
    ],
    "checkall": "true",
    "repair": false,
    "x": 330,
    "y": 180,
    "wires": [["chg"],["dbg"]]
  },
  {
    "id": "chg",
    "type": "change",
    "z": "f2",
    "name": "set alert",
    "rules": [
      {"t":"set","p":"topic","pt":"msg","to":"alert","tot":"str"},
      {"t":"set","p":"payload","pt":"msg","to":"\"HIGH TEMP\"","tot":"jsonata"}
    ],
    "x": 500,
    "y": 160,
    "wires": [["dbg"]]
  },
  {
    "id": "dbg",
    "type": "debug",
    "z": "f2",
    "name": "out",
    "active": true,
    "tosidebar": true,
    "console": false,
    "tostatus": true,
    "complete": "true",
    "targetType": "full",
    "x": 680,
    "y": 180,
    "wires": []
  }
]

示例三:Function 节点与上下文计数

描述:统计消息到达次数,并将计数返回到 msg.payload

json 复制代码
[
  {
    "id": "inj3",
    "type": "inject",
    "z": "f3",
    "name": "trigger",
    "props": [{"p":"payload"}],
    "payload": "1",
    "payloadType": "num",
    "repeat": "1",
    "once": true,
    "x": 150,
    "y": 260,
    "wires": [["fn"]]
  },
  {
    "id": "fn",
    "type": "function",
    "z": "f3",
    "name": "counter",
    "func": "let counter = context.get('counter') || 0;\ncounter += 1;\ncontext.set('counter', counter);\nmsg.payload = counter;\nreturn msg;",
    "outputs": 1,
    "noerr": 0,
    "initialize": "// On Start 里可初始化 context 值\nif (context.get('counter') === undefined) {\n  context.set('counter', 0)\n}",
    "libs": [],
    "x": 330,
    "y": 260,
    "wires": [["dbg3"]]
  },
  {
    "id": "dbg3",
    "type": "debug",
    "z": "f3",
    "name": "out",
    "active": true,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "payload",
    "targetType": "msg",
    "x": 520,
    "y": 260,
    "wires": []
  }
]

常用技巧

  • Change + JSONata:在不写 JS 的情况下高效清洗/映射
  • Switch:选择 checkall=true 支持多条件依次匹配
  • Function:优先修改原始 msg,避免丢失链路信息(如 HTTP req/res)
  • On Start/On Stop:在 Function 编辑器对应面板中做初始化与清理

参考

相关推荐
canonical_entropy9 小时前
AI Agent 的演进之路:从对话到自主代理操作系统
低代码·aigc·agent
Java小卷2 天前
流程设计器为啥选择diagram-js
前端·低代码·工作流引擎
一枚前端小姐姐3 天前
低代码平台表单设计系统技术分析(实战三)
前端·vue.js·低代码
一枚前端小姐姐4 天前
低代码平台表单设计系统技术分析(实战二)
低代码·架构·前端框架
一枚前端小姐姐4 天前
低代码平台表单设计系统架构分析(实战一)
前端·低代码·架构
麦聪聊数据5 天前
统一 Web SQL 平台如何收编企业内部的“野生数据看板”?
数据库·sql·低代码·微服务·架构
古译汉书5 天前
【IoT死磕系列】Day 7:只传8字节怎么控机械臂?学习工业控制 CANopen 的“对象字典”(附企业级源码)
数据结构·stm32·物联网·http
TDengine (老段)5 天前
TDengine IDMP 数据可视化——散点图
大数据·数据库·物联网·信息可视化·时序数据库·tdengine·涛思数据
Lupino5 天前
从逻辑“脑裂”到 AI 重构:不到 2 美金解决物联网电位反转难题
python·物联网