Efficient flow construction

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


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

[{"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"]]}]

See this article in the cookbook for an example of how to join messages into one object.

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

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.

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. :grinning:

This is an example of what I call a flow
image
and this is an example of a node
image

So I think perhaps you've been looking for flows with similar behaviour? :slightly_smiling_face:.

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.

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

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.

What mqtt topics are the values in?

mostly object

No, what topics are the MQTT nodes subscribed to?