# Bunching mqtt data before passing to next node

**URL:** <https://discourse.nodered.org/t/bunching-mqtt-data-before-passing-to-next-node/74208>\
**Category:** General\
**Created:** [26 January 2023 10:59 UTC](https://discourse.nodered.org/t/bunching-mqtt-data-before-passing-to-next-node/74208 "2023-01-26T10:59:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leo](https://avatars.discourse-cdn.com/v4/letter/l/c2a13f/32.png) [@Leo](https://discourse.nodered.org/u/Leo)\
**Post date:** [26 January 2023 10:59 UTC](https://discourse.nodered.org/t/bunching-mqtt-data-before-passing-to-next-node/74208/1 "2023-01-26T10:59:12Z")

</div>

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 ?

Tkx  
Leo

---

<div class="post-metadata">

**Author:** ![OliverWieland](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oliverwieland/32/74937_2.png) [@OliverWieland](https://discourse.nodered.org/u/OliverWieland)\
**Post date:** [26 January 2023 11:11 UTC](https://discourse.nodered.org/t/bunching-mqtt-data-before-passing-to-next-node/74208/2 "2023-01-26T11:11:36Z")

</div>

What you are looking for is called _moving average_

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

```

---

<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:** [26 January 2023 11:13 UTC](https://discourse.nodered.org/t/bunching-mqtt-data-before-passing-to-next-node/74208/4 "2023-01-26T11:13:03Z")

</div>

Have a look at the Smooth node.

---

<div class="post-metadata">

**Author:** ![Leo](https://avatars.discourse-cdn.com/v4/letter/l/c2a13f/32.png) [@Leo](https://discourse.nodered.org/u/Leo)\
**Post date:** [26 January 2023 11:43 UTC](https://discourse.nodered.org/t/bunching-mqtt-data-before-passing-to-next-node/74208/5 "2023-01-26T11:43:39Z")

</div>

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.  
Leo

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [26 January 2023 11:47 UTC](https://discourse.nodered.org/t/bunching-mqtt-data-before-passing-to-next-node/74208/6 "2023-01-26T11:47:19Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/5/c59a43526deffaa43fbcbaab6a820a6eae6925a7.png)

---

<div class="post-metadata">

**Author:** ![OliverWieland](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oliverwieland/32/74937_2.png) [@OliverWieland](https://discourse.nodered.org/u/OliverWieland)\
**Post date:** [26 January 2023 11:52 UTC](https://discourse.nodered.org/t/bunching-mqtt-data-before-passing-to-next-node/74208/7 "2023-01-26T11:52:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [26 January 2023 12:03 UTC](https://discourse.nodered.org/t/bunching-mqtt-data-before-passing-to-next-node/74208/8 "2023-01-26T12:03:51Z")

</div>

Citing a recent user comment ...

> [@Trigger node - how to delay and repeat message?](https://discourse.nodered.org/t/trigger-node-how-to-delay-and-repeat-message/74117/16):
>
> I know there are a lot of possibilities to solve my problem.

... 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`.

---

<div class="post-metadata">

**Author:** ![OliverWieland](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oliverwieland/32/74937_2.png) [@OliverWieland](https://discourse.nodered.org/u/OliverWieland)\
**Post date:** [26 January 2023 12:09 UTC](https://discourse.nodered.org/t/bunching-mqtt-data-before-passing-to-next-node/74208/9 "2023-01-26T12:09:47Z")

</div>

Sorry, didn't know that one... 🤭

---

<div class="post-metadata">

**Author:** ![Leo](https://avatars.discourse-cdn.com/v4/letter/l/c2a13f/32.png) [@Leo](https://discourse.nodered.org/u/Leo)\
**Post date:** [26 January 2023 13:26 UTC](https://discourse.nodered.org/t/bunching-mqtt-data-before-passing-to-next-node/74208/10 "2023-01-26T13:26:10Z")

</div>

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

Tkx  
Leo

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [27 March 2023 13:26 UTC](https://discourse.nodered.org/t/bunching-mqtt-data-before-passing-to-next-node/74208/11 "2023-03-27T13:26:42Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
