# Making percentage calculations with two different msg.payload

**URL:** <https://discourse.nodered.org/t/making-percentage-calculations-with-two-different-msg-payload/215>\
**Category:** General\
**Created:** [17 May 2018 15:21 UTC](https://discourse.nodered.org/t/making-percentage-calculations-with-two-different-msg-payload/215 "2018-05-17T15:21:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamar85](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@Tamar85](https://discourse.nodered.org/u/Tamar85)\
**Post date:** [17 May 2018 15:21 UTC](https://discourse.nodered.org/t/making-percentage-calculations-with-two-different-msg-payload/215/1 "2018-05-17T15:21:20Z")

</div>

Hi

I have problem with my function block. First MQTT is sending me incoming water volume second MQTT is sending outgoing water volume and i wanna calculate whats the difference. My problem is that function block gives me two outputs one is correct and other i don’t even know where it comes.

Can someone please explain me or guide me how to make this thing to work. If i haven’t been understandable, let me know, i’ll try to explain better.

Best regards  
Tamar

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [17 May 2018 17:01 UTC](https://discourse.nodered.org/t/making-percentage-calculations-with-two-different-msg-payload/215/2 "2018-05-17T17:01:59Z")

</div>

Can you post the code you are using in your function block

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [17 May 2018 18:19 UTC](https://discourse.nodered.org/t/making-percentage-calculations-with-two-different-msg-payload/215/3 "2018-05-17T18:19:28Z")

</div>

Node-RED doesn’t have a memory. So when the second message comes in, it has no memory of the previous message which I am guessing where the second message comes from.

There are different techniques to overcome this, either global variables or using the join node. But as already has been said without seeing your function it is hard to say

---

<div class="post-metadata">

**Author:** ![Tamar85](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@Tamar85](https://discourse.nodered.org/u/Tamar85)\
**Post date:** [17 May 2018 19:21 UTC](https://discourse.nodered.org/t/making-percentage-calculations-with-two-different-msg-payload/215/4 "2018-05-17T19:21:18Z")

</div>

```auto
if (msg.topic == 'sensor/ylekastmine/2') {
    context.ylekastmine = msg.payload;
} else if (msg.topic == 'sensor/spoon/2') {
    context.spoon = msg.payload;
}
var test = {payload:0};
test.payload = Math.round(100/Number(context.ylekastmine)*(Number(context.spoon)));
return test;

```

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [17 May 2018 20:35 UTC](https://discourse.nodered.org/t/making-percentage-calculations-with-two-different-msg-payload/215/5 "2018-05-17T20:35:06Z")

</div>

check the syntax for setting and getting context as per the docs [https://nodered.org/docs/writing-functions#storing-data](https://nodered.org/docs/writing-functions#storing-data)

---

<div class="post-metadata">

**Author:** ![Andres](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andres/32/112_2.png) [@Andres](https://discourse.nodered.org/u/Andres)\
**Post date:** [18 May 2018 11:30 UTC](https://discourse.nodered.org/t/making-percentage-calculations-with-two-different-msg-payload/215/6 "2018-05-18T11:30:07Z")

</div>

You colud use the join node, in manual mode, then set it to combine every payload to create a key/value object using the topic as key and wait to get the msg.complete (that you’ll send when you send the second message)

---

<div class="post-metadata">

**Author:** ![mammoet](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@mammoet](https://discourse.nodered.org/u/mammoet)\
**Post date:** [18 May 2018 12:25 UTC](https://discourse.nodered.org/t/making-percentage-calculations-with-two-different-msg-payload/215/7 "2018-05-18T12:25:20Z")

</div>

> [@Tamar85](#):
>
> nding outgoing water volume and i wanna calculate whats the difference. My problem is that function block gives me two outputs one is correct and other i don’t even know where it comes.

i made a quick flow for you with a formula.  
just change the timestamp with th mqtt input.

[{"id":"27ee89f8.d3a6c6","type":"inject","z":"77705272.2fb44c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":180,"wires":[["3e97606.cf3b1a"]]},{"id":"250c6f55.d881","type":"inject","z":"77705272.2fb44c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":320,"wires":[["bb39ee40.1897d"]]},{"id":"3e97606.cf3b1a","type":"change","z":"77705272.2fb44c","name":"","rules":[{"t":"set","p":"income","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":180,"wires":[["714e8906.920db8","1cf69bf8.a51954"]]},{"id":"bb39ee40.1897d","type":"change","z":"77705272.2fb44c","name":"","rules":[{"t":"set","p":"outgoing","pt":"flow","to":".payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":320,"wires":[["7970dd98.a965f4","fa8138e8.1da4f8"]]},{"id":"2d60b2fd.5aec4e","type":"function","z":"77705272.2fb44c","name":"function","func":"var income = flow.get('income');\nvar outgoing = flow.get('outgoing');\nmsg.payload = income - outgoing;//or what ever kind of formula you want\n\nreturn msg;","outputs":1,"noerr":0,"x":680,"y":600,"wires":[["c5ec5317.afe59"]]},{"id":"7970dd98.a965f4","type":"debug","z":"77705272.2fb44c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":930,"y":320,"wires":},{"id":"714e8906.920db8","type":"debug","z":"77705272.2fb44c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":930,"y":180,"wires":},{"id":"1cf69bf8.a51954","type":"link out","z":"77705272.2fb44c","name":"in","links":["6cf07c18.be9104"],"x":975,"y":120,"wires":},{"id":"fa8138e8.1da4f8","type":"link out","z":"77705272.2fb44c","name":"out","links":["274642cd.16094e"],"x":975,"y":380,"wires":},{"id":"6cf07c18.be9104","type":"link in","z":"77705272.2fb44c","name":"","links":["1cf69bf8.a51954"],"x":315,"y":540,"wires":[["2d60b2fd.5aec4e"]]},{"id":"274642cd.16094e","type":"link in","z":"77705272.2fb44c","name":"","links":["fa8138e8.1da4f8"],"x":315,"y":660,"wires":[["2d60b2fd.5aec4e"]]},{"id":"c5ec5317.afe59","type":"debug","z":"77705272.2fb44c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1210,"y":600,"wires":}]

---

<div class="post-metadata">

**Author:** ![Tamar85](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@Tamar85](https://discourse.nodered.org/u/Tamar85)\
**Post date:** [18 May 2018 14:24 UTC](https://discourse.nodered.org/t/making-percentage-calculations-with-two-different-msg-payload/215/8 "2018-05-18T14:24:53Z")

</div>

I dont know why, but i cant import this nodes. Is there any other option?
