前言
- node-red一共有三种变量
- context/flow/global
三种变量的使用请参考下列教程
node-red教程 5.4 context global与函数节点的其它功能
为何需要持久化存储变量
- node-red变量都存储在内存中,当涉及到需要长时间存储的变量就会蒙圈
修改settings.js
修改/opt/iobroker/node_modules/iobroker.node-red/settings.js在末尾添加下面代码,注意需要添加逗号与之前的配置隔开,
contextStorage: {
store: { module: "localfilesystem"},
default: { module: "memory" }
}
重启node-red
持久化存储变量演示
将一个时间戳存储为持久化变量的流程
{"id":"9bf6d5e9.701a68","type":"inject","z":"6469eaf7.924d94","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":380,"y":860,"wires":"24a980c7.40f58"]]},{"id":"24a980c7.40f58","type":"change","z":"6469eaf7.924d94","name":"","rules":{"t":"move","p":"payload","pt":"msg","to":"#:(store)::timestamp","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":880,"wires":]]}]
查看刚刚存的持久化变量,点击刷新
读取持久化变量演示
- 持久化存储的变量与存储在内存的变量是两个变量,即使它们名称完全一样
- 读取持久化变量的时候,需声明变量类型,否则会优先读取内存变量,即使这个变量不存在
{"id":"17b92e08.db1f12","type":"inject","z":"6fddb7c0.6e1238","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":620,"y":540,"wires":"ce9d5ed6.2510a"]]},{"id":"ce9d5ed6.2510a","type":"function","z":"6fddb7c0.6e1238","name":"读取开门时间","func":"var message = global.get('open_door_time', \"store\");\n\nmsg.payload = {};\nmsg.payload.data = {\"message\":message,\"miai_num\":\"0\"};\nreturn msg;\n","outputs":1,"noerr":0,"x":800,"y":560,"wires":"5f7fa373.528d2c"]]},{"id":"5f7fa373.528d2c","type":"debug","z":"6fddb7c0.6e1238","name":"调式信息","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1000,"y":560,"wires":]}]
持久化存储的秘密
;P存成文件了
参考文档