跳转至

功能

这块Node Red 最常用了一些预先写好的节点信息, 目的是让大家拖拽就可以把自己想写的流程写出来

function

这个节点允许你直接写JavaScript来实现你想要的功能, 后面会单独开一章来将.

Template

是一个轻逻辑的模板语言,它可以使用 msg里面的属性来生成按照一个格式的文本,例如:

// function 节点信息
msg.payload = {}
msg.payload.title = '张三'
msg.payload.content= 'World'
return msg;

大家好,我叫{{payload.title}}
Hello {{payload.content}}
输出信息如下:
大家好,我叫张三
Hello World
node_red_function_1

delay

顾名思义是延迟消息到下一个节点的时间,这个主要有两种:

  • 延迟每一条消息
    每条消息延迟指定时间,这个适合对定时任务
  • 限制速率
    当有多条消息到这个节点的时候,会按照一定速率发送到下一个节点. 这个适合你想按照相隔多少时间下发到下一个节点.

node_red_function_2

[{"id":"68360605.862db8","type":"tab","label":"测试流程1","disabled":false,"info":""},{"id":"3256a421.49529c","type":"inject","z":"68360605.862db8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":112,"y":150,"wires":[["2b610f21.4dce"]]},{"id":"2b610f21.4dce","type":"delay","z":"68360605.862db8","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":260,"y":144,"wires":[["3d01d676.83160a"]]},{"id":"3d01d676.83160a","type":"debug","z":"68360605.862db8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":436,"y":140,"wires":[]},{"id":"b0821dc0.3ae36","type":"inject","z":"68360605.862db8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":91,"y":242,"wires":[["ff0f3d5e.a3cf3"]]},{"id":"1d4771b1.27193e","type":"debug","z":"68360605.862db8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":486,"y":239,"wires":[]},{"id":"ff0f3d5e.a3cf3","type":"delay","z":"68360605.862db8","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":277,"y":248,"wires":[["1d4771b1.27193e"]]}]

comment

用来写注释的,当你想在一些笔记或给指定节点做注释都可以用

switch

此节点可以按照自己写的判断条件,进入不同的流程里面.在没有任何条件满足的时候会丢弃当前消息,部分信息参考change节点 node_red_function_4

[{"id":"68360605.862db8","type":"tab","label":"测试流程1","disabled":false,"info":""},{"id":"9dc4018a.bee4e","type":"inject","z":"68360605.862db8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":108,"y":311,"wires":[["e22e0657.64dde8"]]},{"id":"e22e0657.64dde8","type":"change","z":"68360605.862db8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":284,"y":307,"wires":[["a12e63a8.b0afc"]]},{"id":"a12e63a8.b0afc","type":"switch","z":"68360605.862db8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"2","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":470,"y":300,"wires":[["af39065.d69bef8"],["cc4d3d48.a4ae4"]]},{"id":"af39065.d69bef8","type":"debug","z":"68360605.862db8","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":598,"y":236,"wires":[]},{"id":"cc4d3d48.a4ae4","type":"debug","z":"68360605.862db8","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":583,"y":337,"wires":[]}]

change

这个节点可以设置,删除,修改,转移 msg 里面的属性,例如: node_red_function_3

[{"id":"68360605.862db8","type":"tab","label":"测试流程1","disabled":false,"info":""},{"id":"9dc4018a.bee4e","type":"inject","z":"68360605.862db8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":108,"y":311,"wires":[["e22e0657.64dde8"]]},{"id":"e22e0657.64dde8","type":"change","z":"68360605.862db8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":265,"y":305,"wires":[["b2ec2b20.ec26c8"]]},{"id":"b2ec2b20.ec26c8","type":"debug","z":"68360605.862db8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":483,"y":300,"wires":[]}]

split

把消息内容按照一定条件分割成多条消息发送到下一个节点
node_red_function_5

[{"id":"68360605.862db8","type":"tab","label":"测试流程1","disabled":false,"info":""},{"id":"905d59f0.fd9cd8","type":"inject","z":"68360605.862db8","name":"","topic":"","payload":"[1,2,3,4,5]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":290,"wires":[["1d704def.b102c2"]]},{"id":"1d704def.b102c2","type":"split","z":"68360605.862db8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":278,"y":290,"wires":[["a0f0fa50.f84fc8"]]},{"id":"a0f0fa50.f84fc8","type":"debug","z":"68360605.862db8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":463,"y":283,"wires":[]}]

join

把多条消息按照条件合并成一条消息发送到下一个节点

node_red_function_6

[{"id":"68360605.862db8","type":"tab","label":"测试流程1","disabled":false,"info":""},{"id":"905d59f0.fd9cd8","type":"inject","z":"68360605.862db8","name":"","topic":"","payload":"[1,2,3,4,5]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":290,"wires":[["1d704def.b102c2"]]},{"id":"1d704def.b102c2","type":"split","z":"68360605.862db8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":278,"y":290,"wires":[["2985b5b5.30acda"]]},{"id":"a0f0fa50.f84fc8","type":"debug","z":"68360605.862db8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":463,"y":283,"wires":[]},{"id":"2985b5b5.30acda","type":"join","z":"68360605.862db8","name":"","mode":"auto","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":359,"y":431,"wires":[["a0f0fa50.f84fc8"]]}]

json

json对象和字符串互转, 这里有两个概念,啥时候需要使用字符串,啥时候需要转换成js对象(同json对象),首先你要明白他们的区别在哪里:

  • js对象,我们可以很方便的遍历数据或者根据相应key 获取指定的值或索引获取值
  • json字符串,我们是没办法使用json提供的那些方法,很快捷的获取指定的数值,只能使用字符串的方法,因为本质它是字符串
{"name":"张三" ,
  "age": 18, 
  "addr": "天堂",
  "loves" :[
      "1",
      "2"
  ]}

如果msg.payload 内容是上面的,你获取姓名则按照: msg.payload.name ,然后获取1 : msg.payload.loves[0], 因为计算机里面的索引都是从0开始的,请注意

sort

msg的内容进行排序发送到下一个节点

node_red_function_7

[{"id":"68360605.862db8","type":"tab","label":"测试流程1","disabled":false,"info":""},{"id":"905d59f0.fd9cd8","type":"inject","z":"68360605.862db8","name":"","topic":"","payload":"[1,2,3,4,5]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":290,"wires":[["8c6c2405.986378"]]},{"id":"a0f0fa50.f84fc8","type":"debug","z":"68360605.862db8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":463,"y":283,"wires":[]},{"id":"8c6c2405.986378","type":"sort","z":"68360605.862db8","name":"","order":"descending","as_num":false,"target":"payload","targetType":"msg","msgKey":"","msgKeyType":"elem","seqKey":"payload","seqKeyType":"msg","x":268,"y":290,"wires":[["a0f0fa50.f84fc8"]]}]