# Would such a node exist? Max/Ave/Min

**URL:** https://discourse.nodered.org/t/would-such-a-node-exist-max-ave-min/51840
**Category:** General
**Created:** [4 October 2021 14:43 UTC](https://discourse.nodered.org/t/would-such-a-node-exist-max-ave-min/51840 "2021-10-04T14:43:14Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [4 October 2021 14:43 UTC](https://discourse.nodered.org/t/would-such-a-node-exist-max-ave-min/51840/1 "2021-10-04T14:43:14Z")

</div>

It wouldn't be _rocket science_, but I don't want to reinvent the wheel if I can avoid it.

You are receiving a series of messages (values).  
The node "splits" the values to 3 outputs:  
Maximum value received.  
Average value received.  
Minimum value received.  
(If a _reset_ message is received all stored values are wiped)

Would such a node exist?

## Update:

The `calculate` node NEARLY does this.  
`node-red-contrib-calculate`  
But lacks the ability to be reset by a message.  
And yes, I would need 3 nodes to do what I want, but that doesn't mean I couldn't use it.

---

<div class="post-metadata">

### Author: ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)
#### Post date: [4 October 2021 14:59 UTC](https://discourse.nodered.org/t/would-such-a-node-exist-max-ave-min/51840/2 "2021-10-04T14:59:27Z")

</div>

Have you had a look at [`moving-average`](https://flows.nodered.org/node/node-red-contrib-moving-average)?

---

<div class="post-metadata">

### Author: ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)
#### Post date: [4 October 2021 14:59 UTC](https://discourse.nodered.org/t/would-such-a-node-exist-max-ave-min/51840/3 "2021-10-04T14:59:48Z")

</div>

Smooth node? (built in).

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [4 October 2021 15:03 UTC](https://discourse.nodered.org/t/would-such-a-node-exist-max-ave-min/51840/4 "2021-10-04T15:03:34Z")

</div>

Looking good - was looking at the other one but yours slipped in just in time.

But..... How do I make the ` over the most recent` ongoing until a `msg.reset` is received?

It is 02:03 local here. Brain is really not in gear.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [4 October 2021 15:04 UTC](https://discourse.nodered.org/t/would-such-a-node-exist-max-ave-min/51840/5 "2021-10-04T15:04:24Z")

</div>

Thanks.

I'll look at it. Just I saw the reply with `smooth` and it is _built in_ which is kind of handy.  
Just got that question about _ongoing_ number of messages.

Yeah, ok. Sorry. Same problem as `smooth`.  
The _number of messages_ is unknown and only determined by `msg.reset`.  
So: not quite there.

---

<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: [4 October 2021 15:09 UTC](https://discourse.nodered.org/t/would-such-a-node-exist-max-ave-min/51840/6 "2021-10-04T15:09:41Z")

</div>

What happens if you put a very large number as the number to average/max/min over? I suspect it may do what you want.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [4 October 2021 15:12 UTC](https://discourse.nodered.org/t/would-such-a-node-exist-max-ave-min/51840/7 "2021-10-04T15:12:16Z")

</div>

Y-e-a-h....

Ok.

That is an option.

(Thanks.)

I just ..... wasn't sure that was.... _coacher_ in doing it that way. (_Legal_ - if you will)  
It isn't like I am talking squillions of messages.  
24 \* 60 \* ...... 2 -ish.  
90,000 I think is there about.

---

<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: [4 October 2021 15:26 UTC](https://discourse.nodered.org/t/would-such-a-node-exist-max-ave-min/51840/8 "2021-10-04T15:26:14Z")

</div>

> [@Trying\_to\_learn](#):
>
> just ..... wasn't sure that was.... _coacher_ in doing it that way.

Submit a PR on the docs for the node to clarify the action when less than the configured count have been received. Then it will be legal.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [4 October 2021 20:14 UTC](https://discourse.nodered.org/t/would-such-a-node-exist-max-ave-min/51840/9 "2021-10-04T20:14:34Z")

</div>

Just got up a while ago.

While _sleeping_ between those posts and now I thought:

(I'm asking for a sanity check on this option)

As I am wanting Max, Min and AVG (as opposed to Mean and all the other things offered by those nodes.

How about this code in a `function` node:

```auto
var MAX = context.get("MAX") || 0;
var COUNTER = context.get("COUNTER") || 0;
var MIN = context.get("MIN") || null;
var AVG = context.get("AVG") || null;
var TOTAL = context.get("TOTAL") || null;
var NEW = msg.payload;

msg1 = {};
msg2 = {};

if (msg.reset == true)
{
    // Reset all things
    node.warn("RESET");
    context.set("MAX",0);
    context.set("COUNTER",0);
    context.set("MIN",null);
    context.set("AVG",null);
    context.set("TOTAL",null);
    return;
}

if (COUNTER == 0)
{
    // First pass
    MIN = msg.payload;
}

NEW = msg.payload;

if (MAX > NEW)
{
    MAX = NEW;
    context.set("MAX",NEW);
}

if (MIN < NEW)
{
	MIN = NEW;
	context.set("MIN",NEW);
}

COUNTER = COUNTER + 1;
context.set("COUNTER",COUNTER);
TOTAL = TOTAL + NEW;
context.set("TOTAL",TOTAL);
AVG = parseInt((TOTAL / COUNTER));

msg.payload = "My calculated average is " + AVG;
msg1.payload = MAX;
msg2.payload = MIN;
return [msg,msg1,msg2];

```

No offence on the offers.  
But the _overhead_ of the 3 nodes to do a _simple_ thing over a 24 hour time....  
Their _stacks_ (?) would/could get quite big because they offer so much more that I don't need.

This way it gets rid of a lot of that kind of stuff.

Just wanting to check.

---

<div class="post-metadata">

### Author: ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)
#### Post date: [4 October 2021 20:59 UTC](https://discourse.nodered.org/t/would-such-a-node-exist-max-ave-min/51840/10 "2021-10-04T20:59:09Z")

</div>

I think the two if conditions are not correct. Shouldn't it be `MAX < NEW` and `MIN > NEW`? Or easier to read, `NEW > MAX` and `NEW < MIN`.

You could also just do something like

```auto
context.set("MAX", Math.max(...[NEW, MAX]));
context.set("MIN", Math.min(...[NEW, MIN]));

// etc.

```

IMHO, I would rather use an existing node, even if it's not perfect, than reinventing the wheel. But it really depends on your goals.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [4 October 2021 21:06 UTC](https://discourse.nodered.org/t/would-such-a-node-exist-max-ave-min/51840/11 "2021-10-04T21:06:39Z")

</div>

No problems. It just A_hit me_ while I was sleeping.

Ok, what I'm doing:

At start up, there are no values. Given.

```auto
if (COUNTER == 0)
{
    // First pass
    MIN = msg.payload;
    MAX = msg.payload;
}

```

That sets the MAX and MIN to the first message's value.

Though I think this is the part - yes?

```auto
if (MAX > NEW)
{
    MAX = NEW;
    context.set("MAX",NEW);
}

if (MIN < NEW)
{
	MIN = NEW;
	context.set("MIN",NEW);
}

```

You are correct.  
`if (NEW > MAX)`  
and  
`if (NEW < MIN)`

Thanks!

I didn't know of that command. `Math.max` and `Math.min`.  
But...

Anyway, it was just a thought. Just having three nodes which are way more _powerful_ than what is needed, etc... Seems overkill. (And memory use)

---

<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: [3 December 2021 22:54 UTC](https://discourse.nodered.org/t/would-such-a-node-exist-max-ave-min/51840/13 "2021-12-03T22:54:37Z")

</div>

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