我们正在处理MQTT消息,处理这些消息,并调用REST服务将信息发送到另一个系统。
现在,我们需要保存MQTT消息中的一些值,以便将这些值与其他消息进行比较。是否可以在工作流之外创建一个可以从其他工作流访问的数组?目前,我们正在将值保存到一个文件中,但这不是我们想要的方式
听起来您需要一个外部数据库或键值存储。
Node RED有很多数据库节点可以做到这一点,或者对于键值对,可以使用redis之类的东西。
您可以在上搜索https://flows.nodered.org用于数据库和redis节点。
您可以设置全局变量,但当您重新启动node red时,它们不会被保留。这里有一个例子。
[{"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");nnif(typeof(compare)=='undefined'){n //Good idea to check if it's been set so you don't get 'undefined' errorsn 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}nnreturn 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);nnreturn msg;","outputs":1,"noerr":0,"x":770,"y":580,"wires":[[]]}]
尝试node red contrib state它在node red重新启动时保持状态,并有一些其他用于状态管理的工具。
如果您有权访问mqtt代理,那么mqtt也有利于保存状态。