# Batch function with averaging

**URL:** https://discourse.nodered.org/t/batch-function-with-averaging/72031
**Category:** General
**Tags:** function-node
**Created:** [11 December 2022 12:45 UTC](https://discourse.nodered.org/t/batch-function-with-averaging/72031 "2022-12-11T12:45:11Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![oliver.es](https://avatars.discourse-cdn.com/v4/letter/o/dc4da7/32.png) [@oliver.es](https://discourse.nodered.org/u/oliver.es)
#### Post date: [11 December 2022 12:45 UTC](https://discourse.nodered.org/t/batch-function-with-averaging/72031/1 "2022-12-11T12:45:11Z")

</div>

Hi,

in Node-RED there is node called "batch" where I can group messages by time interval. But I need to average every field that is received in meantime repeatedly. The problem is when messages are sent randomly (when values are changed). I can't define in advance the count of messages and even the field names.

It is possible to write that batch function with grouping messages and averaging values"

Thank you

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [11 December 2022 13:39 UTC](https://discourse.nodered.org/t/batch-function-with-averaging/72031/2 "2022-12-11T13:39:13Z")

</div>

You could use batch and join to get a msg.payload that has all of the values over your set time in an array that you could then calculate an average from (using a change node and JSONata or using a function node). That has the advantage of only needing core nodes.

If you are happy installing another node, the node-red-contrib-calculate node should do what you want I think.

---

<div class="post-metadata">

### Author: ![oliver.es](https://avatars.discourse-cdn.com/v4/letter/o/dc4da7/32.png) [@oliver.es](https://discourse.nodered.org/u/oliver.es)
#### Post date: [12 December 2022 07:29 UTC](https://discourse.nodered.org/t/batch-function-with-averaging/72031/3 "2022-12-12T07:29:28Z")

</div>

node-red-contrib-calculate working perfectly ! thank you

---

<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: [26 December 2022 07:29 UTC](https://discourse.nodered.org/t/batch-function-with-averaging/72031/4 "2022-12-26T07:29:41Z")

</div>

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