Would such a node exist? Max/Ave/Min

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.

Have you had a look at moving-average?

1 Like

Smooth node? (built in).

1 Like

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.

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.

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.

1 Like

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.

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.

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:

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.

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

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.

1 Like

No problems. It just Ahit me while I was sleeping.

Ok, what I'm doing:

At start up, there are no values. Given.

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?

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)

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