# Compare two values to generate a difference value

**URL:** <https://discourse.nodered.org/t/compare-two-values-to-generate-a-difference-value/46890>\
**Category:** General\
**Created:** [9 June 2021 07:52 UTC](https://discourse.nodered.org/t/compare-two-values-to-generate-a-difference-value/46890 "2021-06-09T07:52:08Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [9 June 2021 07:52 UTC](https://discourse.nodered.org/t/compare-two-values-to-generate-a-difference-value/46890/1 "2021-06-09T07:52:08Z")

</div>

Hi,  
I've two incoming temp sensor values, let's call them hi and lo, they come from different devices so don't arrive at the same time.  
How can I subtract the 'lo' value from the 'hi' value to create a new value called 'diff'?

I have attempted to do this in a function node, but I'm not very good with the syntax, also the input sensors don't output at the same time. Should I bestoring the sensors in a global (I can do that) but then how do I carry out the calculation? It's output can be stored as a global too for use in other flows.  
Thanks

---

<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:** [9 June 2021 07:52 UTC](https://discourse.nodered.org/t/compare-two-values-to-generate-a-difference-value/46890/2 "2021-06-09T07:52:37Z")

</div>

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

EDIT - sorry, i see you store them in global (that is a viable alternative to join node)

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [9 June 2021 07:53 UTC](https://discourse.nodered.org/t/compare-two-values-to-generate-a-difference-value/46890/3 "2021-06-09T07:53:45Z")

</div>

Hi Steve, the problem is they don't come in at the same time. I can store the latest value in a global, but how do I carry out the calculation? Could do with some help with the Function node syntax... It's my kryptonite...

---

<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:** [9 June 2021 07:58 UTC](https://discourse.nodered.org/t/compare-two-values-to-generate-a-difference-value/46890/4 "2021-06-09T07:58:59Z")

</div>

You can use the join node...

Demo

```auto
[{"id":"8ccddb9a.a55f38","type":"inject","z":"30e2e9e0.eeaab6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"hi","payload":"100","payloadType":"num","x":1330,"y":880,"wires":[["47b769c5.cb0e28"]]},{"id":"47b769c5.cb0e28","type":"join","z":"30e2e9e0.eeaab6","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1490,"y":900,"wires":[["0e7a60dec064d06d"]]},{"id":"f9afb265.b11b7","type":"debug","z":"30e2e9e0.eeaab6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1770,"y":900,"wires":[]},{"id":"2d269127.4f04ce","type":"inject","z":"30e2e9e0.eeaab6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"lo","payload":"15","payloadType":"num","x":1330,"y":920,"wires":[["47b769c5.cb0e28"]]},{"id":"0e7a60dec064d06d","type":"function","z":"30e2e9e0.eeaab6","name":"","func":"msg.topic = \"diff\"\nmsg.payload = msg.payload.hi - msg.payload.lo;\nreturn msg;","outputs":1,"noerr":1,"initialize":"","finalize":"","libs":[],"x":1620,"y":900,"wires":[["f9afb265.b11b7"]]}]

```

or you can use a function to grab the globals and do the math

```auto
[{"id":"f9afb265.b11b7","type":"debug","z":"30e2e9e0.eeaab6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1750,"y":900,"wires":[]},{"id":"0e7a60dec064d06d","type":"function","z":"30e2e9e0.eeaab6","name":"","func":"msg.topic = \"diff\"\n\nvar hi = global.get(\"hi\");\nvar lo = global.get(\"lo\");\n\nmsg.payload = hi - lo;\n\nreturn msg;","outputs":1,"noerr":1,"initialize":"","finalize":"","libs":[],"x":1580,"y":900,"wires":[["f9afb265.b11b7"]]},{"id":"95b54c27bb57d871","type":"inject","z":"30e2e9e0.eeaab6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"hi","payload":"100","payloadType":"num","x":1430,"y":800,"wires":[["6c1a1eeb3b1427fb"]]},{"id":"431f7653eae82780","type":"inject","z":"30e2e9e0.eeaab6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"lo","payload":"15","payloadType":"num","x":1430,"y":840,"wires":[["b8928fa8aec87fab"]]},{"id":"6c1a1eeb3b1427fb","type":"change","z":"30e2e9e0.eeaab6","name":"","rules":[{"t":"set","p":"hi","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1590,"y":800,"wires":[[]]},{"id":"b8928fa8aec87fab","type":"change","z":"30e2e9e0.eeaab6","name":"","rules":[{"t":"set","p":"lo","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1590,"y":840,"wires":[[]]},{"id":"fb2b743f2ccbdfe6","type":"inject","z":"30e2e9e0.eeaab6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":1430,"y":900,"wires":[["0e7a60dec064d06d"]]}]

```

You could even use a JSONata expression in a change node.

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [9 June 2021 08:35 UTC](https://discourse.nodered.org/t/compare-two-values-to-generate-a-difference-value/46890/5 "2021-06-09T08:35:59Z")

</div>

Got it. Thanks for the syntax.  
I'll post my solution (Once I've tidied it up a bit...)

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [9 June 2021 08:43 UTC](https://discourse.nodered.org/t/compare-two-values-to-generate-a-difference-value/46890/6 "2021-06-09T08:43:30Z")

</div>

How do I change this line to allow one decimal place 0.0? It's current a whole number but I could do with one place right.  
`global.set("fridge_diff", Math.round(parseFloat(diff)) );`  
Thanks

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [9 June 2021 10:17 UTC](https://discourse.nodered.org/t/compare-two-values-to-generate-a-difference-value/46890/7 "2021-06-09T10:17:01Z")

</div>

> [@dougle03](#):
>
> Math.round(parseFloat(diff)

Math.round( parseFloat(diff) \* 10 ) / 10

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [9 June 2021 11:01 UTC](https://discourse.nodered.org/t/compare-two-values-to-generate-a-difference-value/46890/8 "2021-06-09T11:01:19Z")

</div>

Magic, although I have no idea why that works....  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/a/8a5e767f14e81f24b68ef90aaa707a66b6866040.png)

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [9 June 2021 11:14 UTC](https://discourse.nodered.org/t/compare-two-values-to-generate-a-difference-value/46890/9 "2021-06-09T11:14:13Z")

</div>

Is there a better way to trigger a flow based on a Global variable than using an inject node?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/9/8997213ec49b3dcc14b94903c5eb61c8930c7392.png)

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [9 June 2021 11:25 UTC](https://discourse.nodered.org/t/compare-two-values-to-generate-a-difference-value/46890/10 "2021-06-09T11:25:21Z")

</div>

Behind the scenes:  
`parseFloat(diff)` -\> `"3.278942"` will be `3.278942`

If the `diff` is type of string (which contains numbers) it will be converted to number type. Math operations can't be made with strings.  
If you are sure that the type of `diff` is number always, this can freely be omitted.

Then multiply by ten  
`parseFloat(diff) * 10` -\> `3.278942` will be `32.78942`

Then round that result  
`Math.round(32.78942)` -\> 33

And then divide it by ten  
`33 / 10` -\> 3.3

And no more magic 🙂

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [9 June 2021 11:38 UTC](https://discourse.nodered.org/t/compare-two-values-to-generate-a-difference-value/46890/11 "2021-06-09T11:38:27Z")

</div>

> [@dougle03](#):
>
> Is there a better way to trigger a flow based on a Global variable

Yes, run it at the point the variable changes.

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [9 June 2021 12:49 UTC](https://discourse.nodered.org/t/compare-two-values-to-generate-a-difference-value/46890/12 "2021-06-09T12:49:38Z")

</div>

Did just try that but the trigger passes the global variable into the following flow which the upper function does not pass outwards... Wish I was better with these function nodes.... I suspect I could get rid of many of the supporting nodes wrapping their functions up into a single node...

---

<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:** [9 June 2021 12:54 UTC](https://discourse.nodered.org/t/compare-two-values-to-generate-a-difference-value/46890/13 "2021-06-09T12:54:27Z")

</div>

> [@dougle03](#):
>
> the upper function does not pass outwards

then put `return msg` at the end of the function.

TBH, you probably dont even need to use an global context (it is almost always the last place I go when looking for a solution)

Use a join node for the temp long and temp short (set join to key/value, 2 parts) then in the set diff function do your maths & set msg.payload = diff, then return that msg & connect the function to where the inject goes. No need for any globals.

If you do need the values further down in your flow, store them in the `msg` e.g. `msg.temp = temp` then later on you can easily access the other values from the msg - no need for setting and getting global context.

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [9 June 2021 14:16 UTC](https://discourse.nodered.org/t/compare-two-values-to-generate-a-difference-value/46890/14 "2021-06-09T14:16:22Z")

</div>

Ooh, I think that's a few steps beyond my non-programmer level of competence, although I'll have a go at the first part to remove the inject node.  
I like NodeRed because I can get stiff done quickly without having to battle through endless syntax errors that have historically kept me away from code level of computing. NR is slowly easing my 50yo knackered brain into coding... lol

---

<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:** [23 June 2021 14:16 UTC](https://discourse.nodered.org/t/compare-two-values-to-generate-a-difference-value/46890/15 "2021-06-23T14:16:43Z")

</div>

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