Find Max Value In an Array: Best Node-Red Way

I have an array in flow context and want to find the maximum number value.

I have tried several of the Javascript math functions, with no success.

Is there a contrib node for finding the maximum value in an array?

Else, need help with a function. Here is my failed attempt. It always returns 0.

Alan

0 is the max number. Am I missing something?

1 Like

Thanks, Steve

dahhhhh. I am so a embarrassed!

By the way, I am having fun building a Signal Strength Dashboard gauge using your SVG node and the "binding" tab, hence the negative numbers. I am climbing the SVG learning curve. I want to add a marker for the maximum value, hence the function to find the "min" value.

Here is a screenshot. I used your "window shade" example to build the gauge graphic and like your example, am moving the black rectangle to expose the gauge level element.

2 Likes

SVG is fun for sure, but is it really needed to be that complex.
This kind of stuff is easy enough to achieve with basic HTML elements.

[{"id":"8d516329880a7b2d","type":"random","z":"9141c3fbcc7d9e2b","name":"","low":"-100","high":"0","inte":"true","property":"payload","x":480,"y":520,"wires":[["463566f7274c285b"]]},{"id":"a230737c1703a91e","type":"inject","z":"9141c3fbcc7d9e2b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"3","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":310,"y":520,"wires":[["8d516329880a7b2d"]]},{"id":"d02a005c014fb48b","type":"ui_template","z":"9141c3fbcc7d9e2b","group":"f6124e036e2d2ffb","name":"DIY level","order":0,"width":0,"height":0,"format":"<style>\n.level{\n  position:absolute;\n  inset:0;\n  top:50%;\n  bottom:15%;\n  background:repeating-linear-gradient( 90deg, green, green 5px, transparent 5px, transparent 8px);\n  width:calc(1% * var(--size));\n}\n.value{\n  position:absolute;\n  text-align:center;\n  inset:0;\n}\n.limits{\n  font-size:10px;\n}\n.min{\n  position:absolute;\n}\n.max{\n  position:absolute;\n  right:0;\n  margin-right:4px;\n}\n.top{\n  background:red;\n  position:absolute;\n  inset:0;\n  top:95%;\n  width:calc(1% * var(--top));\n  \n}\n</style>\n\n<div id=\"{{'level_min_'+$id}}\" class=\"limits min\">-100</div>\n<div id=\"{{'level_max_'+$id}}\" class=\"limits max\">0</div>\n<div id=\"{{'level_val_'+$id}}\" class=\"value\"></div>\n<div id=\"{{'level_'+$id}}\" class=\"level\"></div>\n<div id=\"{{'level_top'+$id}}\" class=\"top\"></div>\n\n<script>\n    (function(scope) {\n      const min = -100\n      const max = 0\n      const label = \"DIY Level \"\n      const unit = \" dBm\"\n  scope.$watch('msg', function(msg) {\n    if (msg) {\n      // Do something when msg arrives\n      const v = Math.floor(((msg.payload - min) / (max - min)) * 100);\n      const t = Math.floor(((msg.top - min) / (max - min)) * 100);\n      document.getElementById('level_'+scope.$id).style.setProperty('--size', v);\n      document.getElementById('level_top'+scope.$id).style.setProperty('--top', t);\n      document.getElementById('level_val_'+scope.$id).textContent = label + msg.payload + unit;\n    }\n  });\n})(scope);\n</script>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","className":"","x":780,"y":520,"wires":[[]]},{"id":"463566f7274c285b","type":"function","z":"9141c3fbcc7d9e2b","name":"","func":"let last = context.get('last') || Number.MIN_SAFE_INTEGER\nif(msg.payload > last){\n    last = msg.payload;\n    context.set('last',last)\n}\nmsg.top = last\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":520,"wires":[["d02a005c014fb48b"]]},{"id":"f6124e036e2d2ffb","type":"ui_group","name":"Default","tab":"34ab7d39f3e308fc","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"34ab7d39f3e308fc","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]
1 Like

Thanks, hotNipi

I am very low on the learning curve and found the SVG node a "low code" way to build my meter.

I want to learn HTML. Could you point me to some references (starting points) to learn how you built the DYI meter?

Alan

To get started, it takes to get started.
All about the HTML
All about the CSS
Stripes with CSS
And everything else is mostly written into ui_template node's help page

Of course it takes some time to get it going cos you kind of need to learn 3 languages at same time. But they are tightly connected so it seems like one.

And the web is full of examples and explanations - key is to learn how to search and how to use tips and tricks to achieve what ever you are targeting.

1 Like

Thanks. Lot of learning.

Alan

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