Function with different values in different times

I want to calculate an equation with 3 variables. The variable are coming asynchrone so I have to stor the latest values and then calculate the equation. Then output the result. Then wait for a new next value and then calucate with the new incoming value and withthe former two values that not have been updated. And so it continious ... Any good tip how to arrange this function? I'm a rookie Node-Red guy ....

sounds like you will need to use context variables

see the example here

1 Like

Or alternatively use the Join node, which saves the hassle of working out when you have got all three values and so on. Set it to generate key/value pairs using topic as the key, waiting for three topic values before sending anything and then sending every time a new value is received. Then you get all three values in the payload and can do what you want with them.

2 Likes

Or alternatively use the Batch node. Configure the node to group three messages, overlapping two.

This results in the node accumulating the first 3 messages and afterward sending the last two plus the new incoming payload. In other words, do the same as Colin said about the Join node. I just wanted to make the point that there are many different ways of achieving the same result. By the way, before Colin posted I was trying a much more complex solution using function node+ storage + lots of things. Only then I realized I was overthinking.

[{"id":"7f4082f3.ed5ecc","type":"batch","z":"b785aa2d.7963e8","name":"","mode":"count","count":"3","overlap":"2","interval":10,"allowEmptySequence":false,"topics":[],"x":530,"y":200,"wires":[["a15a0642.9fa4f8"]]},{"id":"309f5716.11e278","type":"inject","z":"b785aa2d.7963e8","name":"","topic":"mov","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":200,"wires":[["22f696db.2fd31a"]]},{"id":"22f696db.2fd31a","type":"change","z":"b785aa2d.7963e8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload%10","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"mov","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":200,"wires":[["7f4082f3.ed5ecc"]]},{"id":"a15a0642.9fa4f8","type":"debug","z":"b785aa2d.7963e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":710,"y":200,"wires":[]}]

r-01

1 Like