# Efficient flow construction

**URL:** <https://discourse.nodered.org/t/efficient-flow-construction/93894>\
**Category:** General\
**Tags:** function-node, flows\
**Created:** [13 December 2024 11:43 UTC](https://discourse.nodered.org/t/efficient-flow-construction/93894 "2024-12-13T11:43:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pratham](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@Pratham](https://discourse.nodered.org/u/Pratham)\
**Post date:** [13 December 2024 11:43 UTC](https://discourse.nodered.org/t/efficient-flow-construction/93894/1 "2024-12-13T11:43:06Z")

</div>

Hi everyone ,  
I am trying to work on a flow as attached below.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/4034e7d9b25b63f96a94f1a2bac68ed3b48d48f5.png)  
It's a dummy flow that i have created without proper configuration but I guess its enough to put my requirement forward.  
As you can see, i find sum of mqtt in 1 and in 2 , and subtract in3 from that sum and write the output to mqtt out,  
so I want to know how to build the function node for sum such that it waits until it receives data from both the inputs (inputs can be dynamic in my actual implementation) finds the sum and then continues with the remaining part of the flow, similarly with the difference node. And i don't want to store the data in the global context or flow context (if possible.) or any external database and want to maintain the received data even if i update the flow. I am new to node-red so don't know if it makes sense. I was looking for nodes with similar behavior to check their approach but couldn't not find any.  
Can anyone help me with this.

PS: I create this flows dynamically using admin flow API's  
node code

```auto
[{"id":"81594a14f30547c0","type":"function","z":"d3850bc17ffe523d","name":"difference","func":"// find difference of sum and in3\n// shouldnt continue unless both inputs are recieved \nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":610,"y":260,"wires":[["def478c9c1b53a83"]]}]

```

---

<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:** [13 December 2024 11:52 UTC](https://discourse.nodered.org/t/efficient-flow-construction/93894/2 "2024-12-13T11:52:19Z")

</div>

> [@Pratham](#):
>
> so I want to know how to build the function node for sum such that it waits until it receives data from both the inputs

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<div class="post-metadata">

**Author:** ![Pratham](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@Pratham](https://discourse.nodered.org/u/Pratham)\
**Post date:** [13 December 2024 12:06 UTC](https://discourse.nodered.org/t/efficient-flow-construction/93894/3 "2024-12-13T12:06:21Z")

</div>

In this case if i re deploy nodes without changing any config .. the old data is lost  
and if i make some change to it number of inputs say i want to handle the old data as per requirement

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [13 December 2024 12:28 UTC](https://discourse.nodered.org/t/efficient-flow-construction/93894/4 "2024-12-13T12:28:01Z")

</div>

> [@Pratham](#):
>
> And i don't want to store the data in the global context or flow context (if possible.)

If you don't want to store the latest mqtt messages in context then you **must** use a Join node. And this is a good choice. Don't use context variables unless you really need to.

> [@Pratham](#):
>
> I want to maintain the received data even if i update the flow.

I might be wrong but I think you need to use the retained message feature of MQTT for this.  
Or maybe this is an example where context variables, which can be persistant even over a reboot, would be appropriate. 😀

> [@Pratham](#):
>
> I was looking for nodes with similar behavior to check their approach but couldn't not find any.

This is an example of what I call a flow  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/optimized/3X/1/e/1e9192558299b38fd1b13bfe1f862aa1c9c36e64_2_345x27.png)  
and this is an example of a node  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b5d8e0e8ddc9a5049dfe8ff07b0921669d5bdebd.png)

So I think perhaps you've been looking for _flows_ with similar behaviour? 🙂.

ps For easy code maintenance, it's better to have just one mqtt input on the editor tab/flow, making use of mqtt wildcards.  
And that would allow you to do both your calculations in a single function node.  
Not sure how your thought of dynamic MQTT fits with this.

---

<div class="post-metadata">

**Author:** ![Pratham](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@Pratham](https://discourse.nodered.org/u/Pratham)\
**Post date:** [13 December 2024 12:33 UTC](https://discourse.nodered.org/t/efficient-flow-construction/93894/5 "2024-12-13T12:33:01Z")

</div>

if I use node context to save data then how to implement it ?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [13 December 2024 12:35 UTC](https://discourse.nodered.org/t/efficient-flow-construction/93894/6 "2024-12-13T12:35:26Z")

</div>

[https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)

Use "context" (ie just one node) scope in preference to "flow" variables.  
"global" context variables are an invention of the devil and should be shunned, except perhaps for genuinely static data.

---

<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:** [13 December 2024 12:59 UTC](https://discourse.nodered.org/t/efficient-flow-construction/93894/7 "2024-12-13T12:59:57Z")

</div>

What mqtt topics are the values in?

---

<div class="post-metadata">

**Author:** ![Pratham](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@Pratham](https://discourse.nodered.org/u/Pratham)\
**Post date:** [13 December 2024 13:34 UTC](https://discourse.nodered.org/t/efficient-flow-construction/93894/8 "2024-12-13T13:34:50Z")

</div>

mostly object

---

<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:** [13 December 2024 14:05 UTC](https://discourse.nodered.org/t/efficient-flow-construction/93894/9 "2024-12-13T14:05:16Z")

</div>

No, what _topics_ are the MQTT nodes subscribed to?
