# Calculate average value of sensors

**URL:** https://discourse.nodered.org/t/calculate-average-value-of-sensors/46471
**Category:** General
**Created:** [30 May 2021 17:34 UTC](https://discourse.nodered.org/t/calculate-average-value-of-sensors/46471 "2021-05-30T17:34:11Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Pronta](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pronta/32/42634_2.png) [@Pronta](https://discourse.nodered.org/u/Pronta)
#### Post date: [30 May 2021 17:34 UTC](https://discourse.nodered.org/t/calculate-average-value-of-sensors/46471/1 "2021-05-30T17:34:12Z")

</div>

![191234575_3353365744889886_702907418113150176_n](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/9195b6be0757fd591ec69e33fbb22d54f9bb71a2.png)

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [30 May 2021 19:47 UTC](https://discourse.nodered.org/t/calculate-average-value-of-sensors/46471/2 "2021-05-30T19:47:12Z")

</div>

Are you wanting to only average the values in that one payload? Or are you going to be wanting to average the values of multiple payloads and each one of those occurrences would be averaged?

---

<div class="post-metadata">

### Author: ![Pronta](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pronta/32/42634_2.png) [@Pronta](https://discourse.nodered.org/u/Pronta)
#### Post date: [30 May 2021 20:00 UTC](https://discourse.nodered.org/t/calculate-average-value-of-sensors/46471/3 "2021-05-30T20:00:07Z")

</div>

I'm wanting to average the values of four sensors of for a NODE and each one of those calculations occurrences would be averaged. thank you.

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [30 May 2021 22:01 UTC](https://discourse.nodered.org/t/calculate-average-value-of-sensors/46471/4 "2021-05-30T22:01:28Z")

</div>

I'm assuming that you want the average value of the 4 vales in your screenshot above.  
(probe1 + probe2 + probe3 + probe4) / 4 is that correct?  
If so, try this in a function node;

```auto
const val = Object.values(msg.payload[0])
function calculate(array) {
    return array.reduce((a, b) => a + b) / array.length;
}
msg.payload = calculate(val)
return msg;

```

If you mean something different, you will need to provide a better explanation, in more detail.

---

<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: [13 June 2021 22:01 UTC](https://discourse.nodered.org/t/calculate-average-value-of-sensors/46471/5 "2021-06-13T22:01:49Z")

</div>

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