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"]]}]
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.
This is an example of what I call a flow
and this is an example of a node
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.
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.