# Mathematically dividing two payloads

**URL:** <https://discourse.nodered.org/t/mathematically-dividing-two-payloads/43454>\
**Category:** General\
**Created:** [31 March 2021 10:36 UTC](https://discourse.nodered.org/t/mathematically-dividing-two-payloads/43454 "2021-03-31T10:36:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sahsha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahsha/32/43845_2.png) [@sahsha](https://discourse.nodered.org/u/sahsha)\
**Post date:** [31 March 2021 10:36 UTC](https://discourse.nodered.org/t/mathematically-dividing-two-payloads/43454/1 "2021-03-31T10:36:04Z")

</div>

Hello! I want to divide two payloads. I tried using join node and then [calculator node](https://flows.nodered.org/node/node-red-contrib-calc) but numerator injects later than denominator. So I used a delay node to delay denominator. Even then it couldn't work as at times numerator didn't inject the first time.

I tried searching for a solution in other forums where function node is being used but couldn't understand. Can anyone help me on this?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 March 2021 10:39 UTC](https://discourse.nodered.org/t/mathematically-dividing-two-payloads/43454/2 "2021-03-31T10:39:50Z")

</div>

you dont need a calculator node for something as simple as a division...

Just add topics to your values and set the join to make a key/value object...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/e/fe24f4fb923da04ebc62442eed3394bbcd977d88.png)

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

function code...

```auto
msg.payload = msg.payload.item1 / msg.payload.item2;
return msg;

```

Demo flow...

```auto
[{"id":"78a5547c.17a78c","type":"inject","z":"89aa4a33.4c3098","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"item1","payload":"20","payloadType":"num","x":430,"y":720,"wires":[["f9a52c63.53b6b"]]},{"id":"c9ce3cd8.6bc07","type":"inject","z":"89aa4a33.4c3098","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"item2","payload":"5","payloadType":"num","x":430,"y":760,"wires":[["f9a52c63.53b6b"]]},{"id":"f9a52c63.53b6b","type":"join","z":"89aa4a33.4c3098","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":"","reduceFixup":"","x":580,"y":740,"wires":[["5fad36fe.4cd3a8"]]},{"id":"5fad36fe.4cd3a8","type":"function","z":"89aa4a33.4c3098","name":"msg.payload.item1 / msg.payload.item2","func":"msg.payload = msg.payload.item1 / msg.payload.item2;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":840,"y":740,"wires":[["a32f801a.4354"]]},{"id":"a32f801a.4354","type":"debug","z":"89aa4a33.4c3098","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1110,"y":740,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![sahsha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahsha/32/43845_2.png) [@sahsha](https://discourse.nodered.org/u/sahsha)\
**Post date:** [31 March 2021 11:16 UTC](https://discourse.nodered.org/t/mathematically-dividing-two-payloads/43454/3 "2021-03-31T11:16:45Z")

</div>

@Steve-Mcl , I tried. check

```auto
[{"id":"8037e618.8cfef8","type":"function","z":"35319a86.99a836","name":"","func":"msg.payload = msg.payload.okprod / msg.payload.target;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1700,"y":420,"wires":[["a32f801a.4354"]]},{"id":"f9a52c63.53b6b","type":"join","z":"35319a86.99a836","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"payload","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1530,"y":420,"wires":[["ceaf078a.34d1d8","8037e618.8cfef8"]]},{"id":"a32f801a.4354","type":"debug","z":"35319a86.99a836","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1890,"y":420,"wires":[]},{"id":"ceaf078a.34d1d8","type":"debug","z":"35319a86.99a836","name":"","active":false,"tosidebar":false,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1690,"y":480,"wires":[]},{"id":"49ef46e.67d9ab8","type":"function","z":"35319a86.99a836","name":"","func":"msg.topic=\"okprod\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1320,"y":400,"wires":[["f9a52c63.53b6b","9f0b1bf0.c5dde8"]]},{"id":"5ecae5b.4c5ec1c","type":"function","z":"35319a86.99a836","name":"","func":"msg.topic=\"target\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1320,"y":440,"wires":[["f9a52c63.53b6b","28182701.4807f8"]]},{"id":"9f0b1bf0.c5dde8","type":"debug","z":"35319a86.99a836","name":"","active":false,"tosidebar":false,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1490,"y":360,"wires":[]},{"id":"28182701.4807f8","type":"debug","z":"35319a86.99a836","name":"","active":false,"tosidebar":false,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1490,"y":480,"wires":[]}]

```

 ![screenshot1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/b/6b3d93c8d39e7517b4c6ec1a0d5e162eaa131371.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 March 2021 11:50 UTC](https://discourse.nodered.org/t/mathematically-dividing-two-payloads/43454/4 "2021-03-31T11:50:18Z")

</div>

Check your values going into join have a topic.

Check the payloads going into join contain a number.

Check the join node is set exactly as I showed.

---

<div class="post-metadata">

**Author:** ![sahsha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahsha/32/43845_2.png) [@sahsha](https://discourse.nodered.org/u/sahsha)\
**Post date:** [31 March 2021 12:37 UTC](https://discourse.nodered.org/t/mathematically-dividing-two-payloads/43454/5 "2021-03-31T12:37:13Z")

</div>

@Steve-Mcl Yes, corrected. Its working now. thankyou.

---

<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:** [14 April 2021 12:37 UTC](https://discourse.nodered.org/t/mathematically-dividing-two-payloads/43454/6 "2021-04-14T12:37:17Z")

</div>

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