Comparing payloads with different topics

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

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.

image
image


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'

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 for an example of how to join messages into one object.

2: Working with context : Node-RED

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?

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.

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

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

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.

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!

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.