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 编辑器对应面板中做初始化与清理

参考

相关推荐
数据库学啊2 小时前
物联网(IoT)项目,时序数据库选型推荐哪个?
数据库·物联网·时序数据库
DolphinDB智臾科技3 小时前
工业物联网时序数据库选型指南:DolphinDB vs InfluxDB vs TimescaleDB
数据库·物联网·时序数据库
OpenTiny社区3 小时前
低代码运行时渲染搞不懂?TinyEngine 从理论到实践全攻略,看完直接上手!
前端·vue.js·低代码
捷米特网关模块通讯3 小时前
EtherNet/IP主站转Modbus TCP智能网关:锂电化成车间多仪表零丢包改造实录
数据采集·罗克韦尔plc·工业自动化·工业智能网关·网关模块
鲁邦通物联网4 小时前
边缘计算实战:如何并发采集S7、MC、FINS协议并转MQTT?
边缘计算·数据采集·工业数据采集·边缘网关·边缘计算网关·5g数采
广东大榕树信息科技有限公司7 小时前
国产化动环监控系统在数据中心安全中的作用
网络·物联网·国产动环监控系统·动环监控系统
小李做物联网8 小时前
26.3基于stm32单片机毕业设计物联网软硬件智能遮阳棚设计
stm32·单片机·嵌入式硬件·物联网
沫儿笙8 小时前
kuka库卡弧焊接机器人保护气节气装置
人工智能·物联网·机器人
Qoitech 中国9 小时前
Otii 应用场景系列:使用 Otii Arc和Otii Ace进行差分测量
嵌入式硬件·物联网·自动化·集成测试·智能硬件