# Comparing payloads with different topics

**URL:** https://discourse.nodered.org/t/comparing-payloads-with-different-topics/75115
**Category:** General
**Tags:** function-node
**Created:** [13 February 2023 14:04 UTC](https://discourse.nodered.org/t/comparing-payloads-with-different-topics/75115 "2023-02-13T14:04:25Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![kyledoab](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@kyledoab](https://discourse.nodered.org/u/kyledoab)
#### Post date: [13 February 2023 14:04 UTC](https://discourse.nodered.org/t/comparing-payloads-with-different-topics/75115/1 "2023-02-13T14:04:25Z")

</div>

Hi there, I am using a buffer parser to parse data received over modbus. I have assigned topics to each bit of data inside the buffer parser but I am struggling to figure out how to compare payloads between topics. For example, I have the topic set as 'apple' and the msg.payload for 'apple' alternates between 0 and 1 but I need to compare this data with another msg.payload under topic 'orange' which also alternates between 0 and 1. How do I compare the payloads for different topics in the same function node?  
-Kyle

---

<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: [13 February 2023 14:08 UTC](https://discourse.nodered.org/t/comparing-payloads-with-different-topics/75115/2 "2023-02-13T14:08:30Z")

</div>

Hi Kyle, without seeing your flow or knowing if the data arrives in separate messages or the same message, we can only guess.

It may be down to how you have the parser setup (i.e. to release individual messages) or it may be down to how the data arrives.

Show us with more detail (screen shot or better still a minimal reproducible flow) with explanation and debug outputs.

---

<div class="post-metadata">

### Author: ![kyledoab](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@kyledoab](https://discourse.nodered.org/u/kyledoab)
#### Post date: [13 February 2023 14:27 UTC](https://discourse.nodered.org/t/comparing-payloads-with-different-topics/75115/3 "2023-02-13T14:27:45Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/47f716d6e4717ea84105351524f38247c74ccfb5.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/c/dce7f66fe7e0fe89b84eb6a2d3ad8afef6bce38e.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/403cc953f058b00139bdd48c0a9ee2eaa00604cb.png)  
I will change up from my original example and demonstrate with this flow. Essentially, I would have 3 inputs into a function node each with a given topic assigned. Depending on which switch was active would depend on the output payload of the function node. I don't know the correct way to read from each switch and have just typed ('switch1') as a placeholder for the code that I need to correctly read the payload attached to 'switch1'

---

<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: [13 February 2023 14:38 UTC](https://discourse.nodered.org/t/comparing-payloads-with-different-topics/75115/4 "2023-02-13T14:38:02Z")

</div>

ok, so a couple of important (fundamental) aspect of node-red to be aware of is that...

1. messages travel down each wire separately and arrive at the next node at a different time to other messages.
2. code in a function executes from the top down with zero knowledge of values from the last run (like it starts again from scratch every time its ran)

Armed with those 2 pieces of info, you should be able to determine that you cannot simply compare the payload of different messages. In short, you need to have access to both parts at the same time.

To do that, there are 2 common techniques (examples below)

1. Use a join node
2. Store the incoming values in context (for later comparison) and retrieve the previous value from context

* * *

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

2: [Working with context : Node-RED](https://nodered.org/docs/user-guide/context)

---

<div class="post-metadata">

### Author: ![kyledoab](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@kyledoab](https://discourse.nodered.org/u/kyledoab)
#### Post date: [13 February 2023 15:22 UTC](https://discourse.nodered.org/t/comparing-payloads-with-different-topics/75115/6 "2023-02-13T15:22:44Z")

</div>

Hi Steve, I have just checked with my actual flow and each message will theoretically come through at the same time as the modbus read node sends an array of data every 5 seconds. From my example, could the function node compare the values if all switches output their values at the same time?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [13 February 2023 15:27 UTC](https://discourse.nodered.org/t/comparing-payloads-with-different-topics/75115/7 "2023-02-13T15:27:20Z")

</div>

You also have multiple syntax errors in your function code extra `)` and `'switch' === 1` will always br false as a string will never equal a number in type and value.

---

<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: [13 February 2023 15:32 UTC](https://discourse.nodered.org/t/comparing-payloads-with-different-topics/75115/8 "2023-02-13T15:32:13Z")

</div>

Sorry, but you screenshots above differ from the real world situation.

You would need to provide the info I requested first time around.

---

<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: [13 February 2023 16:07 UTC](https://discourse.nodered.org/t/comparing-payloads-with-different-topics/75115/9 "2023-02-13T16:07:29Z")

</div>

> [@kyledoab](#):
>
> From my example, could the function node compare the values if all switches output their values at the same time?

A node can only process one message at a time.  
msg.payload can only have one value. When a new message arrives, msg.payload has a new value; the previous value from a millisecond ago is gone.  
You need to join the messages or use context variables.

---

<div class="post-metadata">

### Author: ![kyledoab](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@kyledoab](https://discourse.nodered.org/u/kyledoab)
#### Post date: [13 February 2023 16:23 UTC](https://discourse.nodered.org/t/comparing-payloads-with-different-topics/75115/10 "2023-02-13T16:23:25Z")

</div>

Thanks for your responses guys, I am unable to properly show you the issue as the flow is confidential so I can understand how tough it is to properly assist me with only a small amount of info and a hypothetical flow. I believe it is a join node I need to use and so I am looking into how to use one now, thanks again!

---

<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: [13 February 2023 16:44 UTC](https://discourse.nodered.org/t/comparing-payloads-with-different-topics/75115/11 "2023-02-13T16:44:59Z")

</div>

> [@kyledoab](#):
>
> each message will theoretically come through at the same time as the modbus read node sends an array of data every 5 seconds

Do you mean that all the data you need is in that array? If so then don't split it up into separate messages, keep it all together in one message, then you don't need a Join node.
