让node-red可持久化存储变量

前言

  1. node-red一共有三种变量
  2. context/flow/global

三种变量的使用请参考下列教程

node-red教程 5.4 context global与函数节点的其它功能

为何需要持久化存储变量

  1. 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":]]}]

查看刚刚存的持久化变量,点击刷新
TIM截图20190902205103.jpg

读取持久化变量演示

  1. 持久化存储的变量与存储在内存的变量是两个变量,即使它们名称完全一样
  2. 读取持久化变量的时候,需声明变量类型,否则会优先读取内存变量,即使这个变量不存在
{"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存成文件了

参考文档

我们的l大还是一如既往的牛逼啊

拿来存token不错:lol


请问 这个持久化用函数表达不行么,只能用change节点吗?

测试过了,可以这样操作的。

我又测试了一下 果然可以 :grin:

我直接存到txt中,用的时候再读,哈哈哈

这个太有用了,最近正在为一重启全局变量就没了这事烦恼呢