# "Best Practice" way to do math on a stream of data

**URL:** <https://discourse.nodered.org/t/best-practice-way-to-do-math-on-a-stream-of-data/61815>\
**Category:** General\
**Created:** [26 April 2022 18:32 UTC](https://discourse.nodered.org/t/best-practice-way-to-do-math-on-a-stream-of-data/61815 "2022-04-26T18:32:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SkyMo](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@SkyMo](https://discourse.nodered.org/u/SkyMo)\
**Post date:** [26 April 2022 18:32 UTC](https://discourse.nodered.org/t/best-practice-way-to-do-math-on-a-stream-of-data/61815/1 "2022-04-26T18:32:24Z")

</div>

Hi. I have a string of numbers like "1 2 3 4 5 6" (it's way more complicated than that, but this illustrates the idea) and they represent measurements. I use buffer parser to combine them into topic/value messages. In this example, the measurements are 12, 34,56. I need to divide 56/12 to get a % used.

Should I save 56 and 12 as global and use a function to add them?  
Should I use join to wait for the two messages and then add them in a function like so. This seems simple and works well.

```auto
[{"id":"62936f7e337ea844","type":"function","z":"c90f4fbedf92be5c","name":"SOC Calculator","func":"total = msg.payload.PackTmAH;\nremaining = msg.payload.PackRmAH;\n\npavailable = remaining/total;\n\nmsg.topic = \"PackPAvail\"\nmsg.payload = pavailable;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1160,"y":900,"wires":[["4b33e4f0c01c6892","9b7092f354906e38"]]}]

```

Is there a better way I am not thinking of?

---

<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:** [25 June 2022 18:32 UTC](https://discourse.nodered.org/t/best-practice-way-to-do-math-on-a-stream-of-data/61815/2 "2022-06-25T18:32:29Z")

</div>

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