Merge eigth numbers in a function node | same order, everytime

I don't know how to solve the following problem:
I have two inputs in a function node, and would like to merge the two payloads of the inputs (do not take + or *) so that if the first sends a 2 the second sends a 6 then the function node outputs a 26.
If the second sends 9 then 29 should be output. And all with 8 inputs for a binary code.
Also they had to keep the same order(granted by diffrent topics)

Hi @Kiwi_gamer01

if each message is coming separately you need a way to join them together into a single message so the Function can work with all of the information.

I would start by looking at the Join node configured to merge messages into a key/value object. That would expect each message to have a unique msg.topic so you known what order they come in. You'll need to think about how the node will know it has received all of the expected messages.

1 Like

unfortunately i don't know in which order the numbers come it is unfortunately random

Where are the numbers coming from? What does your flow look like? Why are the messages arriving in a random order and not the order they are originally generated?

1 Like

the whole program is used to control our shutters. if somebody presses a switch, a signal is sent. and since it is not specified who is allowed to press when, the input data are random at the end (only between 1 and 0).
And this part of the program merges the pins because an i2c bus can only sent on command each time

Are you talking about a physical switch, if so, how are these switch presses getting into Node-RED?
Or is it a switch on a webpage/node-red-dashboard?

How does pressing one switch relate to having multiple messages with numeric values in that you want to combine in some order?

I'm struggling to understand the full scenario. I understand you have some messages from somewhere that you want to combine... but if you can't identify the proper order for those messages then I'm not sure what you can do.

a small section

[{"id":"e3315dda.cb65f","type":"tab","label":"Rollo 1","disabled":false,"info":""},{"id":"c753436a.b9b35","type":"inject","z":"e3315dda.cb65f","name":"Rollo 1 rauf","topic":"R_1_a","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":80,"wires":[["85e204ce.1e4688","14852660.fe9d9a","289decf7.4c01b4","3e99263.3d277da"]]},{"id":"756942bd.e58f8c","type":"inject","z":"e3315dda.cb65f","name":"Aus","topic":"aus","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":530,"y":160,"wires":[["f2b12843.1621b8","3e99263.3d277da"]]},{"id":"1ce32171.9aa4ef","type":"inject","z":"e3315dda.cb65f","name":"Rollo 1 runter","topic":"R_1_r","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":240,"wires":[["709dd61e.e75198","a16ea1ac.0a1d5","d8891466.b54768","f2b12843.1621b8"]]},{"id":"d5ab9e47.d53e9","type":"delay","z":"e3315dda.cb65f","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":500,"y":200,"wires":[["3e99263.3d277da","f2b12843.1621b8"]]},{"id":"f685ad78.2fde2","type":"delay","z":"e3315dda.cb65f","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":500,"y":120,"wires":[["f2b12843.1621b8","3e99263.3d277da"]]},{"id":"f5ddf286.9d32a","type":"debug","z":"e3315dda.cb65f","name":"R_1_r","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1230,"y":240,"wires":[]},{"id":"3e99263.3d277da","type":"change","z":"e3315dda.cb65f","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"num","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":200,"wires":[["9c2b9b9b.2dd778"]]},{"id":"f2b12843.1621b8","type":"change","z":"e3315dda.cb65f","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"num","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":120,"wires":[["302d667f.5f817a"]]},{"id":"34a3c0cc.ee9f8","type":"debug","z":"e3315dda.cb65f","name":"R_1_a","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1230,"y":80,"wires":[]},{"id":"641d6a4.94fba94","type":"switch","z":"e3315dda.cb65f","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":1030,"y":120,"wires":[["34a3c0cc.ee9f8","289decf7.4c01b4"],[]]},{"id":"e9d4455e.7b74e8","type":"switch","z":"e3315dda.cb65f","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":1030,"y":200,"wires":[["f5ddf286.9d32a","d8891466.b54768"],[]]},{"id":"709dd61e.e75198","type":"delay","z":"e3315dda.cb65f","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":310,"y":200,"wires":[["d5ab9e47.d53e9"]]},{"id":"a16ea1ac.0a1d5","type":"delay","z":"e3315dda.cb65f","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":510,"y":240,"wires":[["f5ddf286.9d32a"]]},{"id":"85e204ce.1e4688","type":"delay","z":"e3315dda.cb65f","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":310,"y":120,"wires":[["f685ad78.2fde2"]]},{"id":"14852660.fe9d9a","type":"delay","z":"e3315dda.cb65f","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":510,"y":80,"wires":[["34a3c0cc.ee9f8"]]},{"id":"289decf7.4c01b4","type":"switch","z":"e3315dda.cb65f","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":1230,"y":120,"wires":[[],[]]},{"id":"d8891466.b54768","type":"switch","z":"e3315dda.cb65f","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":1230,"y":200,"wires":[[],[]]},{"id":"9c2b9b9b.2dd778","type":"function","z":"e3315dda.cb65f","name":"","func":"msg.topic = \"R_1_r\"\nreturn msg;","outputs":1,"noerr":0,"x":890,"y":200,"wires":[["e9d4455e.7b74e8"]]},{"id":"302d667f.5f817a","type":"function","z":"e3315dda.cb65f","name":"","func":"msg.topic = \"R_1_a\"\nreturn msg;","outputs":1,"noerr":0,"x":890,"y":120,"wires":[["641d6a4.94fba94"]]},{"id":"17449983.18dd16","type":"comment","z":"e3315dda.cb65f","name":" switches","info":"","x":100,"y":160,"wires":[]},{"id":"eb4f0d06.9e408","type":"comment","z":"e3315dda.cb65f","name":" the payload of R_1_a and R_1_r must be merged","info":"","x":1360,"y":160,"wires":[]}]

sorry for the german words in the code. just come from germany

What is that flow showing? How does that related to switches?

1 Like

I am not concerned with the switches, it is about the output. The flow contains two switches (initially only digital (but therefore unimportant)) and the sketion ensures that only one of the two outputs may be 1 at a time because the motor has two inputs in the roller shutter. (one up and one down) Of course so far everything works only via debug nodes and not with no connection to the outside. The two debug nodes R_1_a, R_1_r say which motor input is later on.

I don't know how to explain why the two numbers have to be in a row in a payload

Your original question was about having multiple messages containing digits and wanting to combine them in the right order. How does the flow you've just shared relate to that issue?

I assume one of the Inject buttons represents 'open blind' and the other one represents 'close blind'? So if someone clicks 'open', you need to send a 0 to the 'close' motor output to stop it closing, and then a 1 to the 'open' motor output. And vice versa - clicking the 'close' should send a '0' to the open output and then a 1 to the close motor output. That would explain the need to get the outputs in the right order - to ensure the motor is stopped before changing direction.

Is that a correct description of the task?

1 Like

I simplify the whole thing

i also start a neu topic

You need to make sure each switch input has a unique msg.topic value so you know which switch generated the value. For example, using topics of v1, v2,... v8.

Then use a Join node to merge to a key/value object use msg.topic as the key.

That will get you a message with a payload of:

{
   payload: {
      v1: 1,
      v2: 7,
      ...
      v8: 9
   }
}

You can then use a Function node to generate the output you want:

msg.payload = msg.payload.v1 + msg.payload.v2 + ... + msg.payload.v8;
return msg;
1 Like

you could please create an example flow

I have given you all the pieces.

  1. add msg.topic to each of your Inject nodes so you know which is which
  2. wire them all to a Join node configured as I describe
  3. wire the Join node to a Function node with the code I have given you.

The only piece you will have to experiment with is how the Join node is configured to send on the message.

1 Like

that with the join node does not work with the function node

[{"id":"76d8af3f.b4518","type":"debug","z":"a4f6adcd.192d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":810,"y":540,"wires":[]},{"id":"c76a4e0e.87e83","type":"join","z":"a4f6adcd.192d8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":490,"y":540,"wires":[[]]},{"id":"dcf1523.ebd5eb","type":"inject","z":"a4f6adcd.192d8","name":"","topic":"v2","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":560,"wires":[["c76a4e0e.87e83"]]},{"id":"cb2fe72b.aac1f8","type":"inject","z":"a4f6adcd.192d8","name":"","topic":"v1","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":520,"wires":[["c76a4e0e.87e83"]]},{"id":"e0851381.5d568","type":"inject","z":"a4f6adcd.192d8","name":"","topic":"v1","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":480,"wires":[["c76a4e0e.87e83"]]},{"id":"d2082786.09aef8","type":"inject","z":"a4f6adcd.192d8","name":"","topic":"v2","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":600,"wires":[["c76a4e0e.87e83"]]}]

cloud you you please insert the funktion node and configure it right

Please look at this

[{"id":"76d8af3f.b4518","type":"debug","z":"a4f6adcd.192d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":810,"y":540,"wires":[]},{"id":"c76a4e0e.87e83","type":"join","z":"a4f6adcd.192d8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":490,"y":540,"wires":[["d8c8e22d.902eb","76d8af3f.b4518"]]},{"id":"dcf1523.ebd5eb","type":"inject","z":"a4f6adcd.192d8","name":"","topic":"v2","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":560,"wires":[["c76a4e0e.87e83"]]},{"id":"cb2fe72b.aac1f8","type":"inject","z":"a4f6adcd.192d8","name":"","topic":"v1","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":520,"wires":[["c76a4e0e.87e83"]]},{"id":"e0851381.5d568","type":"inject","z":"a4f6adcd.192d8","name":"","topic":"v1","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":480,"wires":[["c76a4e0e.87e83"]]},{"id":"d2082786.09aef8","type":"inject","z":"a4f6adcd.192d8","name":"","topic":"v2","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":600,"wires":[["c76a4e0e.87e83"]]},{"id":"d8c8e22d.902eb","type":"function","z":"a4f6adcd.192d8","name":"","func":"msg.payload = msg.payload.v1 + msg.payload.v2;\nreturn msg;","outputs":1,"noerr":0,"x":650,"y":540,"wires":[["76d8af3f.b4518"]]}]

watch for the mistake...

Could it be that you don't know what I'm getting at?

i want two merge two numbers 1 and 2 to 12
in one message