Node-Red compare new and previous values

#1

I'm new to Node-Red and I don't know if that's possible without some storage (SQL database for example).
So, I have an MQTT endpoint that pushes numeric value when update made.
For example - message.payload = 799.
How to on every new message compare this new value with the previous one (the one that was received right before) without any external storage?kIPZi

0 Likes

#2

Have you read the section on context variables in the docs?

And then read this thread in the FAQ section Store a value in a function

1 Like

#3

There is also the RBE node which is report by exception which should achieve this!

0 Likes

#4

Another solution, without using storage. Using a join node to put the values into an array and then a change node to subtract the members of the array:

[{"id":"af1019af.e762c8","type":"debug","z":"e8e5ccfb.8b54e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":630,"y":140,"wires":[]},{"id":"d6ad51ff.f94b4","type":"inject","z":"e8e5ccfb.8b54e","name":"","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":140,"wires":[["729a39b.4a576c8"]]},{"id":"729a39b.4a576c8","type":"join","z":"e8e5ccfb.8b54e","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":300,"y":140,"wires":[["871e9009.b424"]]},{"id":"871e9009.b424","type":"change","z":"e8e5ccfb.8b54e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[0]-payload[1]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":140,"wires":[["af1019af.e762c8"]]},{"id":"3561fcbd.d33044","type":"inject","z":"e8e5ccfb.8b54e","name":"","topic":"","payload":"4","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":180,"wires":[["729a39b.4a576c8"]]}]
0 Likes