我有这个功能,可以解析在mqtt上收到的值。该值实际上是arduino发送的时间戳,并且是1234,1345等数字。
var parts = msg.payload.trim().split(/[ |]+/);
var update = parts[10];
msg.payload = update;
return msg;
我想要的实际上是最后一个值(在我的情况下是更新变量)是获取最后一个接收到的值与上一个之间的差。 基本上,如果我收到1234,然后收到1345,我想记住1234,函数返回的值为1345-1234 = 111。
谢谢
答案 0 :(得分:0)
如果您想存储一个值以便以后比较,则需要查看如何使用context来存储它。
上下文通常是用于命名变量的内存存储,但是它由一个API支持,该API可用于在两次重启之间持久保存上下文。
答案 1 :(得分:0)
我想提出一种替代方法。 Node-RED有一些核心节点,旨在跨序列工作,为此,它们保留了一个内部缓冲区。这些节点之一是批处理节点。像您这样的用例,可以利用此功能来存储值,因此不需要使用上下文存储器。我在下面共享的流使用一个批处理节点,该节点配置为将两个消息按顺序分组,这意味着它将始终向下游发送当前有效负载和前一个有效负载。然后,一个加入节点将按这样的顺序工作,以将有效负载减少到单个值,即时间戳之间的差。您需要打开每个节点的配置对话框,以充分了解如何设置这些节点以实现所需的目标。我将联接节点配置为应用修正表达式,以将有效载荷除以一千,因此您将获得以秒为单位的值(而不是毫秒)。
流量:
[{"id":"3121012f.c8a3ce","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"2ab0e0ba.9bd5f","type":"batch","z":"3121012f.c8a3ce","name":"","mode":"count","count":"2","overlap":"1","interval":10,"allowEmptySequence":false,"topics":[],"x":310,"y":280,"wires":[["342f97dd.23be08"]]},{"id":"17170419.f6b98c","type":"inject","z":"3121012f.c8a3ce","name":"","topic":"timedif","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":280,"wires":[["2ab0e0ba.9bd5f"]]},{"id":"342f97dd.23be08","type":"join","z":"3121012f.c8a3ce","name":"","mode":"reduce","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"payload-$A","reduceInit":"0","reduceInitType":"num","reduceFixup":"$A/1000","x":450,"y":280,"wires":[["e83170ce.56c08"]]},{"id":"e83170ce.56c08","type":"debug","z":"3121012f.c8a3ce","name":"Debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":600,"y":280,"wires":[]}]