Bunching mqtt data before passing to next node

Hi there,

I have an mqtt in node that receives every second a value which is send into a function node.
Now I want to get 60 values (every minute) from the mqtt, average them and only send the average to an influx database. How can I do that ? Looked for nodes like complete, join and so on, but I cant get it to work. Any ideas how I can achieve this ?


What you are looking for is called moving average

[{"id":"8485c3f8e4265fa0","type":"inject","z":"99bbfbdd.2d4768","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":1480,"wires":[["678138d7a6a354ce"]]},{"id":"af5a405fbbe17fd5","type":"debug","z":"99bbfbdd.2d4768","name":"debug 13","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":1480,"wires":[]},{"id":"678138d7a6a354ce","type":"function","z":"99bbfbdd.2d4768","name":"Moving average","func":"const COUNT_VALUES = 60\nlet avg = context.get(\"avg\") || 0\n\nmsg.payload = (avg * (COUNT_VALUES - 1) + msg.payload) / COUNT_VALUES\ncontext.set(\"avg\", msg.payload)\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":1480,"wires":[["af5a405fbbe17fd5"]]}]

Have a look at the Smooth node.

Hi Oliver,
Tkx for you reply, but I seems my question was not well put. I only want to send one value per 60 sec send to the influx database.

This sounds like a job for the delay node.
set it to rate limit, with dropping intermediate messages, or queue them.

Then you could combine my solution with the one from @marcus-j-davies

Citing a recent user comment ...

... I'd emphasize to look at the smooth node - as @Colin already proposed: It provides several forms of averaging & the functionality to reduce to 1 message per x messages.


Sorry, didn't know that one... :face_with_hand_over_mouth:

Got it to work thanks to your input guys, much appreciated !