# Node-Red compare new and previous values

**URL:** <https://discourse.nodered.org/t/node-red-compare-new-and-previous-values/8215>\
**Category:** General\
**Created:** [21 February 2019 10:40 UTC](https://discourse.nodered.org/t/node-red-compare-new-and-previous-values/8215 "2019-02-21T10:40:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vovabcps](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vovabcps/32/6179_2.png) [@vovabcps](https://discourse.nodered.org/u/vovabcps)\
**Post date:** [21 February 2019 10:40 UTC](https://discourse.nodered.org/t/node-red-compare-new-and-previous-values/8215/1 "2019-02-21T10:40:15Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/0b10deca7e6ba25d3481939a81027bd7fe443406.png)

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [21 February 2019 10:47 UTC](https://discourse.nodered.org/t/node-red-compare-new-and-previous-values/8215/2 "2019-02-21T10:47:20Z")

</div>

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](https://discourse.nodered.org/t/store-a-value-in-a-function/990?u=ukmoose)

---

<div class="post-metadata">

**Author:** ![RoganFPS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/roganfps/32/3208_2.png) [@RoganFPS](https://discourse.nodered.org/u/RoganFPS)\
**Post date:** [21 February 2019 11:44 UTC](https://discourse.nodered.org/t/node-red-compare-new-and-previous-values/8215/3 "2019-02-21T11:44:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [21 February 2019 11:55 UTC](https://discourse.nodered.org/t/node-red-compare-new-and-previous-values/8215/4 "2019-02-21T11:55:47Z")

</div>

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:

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

```
