# Compare two payloads

**URL:** https://discourse.nodered.org/t/compare-two-payloads/66825
**Category:** General
**Tags:** function-node
**Created:** [23 August 2022 21:30 UTC](https://discourse.nodered.org/t/compare-two-payloads/66825 "2022-08-23T21:30:30Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![lourdes98](https://avatars.discourse-cdn.com/v4/letter/l/e47c2d/32.png) [@lourdes98](https://discourse.nodered.org/u/lourdes98)
#### Post date: [23 August 2022 21:30 UTC](https://discourse.nodered.org/t/compare-two-payloads/66825/1 "2022-08-23T21:30:30Z")

</div>

Hi! I'm using the 16.17.0 version of Node.JS and Node-RED v2.2.2.

I want to read a value coming from payload, save it and compare it with the next one. Both comes from the same node.

Then, execute a routine inside of a function node.

Can ypu help me with this+?

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [23 August 2022 22:19 UTC](https://discourse.nodered.org/t/compare-two-payloads/66825/2 "2022-08-23T22:19:37Z")

</div>

As they are distinct messages you need to save one somewhere until the second one comes in

You have two choices - you can use a context variable (read up on flow context variables) or you can try and use the join node before your function node - however if both values are coming from the same node it may be difficult to differentiate them properly for the join node to work

Craig

---

<div class="post-metadata">

### Author: ![MecatronicaMADE](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@MecatronicaMADE](https://discourse.nodered.org/u/MecatronicaMADE)
#### Post date: [23 August 2022 23:54 UTC](https://discourse.nodered.org/t/compare-two-payloads/66825/3 "2022-08-23T23:54:40Z")

</div>

Hi @lourdes98 and welcome to Node-RED community, as @craigcurtin said, you can use variables to store your values, I share you a flow with a little example:

```auto
[{"id":"54e35e363e1805cc","type":"tab","label":"Flow 9","disabled":false,"info":"","env":[]},{"id":"1f83928af45a3962","type":"group","z":"54e35e363e1805cc","name":"Simulation same node (different/equal payload)","style":{"label":true},"nodes":["9a5efdaae27e09b9","ed7d0f6563825d7d"],"x":54,"y":259,"w":301,"h":122},{"id":"9a5efdaae27e09b9","type":"inject","z":"54e35e363e1805cc","g":"1f83928af45a3962","name":"payload = 1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":170,"y":300,"wires":[["ca5884a7a3cdb668"]]},{"id":"ca5884a7a3cdb668","type":"switch","z":"54e35e363e1805cc","name":"","property":"flag","propertyType":"flow","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":430,"y":320,"wires":[["b170d284034311a3"],["73b4643553a5727e"]]},{"id":"a7ae19ed29879601","type":"function","z":"54e35e363e1805cc","name":"Set flow.flag","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nflow.set(\"flag\", true);","finalize":"","libs":[],"x":190,"y":200,"wires":[[]]},{"id":"b170d284034311a3","type":"change","z":"54e35e363e1805cc","name":"set val1 (Payload 1)","rules":[{"t":"set","p":"var1","pt":"flow","to":"payload","tot":"msg"},{"t":"set","p":"flag","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":300,"wires":[[]]},{"id":"73b4643553a5727e","type":"change","z":"54e35e363e1805cc","name":"set val2 (Payload 2)","rules":[{"t":"set","p":"var2","pt":"flow","to":"payload","tot":"msg"},{"t":"set","p":"flag","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":340,"wires":[["cb8110b5f8154455"]]},{"id":"cb8110b5f8154455","type":"switch","z":"54e35e363e1805cc","name":"Comparation","property":"var1","propertyType":"flow","rules":[{"t":"eq","v":"var2","vt":"flow"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":830,"y":340,"wires":[["ad025ba930cef847"],["c96a04c9dda328e4"]]},{"id":"ed7d0f6563825d7d","type":"inject","z":"54e35e363e1805cc","g":"1f83928af45a3962","name":"Payload = 2 or 1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":180,"y":340,"wires":[["ca5884a7a3cdb668"]]},{"id":"8fd99dfe4918836a","type":"debug","z":"54e35e363e1805cc","name":"debug 13","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1140,"y":340,"wires":[]},{"id":"ad025ba930cef847","type":"change","z":"54e35e363e1805cc","name":"message 1","rules":[{"t":"set","p":"payload","pt":"msg","to":"First and second payload are equal...","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":990,"y":320,"wires":[["8fd99dfe4918836a"]]},{"id":"c96a04c9dda328e4","type":"change","z":"54e35e363e1805cc","name":"message 2","rules":[{"t":"set","p":"payload","pt":"msg","to":"First and second payload are different...","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":990,"y":360,"wires":[["8fd99dfe4918836a"]]}]

```

In this example I compare two payloads, simulating that comes from the same node. I did this example with numbers, but can work with other kinds...

I hope it helps you!!

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [24 August 2022 00:33 UTC](https://discourse.nodered.org/t/compare-two-payloads/66825/4 "2022-08-24T00:33:16Z")

</div>

> [@lourdes98](#):
>
> I want to read a value coming from payload, save it and compare it with the next one.

If this comparison is a relatively simple one, for example if a numeric payload changes by 10% or more, then the Filter node can do the job, no need to store the payload in a variable.

---

<div class="post-metadata">

### Author: ![lourdes98](https://avatars.discourse-cdn.com/v4/letter/l/e47c2d/32.png) [@lourdes98](https://discourse.nodered.org/u/lourdes98)
#### Post date: [26 August 2022 00:09 UTC](https://discourse.nodered.org/t/compare-two-payloads/66825/5 "2022-08-26T00:09:33Z")

</div>

Thank you @MecatronicaMADE ,You really helped me

---

<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: [26 August 2022 09:11 UTC](https://discourse.nodered.org/t/compare-two-payloads/66825/6 "2022-08-26T09:11:56Z")

</div>

Where possible, context should be the last choice. I say this due to the async nature of node-red and unexpected results you can get when multiple values are passing within quick succession.

Here is a very similar flow using the join node instead (no context)...  
 ![Greenshot_r3iQcFgNgR](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/e/ee98251a08053aee5b13a38d1d49bfa5ab7543d2.gif)

---

<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: [25 October 2022 09:12 UTC](https://discourse.nodered.org/t/compare-two-payloads/66825/7 "2022-10-25T09:12:01Z")

</div>

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