在NodeRED中全局保存值

时间:2018-08-04 05:10:24

标签: global-variables node-red

我们正在使用MQTT消息,处理这些消息并调用REST服务以将信息发送到另一个系统。

现在,我们需要保存MQTT消息中的某些值,以将这些值与其他消息进行比较。是否可以在工作流之外创建可以从其他工作流访问的数组?当前,我们将值保存到文件中,但这不是我们想要的方式。

3 个答案:

答案 0 :(得分:0)

听起来像您需要外部数据库或键值存储。

对于Node-RED,有很多数据库节点可以做到这一点,对于键值对,您可以使用redis之类的东西。

您可以在https://flows.nodered.org上搜索databaseredis节点。

答案 1 :(得分:0)

您可以设置全局变量,但是当您重新启动节点红色时,它们将不会保留。这是给你的例子。

[{"id":"5a6c6b8.2487294","type":"inject","z":"98c20df4.95abc","name":"","topic":"","payload":"val1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":590,"y":260,"wires":[["e62b621d.37897"]]},{"id":"f83255b0.19aa48","type":"inject","z":"98c20df4.95abc","name":"","topic":"","payload":"val2","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":590,"y":300,"wires":[["e62b621d.37897"]]},{"id":"e62b621d.37897","type":"change","z":"98c20df4.95abc","name":"","rules":[{"t":"set","p":"testvar","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":810,"y":280,"wires":[[]]},{"id":"c99e3c90.ae63d","type":"inject","z":"98c20df4.95abc","name":"","topic":"","payload":"val1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":590,"y":400,"wires":[["a72eed82.28ddd"]]},{"id":"940128d3.c5d0a8","type":"inject","z":"98c20df4.95abc","name":"","topic":"","payload":"val2","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":590,"y":440,"wires":[["a72eed82.28ddd"]]},{"id":"a72eed82.28ddd","type":"function","z":"98c20df4.95abc","name":"","func":"var compare = global.get(\"testvar\");\n\nif(typeof(compare)=='undefined'){\n    //Good idea to check if it's been set so you don't get 'undefined' errors\n    node.status({text:\"Global var has not been set yet\"});\n}else if(msg.payload == compare){\n    node.status({text:\"Same\"});\n}else{\n    node.status({text:\"NOT the same\"});\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":767.01953125,"y":415.00390625,"wires":[[]]},{"id":"997cc65f.c8d238","type":"comment","z":"98c20df4.95abc","name":"Set the Global var here","info":"","x":560,"y":220,"wires":[]},{"id":"34e55934.227c16","type":"comment","z":"98c20df4.95abc","name":"Test the Global var here","info":"","x":560,"y":360,"wires":[]},{"id":"81bed25f.6022a","type":"inject","z":"98c20df4.95abc","name":"","topic":"","payload":"val1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":590,"y":560,"wires":[["164c525e.b4f6ce"]]},{"id":"d03c41c8.088a","type":"inject","z":"98c20df4.95abc","name":"","topic":"","payload":"val2","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":590,"y":600,"wires":[["164c525e.b4f6ce"]]},{"id":"a4cd5575.a4cef8","type":"comment","z":"98c20df4.95abc","name":"Another way to set the Global var","info":"","x":590,"y":520,"wires":[]},{"id":"164c525e.b4f6ce","type":"function","z":"98c20df4.95abc","name":"","func":"global.set(\"testvar\",msg.payload);\n\nreturn msg;","outputs":1,"noerr":0,"x":770,"y":580,"wires":[[]]}]

答案 2 :(得分:0)

尝试node-red-contrib-state,它可以在node-red重新启动后保持状态,并具有其他一些状态管理工具。

如果您有权访问mqtt代理,则mqtt还可用于保存状态。