# Two inputs in one function

**URL:** <https://discourse.nodered.org/t/two-inputs-in-one-function/31884>\
**Category:** General\
**Created:** [22 August 2020 18:11 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884 "2020-08-22T18:11:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [22 August 2020 18:11 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/1 "2020-08-22T18:11:28Z")

</div>

Hi

I need to have two inputs in one function, the two inputs look like this  
Input 1 Buffer: {"0":28,"1":2,"2":154,"3":128,"4":255,"5":100,"6":255,"7":102}  
Input 2 JSON: {"on":true,"brightness":40,"rgb":[255,178,76],"hex":"ffb24c","color":"orange","colorTemp":497,"updated":"2020-08-22T19:58:38+02:00"}

I need to combine this two inputs in a function and then send a message

```auto
[{"id":"2dacb3c.81f824c","type":"tcp in","z":"f1689f67.6fb5d","name":"Dynet","server":"client","host":"192.168.0.30","port":"8081","datamode":"stream","datatype":"buffer","newline":"","topic":"data","base64":false,"x":110,"y":180,"wires":[["f42971c8.af614","b6d335b0.151f78"]]}]

```

I did take a look at the Join, but I can't get it working since there is two different messages types, maybe there is a way to save the value in the function when the inputs comes in to the function

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [22 August 2020 18:33 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/2 "2020-08-22T18:33:17Z")

</div>

You can only send one msg at a time to a node. So the function node will process the first msg Ann send out a response, then process the second. You could use context storage to save data from the first msg but you I’ll have to figure out how to tell msg 1 from msg 2 and deal with a case where two of msg 1 or 2 arrive in a row

You should first join the msgs then send the combined msg to the function node

Use a change node to add the missing parts of msg1 to msg 2 and the missing parts of mzg2 to msg1 then join them

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [22 August 2020 18:37 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/3 "2020-08-22T18:37:54Z")

</div>

Could you give an example, a cant get i working because i have two different message types.

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [22 August 2020 18:41 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/4 "2020-08-22T18:41:35Z")

</div>

If it makes any difference, @zenofmud is technically correct.in having to send a response but you can send a Null as a response too.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [22 August 2020 18:45 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/5 "2020-08-22T18:45:59Z")

</div>

Let’s say msg1 has msg.payload.temp and msg2 Has msg.payload.humidity. Send msg1 to a change node and set msg.payload.humidity to nothing. Do the same for msg2 but set msg.payload.temp to nothing and then send them to the join.

You will also have to add the parts needed to do the join (read the help section of the join node)

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [22 August 2020 19:03 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/6 "2020-08-22T19:03:30Z")

</div>

I did try to make a simple setup, so maybe you could have a look at it.  
I cant seem to get it trough the change and the join node

```auto
[{"id":"73bfb3e1.57f90c","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"6ecf26c0.da1d68","type":"debug","z":"73bfb3e1.57f90c","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":870,"y":360,"wires":[]},{"id":"ab92eab2.426c48","type":"join","z":"73bfb3e1.57f90c","name":"","mode":"custom","build":"object","property":"","propertyType":"full","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":690,"y":420,"wires":[["6ecf26c0.da1d68"]]},{"id":"4db10712.8ba2b8","type":"change","z":"73bfb3e1.57f90c","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"request","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":360,"wires":[["6ecf26c0.da1d68","ab92eab2.426c48"]]},{"id":"aa5e4099.515e3","type":"inject","z":"73bfb3e1.57f90c","name":"Light Request (Buffer)","topic":"","payload":"[28,2,172,128,255,100,255,84]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":360,"wires":[["4db10712.8ba2b8"]]},{"id":"ae8c5e93.18492","type":"inject","z":"73bfb3e1.57f90c","name":"Light Reply (JSON)","topic":"","payload":"{\"on\":true,\"brightness\":33,\"reachable\":true,\"rgb\":[255,178,76],\"hex\":\"ffb24c\",\"color\":\"orange\",\"colorTemp\":497,\"updated\":\"2020-08-22T20:56:26+02:00\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":420,"wires":[["ab92eab2.426c48"]]}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/9/09b10eed602881481a90663b8050767378d4fba8.png)

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [22 August 2020 19:15 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/7 "2020-08-22T19:15:57Z")

</div>

I'm late to this party but look at this flow to see the changes, not sure of where you are going with this.

```auto
[{"id":"c0055da0.cc8e9","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"49fc4925.abbdc","type":"debug","z":"c0055da0.cc8e9","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":870,"y":360,"wires":[]},{"id":"3b42092e.64a5be","type":"join","z":"c0055da0.cc8e9","name":"","mode":"custom","build":"object","property":"","propertyType":"full","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":690,"y":420,"wires":[["49fc4925.abbdc"]]},{"id":"7e7e4537.6615b4","type":"change","z":"c0055da0.cc8e9","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"request","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":320,"wires":[["49fc4925.abbdc","3b42092e.64a5be"]]},{"id":"add87a6d.ce3028","type":"inject","z":"c0055da0.cc8e9","name":"Light Request (Buffer)","props":[{"p":"request","v":"[28,2,172,128,255,100,255,84]","vt":"bin"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"request","x":240,"y":360,"wires":[["3b42092e.64a5be"]]},{"id":"4b47eed0.5a2c78","type":"inject","z":"c0055da0.cc8e9","name":"Light Reply (JSON)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"reply","payload":"{\"on\":true,\"brightness\":33,\"reachable\":true,\"rgb\":[255,178,76],\"hex\":\"ffb24c\",\"color\":\"orange\",\"colorTemp\":497,\"updated\":\"2020-08-22T20:56:26+02:00\"}","payloadType":"json","x":230,"y":420,"wires":[["3b42092e.64a5be"]]}]

```

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [22 August 2020 19:23 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/8 "2020-08-22T19:23:32Z")

</div>

Yes something like this, but i cant seem to get the request out from the join with the same value as came in.  
The reply seams to work

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [22 August 2020 19:24 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/9 "2020-08-22T19:24:13Z")

</div>

What is different?

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [22 August 2020 19:30 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/10 "2020-08-22T19:30:01Z")

</div>

sorry my bad, I didn't see that the request payload data was deleted

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [22 August 2020 19:42 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/11 "2020-08-22T19:42:37Z")

</div>

It looks like it is no longer a buffer, how can I keep it in the same format ?

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [22 August 2020 20:23 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/12 "2020-08-22T20:23:56Z")

</div>

Sorry to bail on you but other duties call and I'm not going to be at a computer for several days. Sending from my phone now, best wishes

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [22 August 2020 21:59 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/13 "2020-08-22T21:59:41Z")

</div>

When you run that flow you should be seeing some error messages. What do they say? Make sure the answer is applied to both messages.

Then change the `join`'s 'to create' to 'an Array'

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [23 August 2020 00:01 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/14 "2020-08-23T00:01:14Z")

</div>

Hi,

Could something like this work ?

```auto
[{"id":"111afeb8.45f369","type":"debug","z":"3a02b3c7.cc85b4","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":890,"y":380,"wires":[]},{"id":"fcc7efbe.ef3ce8","type":"join","z":"3a02b3c7.cc85b4","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":610,"y":380,"wires":[["111afeb8.45f369"]]},{"id":"85bc2b05.7e4f7","type":"inject","z":"3a02b3c7.cc85b4","name":"Light Request (Buffer)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"buffer","payload":"[28,2,172,128,255,100,255,84]","payloadType":"bin","x":240,"y":360,"wires":[["fcc7efbe.ef3ce8"]]},{"id":"1079db44.24515d","type":"inject","z":"3a02b3c7.cc85b4","name":"Light Reply (JSON)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"json","payload":"{\"on\":true,\"brightness\":33,\"reachable\":true,\"rgb\":[255,178,76],\"hex\":\"ffb24c\",\"color\":\"orange\",\"colorTemp\":497,\"updated\":\"2020-08-22T20:56:26+02:00\"}","payloadType":"json","x":230,"y":420,"wires":[["fcc7efbe.ef3ce8"]]}]

```

**Note** that i set a topic in your inject nodes in order for **change node** to merge the data

---

<div class="post-metadata">

**Author:** ![harelabb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harelabb/32/27450_2.png) [@harelabb](https://discourse.nodered.org/u/harelabb)\
**Post date:** [23 August 2020 12:01 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/15 "2020-08-23T12:01:47Z")

</div>

Does the join node use the number of different topics to figure out the different parts?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [23 August 2020 13:32 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/16 "2020-08-23T13:32:35Z")

</div>

Have you read the Help?  
 ![Screen Shot 2020-08-23 at 9.31.30 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4f0117589a808ea744c7092e0028edbdd7b2c620.png)

What part of it is unclear?

Have you tried it with different topics? whatwas your result?

---

<div class="post-metadata">

**Author:** ![harelabb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harelabb/32/27450_2.png) [@harelabb](https://discourse.nodered.org/u/harelabb)\
**Post date:** [23 August 2020 14:04 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/17 "2020-08-23T14:04:18Z")

</div>

Sorry. I did not see that the join node specifically was set up to use topics. I was quite sure it said automatic for some reason..

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [23 August 2020 14:19 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/18 "2020-08-23T14:19:12Z")

</div>

A more clear example would be like this :

```auto
[{"id":"943b12b4.005428","type":"debug","z":"1b7f56b2.40cf39","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":970,"y":340,"wires":[]},{"id":"21acea57.c848ee","type":"join","z":"1b7f56b2.40cf39","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":770,"y":340,"wires":[["943b12b4.005428"]]},{"id":"fa04ca07.305258","type":"inject","z":"1b7f56b2.40cf39","name":"Light Request (Buffer)","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[28,2,172,128,255,100,255,84]","payloadType":"bin","x":280,"y":320,"wires":[["643d6998.2d69b8"]]},{"id":"c4c0f3b0.0f7f5","type":"inject","z":"1b7f56b2.40cf39","name":"Light Reply (JSON)","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"on\":true,\"brightness\":33,\"reachable\":true,\"rgb\":[255,178,76],\"hex\":\"ffb24c\",\"color\":\"orange\",\"colorTemp\":497,\"updated\":\"2020-08-22T20:56:26+02:00\"}","payloadType":"json","x":270,"y":380,"wires":[["ef362bc7.593a28"]]},{"id":"643d6998.2d69b8","type":"change","z":"1b7f56b2.40cf39","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"buffer","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":320,"wires":[["21acea57.c848ee"]]},{"id":"ef362bc7.593a28","type":"change","z":"1b7f56b2.40cf39","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"json","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":380,"wires":[["21acea57.c848ee"]]}]

```

In case our orginal messages dont have a topic. we can use a **Change node** to set a topic. That topic we use in **Change nodes** will be used in **Join node** (with manual settings) to merge the messages.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [23 August 2020 15:43 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/19 "2020-08-23T15:43:35Z")

</div>

@UnborN you don’t need the topics to match.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [23 August 2020 15:48 UTC](https://discourse.nodered.org/t/two-inputs-in-one-function/31884/20 "2020-08-23T15:48:24Z")

</div>

> [@zenofmud](#):
>
> you don’t need the topics to match

Hi Paul, but they are not matching .. one is set to ' **buffer**' and the other to ' **json**'  
did i miss something ?

[Next page](https://discourse.nodered.org/t/two-inputs-in-one-function/31884.md?page=2)
