Compare two payloads

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+?

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

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:

[{"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!!

1 Like

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.

Thank you @MecatronicaMADE ,You really helped me

2 Likes

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

2 Likes

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