用户
 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

11

主题

15

帖子

5594

积分

论坛元老

Rank: 8Rank: 8

积分
5594
2019-9-2 20:45:28 lidicn 论坛元老 楼主 2205
本帖最后由 lidicn 于 2019-9-2 20:51 编辑

前言

  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

持久化存储变量演示

173824z3npucr50c3zothq.jpg

将一个时间戳存储为持久化变量的流程

[{"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":[]}]

持久化存储的秘密

存成文件了



参考文档
  • https://nodered.org/docs/api/context/





使用道具 举报 回复
我们的l大还是一如既往的牛逼啊
使用道具 举报 回复 支持 反对
拿来存token不错
使用道具 举报 回复 支持 反对
发新帖
您需要登录后才可以回帖 登录 | 立即注册