Join values into array in specific order to make a calculation

Trying to make a calculation in NR using Join to create an array and then do a division in a calculator node.
Everything works as expected if the values (numerator and denominator) arrives in the correct order but they don't always do.
Setting a delay on one input feels like a bad work around.
Is there anyway to get them sorted in the join node?
Been looking a msg.part.index without luck but was not really knowing what I was doing.
Made a simple flow to illustrate the issue.
I will get different values depending on what I inject first since I'm doing a division.
I need it to always sort upper value as numerator and the lower as denominator.

The flow:

[{"id":"9a11d6ef7f86328c","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"2e221b1.51df8e4","type":"inject","z":"9a11d6ef7f86328c","name":"numerator","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0.5","payloadType":"num","x":480,"y":400,"wires":[["9d63e1180abe6daf"]]},{"id":"10bd8f79.53bee9","type":"inject","z":"9a11d6ef7f86328c","name":"denominator","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":490,"y":440,"wires":[["9d63e1180abe6daf"]]},{"id":"9d63e1180abe6daf","type":"join","z":"9a11d6ef7f86328c","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":670,"y":420,"wires":[["884d77d6bc170327"]]},{"id":"884d77d6bc170327","type":"calculator","z":"9a11d6ef7f86328c","name":"","inputMsgField":"payload","outputMsgField":"payload","operation":"div","constant":"","round":false,"decimals":0,"x":810,"y":420,"wires":[["8a73976f9414d377"]]},{"id":"8a73976f9414d377","type":"debug","z":"9a11d6ef7f86328c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":970,"y":420,"wires":[]}]

Dont use array mode if you want guaranteed order. Use key/value mode.

NOTE: It is always courteous to inlude the full name of any contrib nodes you are using in your demo flow (there are after all 4000+ contrib nodes)

OK, so here is a demo of how you can continue to use the calculator node. It uses the key/value mode of the join node to guarantee values are named and predictable regardless of the order they arrive. Once the payload is set with the values, we convert them to an array using a change node.

The second demo shows how you can avoid this step AND avoid the extra contrib node.

chrome_3rCN1V7O4i

The flow...

[{"id":"2e221b1.51df8e4","type":"inject","z":"9a11d6ef7f86328c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"numerator","payload":"3","payloadType":"num","x":1190,"y":100,"wires":[["9d63e1180abe6daf"]]},{"id":"10bd8f79.53bee9","type":"inject","z":"9a11d6ef7f86328c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"denominator","payload":"10","payloadType":"num","x":1200,"y":140,"wires":[["9d63e1180abe6daf"]]},{"id":"9d63e1180abe6daf","type":"join","z":"9a11d6ef7f86328c","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":1370,"y":120,"wires":[["d49ab23df184922e"]]},{"id":"8a73976f9414d377","type":"debug","z":"9a11d6ef7f86328c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1630,"y":180,"wires":[]},{"id":"884d77d6bc170327","type":"calculator","z":"9a11d6ef7f86328c","name":"","inputMsgField":"payload","outputMsgField":"payload","operation":"div","constant":"","round":false,"decimals":0,"x":1490,"y":180,"wires":[["8a73976f9414d377"]]},{"id":"d49ab23df184922e","type":"change","z":"9a11d6ef7f86328c","name":"copy num' and den' to array","rules":[{"t":"set","p":"payload","pt":"msg","to":"[payload.numerator, payload.denominator]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1560,"y":120,"wires":[["884d77d6bc170327"]]},{"id":"cde63c53c172712c","type":"inject","z":"9a11d6ef7f86328c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"numerator","payload":"3","payloadType":"num","x":1190,"y":240,"wires":[["41053aaafd46bec0"]]},{"id":"3072bf783ee7514c","type":"inject","z":"9a11d6ef7f86328c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"denominator","payload":"10","payloadType":"num","x":1200,"y":280,"wires":[["41053aaafd46bec0"]]},{"id":"41053aaafd46bec0","type":"join","z":"9a11d6ef7f86328c","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":1370,"y":260,"wires":[["ca19b97897c49bc1"]]},{"id":"ca19b97897c49bc1","type":"change","z":"9a11d6ef7f86328c","name":"Divide","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.numerator / payload.denominator","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1490,"y":260,"wires":[["289253c8d71ff355"]]},{"id":"289253c8d71ff355","type":"debug","z":"9a11d6ef7f86328c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1630,"y":260,"wires":[]}]
1 Like

It can also be done just using the join in automatic mode - but you need to specify the parts property correctly - eg for the denominator


and "index":0 for the numerator.
(and set the join into automatic mode)

Ideally you would also change the id for each pair to arrive - to identify them as a pair - but you may be able to work ok without doing that.

Thanks @Steve-Mcl and @dceejay !!
Great info, big thank you to the both of you.
Learned new, very usable stuff from both your posts.

PS. When creating a post like you did @Steve-Mcl , how do you make the code into single line and get
the Copy code option when hoovering over the code?
Will also remember to include full contrib node name..

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

Also, when you export, go to the JSON tab and check that Compact is selected down at the bottom.

Thanks, I did use the 3 backquotes at start and end but inline with text, corrected it now.

Didn't get your solution to work, got the below output after injecting what you suggested.
Am I missing something?
nodes

Is ID supposed to be id lowercase ?

You're right,sorry.
Changed into lowercase.
No error message from the join node now, but in the Divide node I get;
1

Also, something must be wrong with the data sent from Join node, debug 8 has payload 10

[{"id":"9a11d6ef7f86328c","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"9eac2eb719f072e9","type":"inject","z":"9a11d6ef7f86328c","name":"","props":[{"p":"payload"},{"p":"parts","v":"{\"index\":0,\"id\":\"ABC\",\"count\":2}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"3","payloadType":"num","x":610,"y":100,"wires":[["28e6cf4fe0402735","c7e7aae177867498"]]},{"id":"5638e52910bf3e2a","type":"inject","z":"9a11d6ef7f86328c","name":"","props":[{"p":"payload"},{"p":"parts","v":"{\"index\":1,\"id\":\"ABC\",\"count\":2}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":610,"y":220,"wires":[["28e6cf4fe0402735","45b5e9cceb9bd089"]]},{"id":"28e6cf4fe0402735","type":"join","z":"9a11d6ef7f86328c","name":"","mode":"auto","build":"array","property":"parts","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":850,"y":180,"wires":[["974b58f461b18778","12832ecf480c3c25"]]},{"id":"5a7d3cf45cde9542","type":"debug","z":"9a11d6ef7f86328c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1290,"y":180,"wires":[]},{"id":"974b58f461b18778","type":"calculator","z":"9a11d6ef7f86328c","name":"Divide","inputMsgField":"payload","outputMsgField":"payload","operation":"div","constant":"","round":false,"decimals":0,"x":1110,"y":180,"wires":[["5a7d3cf45cde9542"]]},{"id":"c7e7aae177867498","type":"debug","z":"9a11d6ef7f86328c","name":"debug 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1100,"y":80,"wires":[]},{"id":"45b5e9cceb9bd089","type":"debug","z":"9a11d6ef7f86328c","name":"debug 7","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":972.22216796875,"y":247.77777099609375,"wires":[]},{"id":"12832ecf480c3c25","type":"debug","z":"9a11d6ef7f86328c","name":"debug 8","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1020,"y":140,"wires":[]}]

Can you share your latest flow?

Shared it, it's at the bottom.

Hmm - seems to be a small bug... - if you open the join node and set to manual - you will see it says join all msg.parts... that should be msg.payload... then set it back to automatic...

It works, amazing that you found that!
Thanks again!

when you switch to automatic it should reset that hidden field... will have to fix that.

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