Knot following?

I need to do a very simple task: record maximum and minimum values.
I want some node to compare the current value with the previous one and see if it is bigger or smaller. If so, return and update the maximum and minimum values. Otherwise, retone null.

Any suggestion?

Have a look at node red doc Writing Functions. Read it all initially for information, then look in particular at the section on storing data, node local context.

1 Like

So what have you tried so far? Have you read the documentation about working with context?

Here is one very incomplete version of function which probably is able to do it if only the gaps could be filled with ...

// read values from context
var min = context.get('min') || Number.MAX_SAFE_INTEGER
var max = context.get('max') || Number.MIN_SAFE_INTEGER
//mak flag that can be rised then change happened
var changed = false

// compare values with incoming payload
if(msg.payload /*is bigger that last stored max value*/){
    // set last stored max to incoming payload
    // and mark that some value is changed
}

if(msg.payload/*is smaller that last stored min value*/){
    // set last stored min to incoming payload
    // and mark that some value is changed
}

// decide what going to happen next
if(/*there was change*/){
    // store changed values 
    //context.set(['min','max'],[min,max])
    
    // create payload for outgoing msg
    // msg.payload =
    
    //send it out
    //retu....
}

// otherwise do nothing (sending out the null is not reasonable)

It certainly missing the reset capability and may be something more...

1 Like

In general Node-RED has three main approaches for solving a problem.

1- Build a logic around core nodes already available in the node palette.
2- Check in the Node-REd library if there is some custom node that does the job
3- Build logic with JavaScript code inside the function node

My preferred choice is try option 1 then 2 and then 3, in this order. My suggestion would be a flow like the one below. If you are able to understand it then you probably understands the idea of flow based programming.

This is how the switch node is configured:

s-02

You have to understand also Node-Red context to configure the change nodes used in the flow.

If you really want a single node then you can encapsulate this flow inside a subflow.

1 Like

I was able to implement a system similar to your example. It includes panel nodes and some information about the time and date of the maximum and minimum records.

The flow is attached:

[{"id":"8a137bbe.92b958","type":"change","z":"cfbb6e4f.6526a","name":"","rules":[{"t":"set","p":"max","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1170,"y":160,"wires":[["4b392913.b6cd48"]]},{"id":"a638cb2f.c87ac8","type":"ui_button","z":"cfbb6e4f.6526a","name":"","group":"cf817aa5.c91f38","order":5,"width":8,"height":1,"passthru":false,"label":"Reset - Valor máximo","tooltip":"","color":"","bgcolor":"","icon":"","payload":"-1","payloadType":"num","topic":"","x":960,"y":160,"wires":[["8a137bbe.92b958"]]},{"id":"a14d793c.ec4b88","type":"ui_text","z":"cfbb6e4f.6526a","group":"cf817aa5.c91f38","order":1,"width":3,"height":1,"name":"valor max","label":"","format":"{{msg.payload}}","layout":"row-left","x":1440,"y":180,"wires":[]},{"id":"3d26ea37.f552b6","type":"switch","z":"cfbb6e4f.6526a","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"max","vt":"flow"},{"t":"lt","v":"min","vt":"flow"}],"checkall":"true","repair":false,"outputs":2,"x":1030,"y":200,"wires":[["8a137bbe.92b958"],["1377db47.ec6675"]]},{"id":"4b392913.b6cd48","type":"function","z":"cfbb6e4f.6526a","name":"","func":"if(msg.payload == -1)\n{\n msg.payload = \"-\"\n}else\n{\n msg.payload = msg.payload + \" cm \"\n}\nreturn msg;","outputs":1,"noerr":0,"x":1310,"y":160,"wires":[["a14d793c.ec4b88","17c6fe5d.7c1092"]]},{"id":"17c6fe5d.7c1092","type":"simpletime","z":"cfbb6e4f.6526a","name":"Data/hora","x":1440,"y":140,"wires":[["8d7fc11.652a84"]]},{"id":"8d7fc11.652a84","type":"function","z":"cfbb6e4f.6526a","name":"Composição data/hora","func":"var a = \"(\"+msg.mydom+\"/\"+msg.mymonthn+\"/\"+msg.myyear+ \" - \" +msg.mytimes+\")\";\nmsg.payload = a;\nreturn msg;","outputs":1,"noerr":0,"x":1640,"y":140,"wires":[["57f633dc.17b0fc"]]},{"id":"57f633dc.17b0fc","type":"ui_text","z":"cfbb6e4f.6526a","group":"cf817aa5.c91f38","order":2,"width":5,"height":1,"name":"Data/horário max","label":"","format":"{{msg.payload}}","layout":"row-left","x":1650,"y":180,"wires":[]},{"id":"1377db47.ec6675","type":"change","z":"cfbb6e4f.6526a","name":"","rules":[{"t":"set","p":"min","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1170,"y":240,"wires":[["d99940c4.a82bf"]]},{"id":"9e8de5b.9ed3318","type":"ui_button","z":"cfbb6e4f.6526a","name":"","group":"cf817aa5.c91f38","order":6,"width":8,"height":1,"passthru":false,"label":"Reset - Valor mínimo","tooltip":"","color":"","bgcolor":"","icon":"","payload":"10000","payloadType":"num","topic":"","x":960,"y":240,"wires":[["1377db47.ec6675"]]},{"id":"d99940c4.a82bf","type":"function","z":"cfbb6e4f.6526a","name":"","func":"if(msg.payload == 10000)\n{\n msg.payload = \"-\"\n}else\n{\n msg.payload = msg.payload + \" cm \"\n}\nreturn msg;","outputs":1,"noerr":0,"x":1310,"y":240,"wires":[["105c2154.24f3cf","366a70a1.6a146"]]},{"id":"105c2154.24f3cf","type":"ui_text","z":"cfbb6e4f.6526a","group":"cf817aa5.c91f38","order":3,"width":3,"height":1,"name":"valor min","label":"","format":"{{msg.payload}}","layout":"row-left","x":1440,"y":260,"wires":[]},{"id":"366a70a1.6a146","type":"simpletime","z":"cfbb6e4f.6526a","name":"Data/hora","x":1440,"y":220,"wires":[["df36bba2.63e798"]]},{"id":"df36bba2.63e798","type":"function","z":"cfbb6e4f.6526a","name":"Composição data/hora","func":"var a = \"(\"+msg.mydom+\"/\"+msg.mymonthn+\"/\"+msg.myyear+ \" - \" +msg.mytimes+\")\";\nmsg.payload = a;\nreturn msg;","outputs":1,"noerr":0,"x":1640,"y":220,"wires":[["d0f47e73.808f5"]]},{"id":"d0f47e73.808f5","type":"ui_text","z":"cfbb6e4f.6526a","group":"cf817aa5.c91f38","order":4,"width":5,"height":1,"name":"Data/horário min","label":"","format":"{{msg.payload}}","layout":"row-left","x":1650,"y":260,"wires":[]},{"id":"edf76319.1dc86","type":"function","z":"cfbb6e4f.6526a","name":"","func":"\nmsg.payload = Number(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":910,"y":200,"wires":[["3d26ea37.f552b6"]]},{"id":"cf817aa5.c91f38","type":"ui_group","z":"","name":"Valor máximo e mínimo registrados","tab":"afeb27be.73ba48","order":4,"disp":true,"width":"16","collapse":false},{"id":"afeb27be.73ba48","type":"ui_tab","z":"","name":"HC-SR04 - Distância","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

NOTE: I am Brazilian, so some terms are in Portuguese.

Thank you very much for your help and everyone who contributed in any way.

1 Like

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