# Values Difference

**URL:** https://discourse.nodered.org/t/values-difference/45487
**Category:** General
**Created:** [11 May 2021 11:01 UTC](https://discourse.nodered.org/t/values-difference/45487 "2021-05-11T11:01:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jackie7](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jackie7](https://discourse.nodered.org/u/jackie7)
#### Post date: [11 May 2021 11:01 UTC](https://discourse.nodered.org/t/values-difference/45487/1 "2021-05-11T11:01:53Z")

</div>

Hey there!  
I would like to do a flow that calculates the difference between the current value of an inject node and the previous value (with delay of 1 minute).  
I tried to do 2 functions with the second one delayed for 1 minute, and then a function with a subtraction code, but somehow the result is NaN!  
how can I do this flow?  
Thank you,

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [11 May 2021 11:51 UTC](https://discourse.nodered.org/t/values-difference/45487/2 "2021-05-11T11:51:44Z")

</div>

Hi,  
As multiple messages NEVER arrive at a the input of node at the same time (meaning your 2 values to compare are never avaiable at the function at the same time) you either need to store the previous value (in context for example), use a join node or some other means of getting the values you wish to compare into the same message.

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

* * *

If you are new to node-red, I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<div class="post-metadata">

### Author: ![jackie7](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jackie7](https://discourse.nodered.org/u/jackie7)
#### Post date: [12 May 2021 06:54 UTC](https://discourse.nodered.org/t/values-difference/45487/3 "2021-05-12T06:54:02Z")

</div>

Hi,  
This is the flow I did, I want to calculate the difference of ValueA for an interval of 1 minute. What changes should I make to make that happen?  
Thank you,

```auto

[{"id":"23d311f.3d074ee","type":"tab","label":"Flow 11","disabled":false,"info":""},{"id":"2d5ba471.fa222c","type":"function","z":"23d311f.3d074ee","name":"Current Reading","func":"//set a topic to join later\nmsg.topic = 'curr'; \n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":300,"y":120,"wires":[["706bea03.89c674"]]},{"id":"b6139f33.34282","type":"delay","z":"23d311f.3d074ee","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":200,"y":320,"wires":[["c70554b8.d33f88"]]},{"id":"c70554b8.d33f88","type":"function","z":"23d311f.3d074ee","name":"Previous Reading","func":"//set a topic to join later\nmsg.topic = 'prev'; \n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":390,"y":320,"wires":[["706bea03.89c674"]]},{"id":"706bea03.89c674","type":"join","z":"23d311f.3d074ee","name":"Join each topic","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":620,"y":200,"wires":[["1ea8e754.6d5b99"]]},{"id":"1ea8e754.6d5b99","type":"function","z":"23d311f.3d074ee","name":"Difference","func":"var c = msg.payload.curr - msg.payload.prev;\n\n// Calculate\nif (msg.topic == 'curr'){\n msg.payload = c;\n return msg;\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":850,"y":200,"wires":[["58baa679.f5a198"]]},{"id":"f8c67071.d924a","type":"inject","z":"23d311f.3d074ee","name":"INJECT","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"ValueA\":90}","payloadType":"json","x":55,"y":220,"wires":[["2d5ba471.fa222c","b6139f33.34282"]],"l":false},{"id":"58baa679.f5a198","type":"debug","z":"23d311f.3d074ee","name":"Difference","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1050,"y":200,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [11 July 2021 06:54 UTC](https://discourse.nodered.org/t/values-difference/45487/4 "2021-07-11T06:54:32Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
