Find smallest and greatest value

Assume a JSON like

where it is easy to build a sum by

msg = {"payload"    : msg.payload["AC Out L1"]
                    + msg.payload["AC Out L2"]
                    + msg.payload["AC Out L3"]};
return msg;

Now I like to calculate the difference between greatest and smallest value. Like any real measurements, values might change to any size. For 3 elements I need to have 5 if instructions in 3 ident levels for 6 cases:

case1 = L1-L2
case2 = L1-L3
case3 = L2-L3
case4 = L2-L1
case5 = L3-L2
case6 = L3-L1

For more elements, probably anything like a bubblesort loop is required. Is there anything on NodeReds default palette what can solve this matter? Application is my 3 phase Diesel genset what ideally should generate power for symmetric loads.

You can simple create an array of the values and sort their order form low to high, then just take the last array position and subtract the first array position. This should give you the difference between highest and lowest value.

const sorted_array = Object.values(msg.payload).sort((a, b) => a - b);
msg.payload = sorted_array[sorted_array.length - 1] - sorted_array[0];
return msg;
1 Like

Very quick idea for you to try out.
Note: I've added the difference to the original payload.


Node-RED json flow...

[{"id":"869324438973fbe4","type":"tab","label":"Difference","disabled":false,"info":"","env":[]},{"id":"c1a8b1d6a2a5a0cd","type":"inject","z":"869324438973fbe4","name":"Sample Input","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"AC_OUT_L1\":1227,\"AC_OUT_L2\":535,\"AC_OUT_L3\":592}","payloadType":"json","x":210,"y":140,"wires":[["de5d197d2c2594b6"]]},{"id":"de5d197d2c2594b6","type":"function","z":"869324438973fbe4","name":"Calculate Max-Min Difference","func":"let values = Object.values(msg.payload);\nlet max = Math.max(...values);\nlet min = Math.min(...values);\n\n// Option 1: Send just the difference\n// msg.payload = max - min;\n\n// Option 2: Add 'difference' to original object\nmsg.payload.difference = max - min;\n\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":140,"wires":[["94f3373de6c20137"]]},{"id":"94f3373de6c20137","type":"debug","z":"869324438973fbe4","name":"Show Result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":140,"wires":[]}]
2 Likes

Many thanks for both solutions. I am going to try. Again, its the power of Javascript, not only NodeRed. The project advances nice and the percentage of asymmetry is already in the dashboard (Schieflast).

Unfortunately, the float number is much too long. I really need to buy a Javascript book. Is there anything like printf to Google for?

Edit: Found the small sister of Math.min and Math.max what is Math.round - perfect. The Math.methods can do really a lot of things.

The definitive JS resource Number.prototype.toFixed() - JavaScript | MDN
Also many online tutorials learn javascript at DuckDuckGo