# How to add 2 inputs every second

**URL:** <https://discourse.nodered.org/t/how-to-add-2-inputs-every-second/87175>\
**Category:** General\
**Created:** [11 April 2024 21:35 UTC](https://discourse.nodered.org/t/how-to-add-2-inputs-every-second/87175 "2024-04-11T21:35:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmystique](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rmystique/32/84365_2.png) [@rmystique](https://discourse.nodered.org/u/rmystique)\
**Post date:** [11 April 2024 21:35 UTC](https://discourse.nodered.org/t/how-to-add-2-inputs-every-second/87175/1 "2024-04-11T21:35:51Z")

</div>

I have 2 variables that are constantly incoming from a ADC and I rate limited them to 1/sec each, I want to combine them (one is positive, one negative) and have the output be the difference between them. I think this can be done but the problem I am having is I will get 2 of one input sometimes and then my output is thrown off, how can I make sure my calculation takes one reading of each and outputs the result?

---

<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:** [11 April 2024 23:31 UTC](https://discourse.nodered.org/t/how-to-add-2-inputs-every-second/87175/2 "2024-04-11T23:31:56Z")

</div>

Is this two ADC or one ? Messages never arrive together , you will need a join node.

---

<div class="post-metadata">

**Author:** ![kitori](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@kitori](https://discourse.nodered.org/u/kitori)\
**Post date:** [12 April 2024 06:25 UTC](https://discourse.nodered.org/t/how-to-add-2-inputs-every-second/87175/3 "2024-04-12T06:25:20Z")

</div>

uhm something like this?  
for every input, you always get a msg with the latest values, then substract them

```auto
[{"id":"602aab9435fee132","type":"inject","z":"1160bb977c192b32","name":"adc1","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"parts.index","v":"0","vt":"num"},{"p":"parts.count","v":"2","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"adc1","payload":"$floor($random() * 10)\t\t","payloadType":"jsonata","x":370,"y":240,"wires":[["eb26a3852d0dc2e9"]]},{"id":"eb26a3852d0dc2e9","type":"join","z":"1160bb977c192b32","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":550,"y":260,"wires":[["34721846c797337f","7afd33ab1c1b369e","4a1d2095d2df1902"]]},{"id":"c4539199ae85d2ef","type":"inject","z":"1160bb977c192b32","name":"adc2","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"parts.index","v":"1","vt":"num"},{"p":"parts.count","v":"2","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"adc2","payload":"$floor($random() * 10)\t\t","payloadType":"jsonata","x":370,"y":280,"wires":[["eb26a3852d0dc2e9"]]},{"id":"34721846c797337f","type":"function","z":"1160bb977c192b32","name":"substract","func":"const x = msg.payload.adc1 - msg.payload.adc2\nmsg.raw_data = msg.payload\nmsg.payload = x\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":260,"wires":[["0e07a7c884f3a839"]]},{"id":"0e07a7c884f3a839","type":"debug","z":"1160bb977c192b32","name":"debug 77","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":880,"y":260,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 April 2024 08:08 UTC](https://discourse.nodered.org/t/how-to-add-2-inputs-every-second/87175/4 "2024-04-12T08:08:27Z")

</div>

If you set, in the Join node, After a Number of Message Parts to 2 then you don't need to setup msg.parts in the inject nodes.

```auto
[{"id":"602aab9435fee132","type":"inject","z":"bdd7be38.d3b55","name":"adc1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"adc1","payload":"$floor($random() * 10)\t\t","payloadType":"jsonata","x":310,"y":2920,"wires":[["eb26a3852d0dc2e9"]]},{"id":"eb26a3852d0dc2e9","type":"join","z":"bdd7be38.d3b55","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":2940,"wires":[["34721846c797337f"]]},{"id":"c4539199ae85d2ef","type":"inject","z":"bdd7be38.d3b55","name":"adc2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"adc2","payload":"$floor($random() * 10)\t\t","payloadType":"jsonata","x":310,"y":2960,"wires":[["eb26a3852d0dc2e9"]]},{"id":"34721846c797337f","type":"function","z":"bdd7be38.d3b55","name":"substract","func":"const x = msg.payload.adc1 - msg.payload.adc2\nmsg.raw_data = msg.payload\nmsg.payload = x\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":2940,"wires":[["0e07a7c884f3a839"]]},{"id":"0e07a7c884f3a839","type":"debug","z":"bdd7be38.d3b55","name":"debug 77","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":820,"y":2940,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [26 April 2024 08:09 UTC](https://discourse.nodered.org/t/how-to-add-2-inputs-every-second/87175/5 "2024-04-26T08:09:04Z")

</div>

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