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

**URL:** <https://discourse.nodered.org/t/find-max-value-in-an-array-best-node-red-way/62823>\
**Category:** General\
**Created:** [18 May 2022 21:02 UTC](https://discourse.nodered.org/t/find-max-value-in-an-array-best-node-red-way/62823 "2022-05-18T21:02:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![n61ab](https://avatars.discourse-cdn.com/v4/letter/n/cc9497/32.png) [@n61ab](https://discourse.nodered.org/u/n61ab)\
**Post date:** [18 May 2022 21:02 UTC](https://discourse.nodered.org/t/find-max-value-in-an-array-best-node-red-way/62823/1 "2022-05-18T21:02:21Z")

</div>

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.

 ![Screen Shot 2022-05-18 at 4.55.02 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/4942d76876ba2a85e3b900602bd9a3eb93d70f83.jpeg)

Alan

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 May 2022 21:12 UTC](https://discourse.nodered.org/t/find-max-value-in-an-array-best-node-red-way/62823/2 "2022-05-18T21:12:18Z")

</div>

0 is the max number. Am I missing something?

---

<div class="post-metadata">

**Author:** ![n61ab](https://avatars.discourse-cdn.com/v4/letter/n/cc9497/32.png) [@n61ab](https://discourse.nodered.org/u/n61ab)\
**Post date:** [18 May 2022 21:34 UTC](https://discourse.nodered.org/t/find-max-value-in-an-array-best-node-red-way/62823/3 "2022-05-18T21:34:04Z")

</div>

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.

 ![Screen Shot 2022-05-18 at 5.29.26 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/3/439c187297ec336dcf0d2b453e0d1e73a32144e8.png)

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [19 May 2022 08:07 UTC](https://discourse.nodered.org/t/find-max-value-in-an-array-best-node-red-way/62823/4 "2022-05-19T08:07:02Z")

</div>

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.

```auto
[{"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}]

```

---

<div class="post-metadata">

**Author:** ![n61ab](https://avatars.discourse-cdn.com/v4/letter/n/cc9497/32.png) [@n61ab](https://discourse.nodered.org/u/n61ab)\
**Post date:** [19 May 2022 12:54 UTC](https://discourse.nodered.org/t/find-max-value-in-an-array-best-node-red-way/62823/5 "2022-05-19T12:54:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [19 May 2022 19:56 UTC](https://discourse.nodered.org/t/find-max-value-in-an-array-best-node-red-way/62823/6 "2022-05-19T19:56:10Z")

</div>

To get started, it takes to get started.  
[All about the HTML](https://www.w3schools.com/html/default.asp)  
[All about the CSS](https://www.w3schools.com/css/default.asp)  
[Stripes with CSS](https://css-tricks.com/stripes-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.

---

<div class="post-metadata">

**Author:** ![n61ab](https://avatars.discourse-cdn.com/v4/letter/n/cc9497/32.png) [@n61ab](https://discourse.nodered.org/u/n61ab)\
**Post date:** [19 May 2022 21:49 UTC](https://discourse.nodered.org/t/find-max-value-in-an-array-best-node-red-way/62823/7 "2022-05-19T21:49:55Z")

</div>

Thanks. Lot of learning.

Alan

---

<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:** [2 June 2022 21:50 UTC](https://discourse.nodered.org/t/find-max-value-in-an-array-best-node-red-way/62823/8 "2022-06-02T21:50:27Z")

</div>

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