Newbie node function javascript

Hi all,
I am newbie with node-red and I have a question ... after a slider node I put a Gauge node .. how to get the max value of my flow to show it on my gauge ??? it means, if the value of the flow decrease, my gauge will not indicate an inferior value .... sorry for my poor English .. I can't do it with a function and javascript ... thanks a lot !

you are going to have to store the max reading in context storage, then each time compare the new reading to the stored value if it is less ignore, if greater store and use.

https://nodered.org/docs/user-guide/context

Try and work something out. if you have trouble post what you have tried, others will point you in right direction.

Thanks a lot, now I have juste to learn to master this concept !!

Hi @andregilles, welcome to the forum.

I assume you want your Gauge to indicate the maximum value it has seen.

Don't be tempted to jump to solving the problem with Javascript. It can be done with a Switch node, a Change node and a context variable to store the maximum.

This flow has a Reset button too:

2 Likes

Great Judd !! thanks ! it works fine !! I fixes this issue with Javascript as E1cid told me

it woks now !! thanks a lot with context storage

Hi,
my node gauge works fine and indicate max value now, I created another with min value! but now I try to use an node button to "reset" it ... I mean when I press the button I want the gauges to be setting at the entry value .... I can't manage to do It with a node button ?? When I choose Payload "buffer" it works but the value still forced at the value when I pressed ... can you help me ?

Thanks for help ! I fixed my issue with a simple Javascript in a node function but I appreciate you solution too

I would need to see your flow, export it and post here.
when posying use the </> button and put the code between the back ticks.
e.g.
```
code
```

this my flow ... the RAZ button is the problem I think ...

[
    {
        "id": "31112c4beedfab1b",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "b483efaf6a581ac7",
        "type": "ui_base",
        "theme": {
            "name": "theme-light",
            "lightTheme": {
                "default": "#0094CE",
                "baseColor": "#0094CE",
                "baseFont": "-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif",
                "edited": true,
                "reset": false
            },
            "darkTheme": {
                "default": "#097479",
                "baseColor": "#097479",
                "baseFont": "-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif",
                "edited": false
            },
            "customTheme": {
                "name": "Untitled Theme 1",
                "default": "#4B7930",
                "baseColor": "#4B7930",
                "baseFont": "-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"
            },
            "themeState": {
                "base-color": {
                    "default": "#0094CE",
                    "value": "#0094CE",
                    "edited": false
                },
                "page-titlebar-backgroundColor": {
                    "value": "#0094CE",
                    "edited": false
                },
                "page-backgroundColor": {
                    "value": "#fafafa",
                    "edited": false
                },
                "page-sidebar-backgroundColor": {
                    "value": "#ffffff",
                    "edited": false
                },
                "group-textColor": {
                    "value": "#1bbfff",
                    "edited": false
                },
                "group-borderColor": {
                    "value": "#ffffff",
                    "edited": false
                },
                "group-backgroundColor": {
                    "value": "#ffffff",
                    "edited": false
                },
                "widget-textColor": {
                    "value": "#111111",
                    "edited": false
                },
                "widget-backgroundColor": {
                    "value": "#0094ce",
                    "edited": false
                },
                "widget-borderColor": {
                    "value": "#ffffff",
                    "edited": false
                },
                "base-font": {
                    "value": "-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"
                }
            },
            "angularTheme": {
                "primary": "indigo",
                "accents": "blue",
                "warn": "red",
                "background": "grey",
                "palette": "light"
            }
        },
        "site": {
            "name": "Node-RED Dashboard",
            "hideToolbar": "false",
            "allowSwipe": "false",
            "lockMenu": "false",
            "allowTempTheme": "true",
            "dateFormat": "DD/MM/YYYY",
            "sizes": {
                "sx": 48,
                "sy": 48,
                "gx": 6,
                "gy": 6,
                "cx": 6,
                "cy": 6,
                "px": 0,
                "py": 0
            }
        }
    },
    {
        "id": "6eca944f9b78667a",
        "type": "ui_tab",
        "name": "ma petite station",
        "icon": "dashboard",
        "order": 1,
        "disabled": false,
        "hidden": false
    },
    {
        "id": "cf7546fc50cad290",
        "type": "ui_group",
        "name": "température extérieure",
        "tab": "6eca944f9b78667a",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "d715b1cf3e372f0c",
        "type": "ui_slider",
        "z": "31112c4beedfab1b",
        "name": "",
        "label": "slider",
        "tooltip": "",
        "group": "cf7546fc50cad290",
        "order": 1,
        "width": 0,
        "height": 0,
        "passthru": true,
        "outs": "all",
        "topic": "topic",
        "topicType": "msg",
        "min": 0,
        "max": "100",
        "step": 1,
        "className": "",
        "x": 130,
        "y": 60,
        "wires": [
            [
                "a45e41ecd254f1f2",
                "295bd78c155c983d",
                "83683782efe40c3d",
                "5f98b2da1cb8bae1",
                "1f42790ea26ce9a8"
            ]
        ]
    },
    {
        "id": "a45e41ecd254f1f2",
        "type": "ui_gauge",
        "z": "31112c4beedfab1b",
        "name": "",
        "group": "cf7546fc50cad290",
        "order": 2,
        "width": 0,
        "height": 0,
        "gtype": "gage",
        "title": "actuelle",
        "label": "°C",
        "format": "{{value}}",
        "min": 0,
        "max": "100",
        "colors": [
            "#4da1b2",
            "#63e632",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "className": "",
        "x": 820,
        "y": 60,
        "wires": []
    },
    {
        "id": "3feb2eaedf674eab",
        "type": "ui_gauge",
        "z": "31112c4beedfab1b",
        "name": "",
        "group": "cf7546fc50cad290",
        "order": 4,
        "width": "3",
        "height": "2",
        "gtype": "gage",
        "title": "maxi",
        "label": "°C",
        "format": "{{value}}",
        "min": 0,
        "max": "100",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "className": "",
        "x": 830,
        "y": 120,
        "wires": []
    },
    {
        "id": "295bd78c155c983d",
        "type": "function",
        "z": "31112c4beedfab1b",
        "name": "",
        "func": "var maxi =flow.get(\"val_max\");\n//node.warn(\"la valeur de maxi est: \" + maxi);\nvar lecture = msg.payload;\n//node.warn(\"la valeur entrée: \" + lecture);\nif (lecture >= maxi)\n{\n    flow.set(\"val_max\",lecture);\n    return msg;\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "// Code added here will be run once\n// whenever the node is started.\nflow.set(\"val_max\",0);",
        "finalize": "",
        "libs": [],
        "x": 460,
        "y": 120,
        "wires": [
            [
                "3feb2eaedf674eab"
            ]
        ]
    },
    {
        "id": "83683782efe40c3d",
        "type": "function",
        "z": "31112c4beedfab1b",
        "name": "",
        "func": "var mini =flow.get(\"val_min\");\n//node.warn(\"la valeur de maxi est: \" + maxi);\nvar lecture = msg.payload;\n//node.warn(\"la valeur entrée: \" + lecture);\nif (lecture < mini)\n{\n    flow.set(\"val_min\",lecture);\n    return msg;\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "// Code added here will be run once\n// whenever the node is started.\nflow.set(\"val_min\",100);",
        "finalize": "",
        "libs": [],
        "x": 460,
        "y": 180,
        "wires": [
            [
                "01c0e1201fdf1ae0"
            ]
        ]
    },
    {
        "id": "01c0e1201fdf1ae0",
        "type": "ui_gauge",
        "z": "31112c4beedfab1b",
        "name": "",
        "group": "cf7546fc50cad290",
        "order": 5,
        "width": "3",
        "height": "2",
        "gtype": "gage",
        "title": "mini",
        "label": "°C",
        "format": "{{value}}",
        "min": 0,
        "max": "100",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "className": "",
        "x": 830,
        "y": 180,
        "wires": []
    },
    {
        "id": "1905cf228e762479",
        "type": "debug",
        "z": "31112c4beedfab1b",
        "name": "raz",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 310,
        "y": 360,
        "wires": []
    },
    {
        "id": "5f98b2da1cb8bae1",
        "type": "ui_button",
        "z": "31112c4beedfab1b",
        "name": "",
        "group": "cf7546fc50cad290",
        "order": 6,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "raz",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "restore",
        "payload": "",
        "payloadType": "bin",
        "topic": "topic",
        "topicType": "msg",
        "x": 130,
        "y": 300,
        "wires": [
            [
                "33bae8ca7060b7e2",
                "3feb2eaedf674eab",
                "1905cf228e762479"
            ]
        ]
    },
    {
        "id": "4908a3b26c280fa6",
        "type": "ui_text",
        "z": "31112c4beedfab1b",
        "group": "cf7546fc50cad290",
        "order": 7,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "raz le ",
        "format": "{{msg.payload}}",
        "layout": "row-center",
        "className": "",
        "x": 830,
        "y": 240,
        "wires": []
    },
    {
        "id": "33bae8ca7060b7e2",
        "type": "change",
        "z": "31112c4beedfab1b",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$now('[D01].[M01].[Y0001] Ă  [H01]:[m01]:[s01]', '+0100')",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 480,
        "y": 300,
        "wires": [
            [
                "4908a3b26c280fa6"
            ]
        ]
    },
    {
        "id": "1f42790ea26ce9a8",
        "type": "ui_chart",
        "z": "31112c4beedfab1b",
        "name": "",
        "group": "cf7546fc50cad290",
        "order": 3,
        "width": 0,
        "height": 0,
        "label": "température (°C)",
        "chartType": "line",
        "legend": "false",
        "xformat": "HH:mm",
        "interpolate": "linear",
        "nodata": "",
        "dot": false,
        "ymin": "0",
        "ymax": "100",
        "removeOlder": 1,
        "removeOlderPoints": "",
        "removeOlderUnit": "86400",
        "cutout": 0,
        "useOneColor": false,
        "useUTC": false,
        "colors": [
            "#1f77b4",
            "#aec7e8",
            "#ff7f0e",
            "#2ca02c",
            "#98df8a",
            "#d62728",
            "#ff9896",
            "#9467bd",
            "#c5b0d5"
        ],
        "outputs": 1,
        "useDifferentColor": false,
        "className": "",
        "x": 840,
        "y": 20,
        "wires": [
            []
        ]
    }
]

It certainly was when i pressed it it crashed node-red. It was the setting buffer that caused it.
anyway to reset your gauges you need to set both flow vars to 0 and set msg payload to 0
e.g.

[{"id":"b42c6209.25967","type":"ui_slider","z":"302ecf16.a616f","name":"","label":"slider","tooltip":"","group":"cf7546fc50cad290","order":1,"width":0,"height":0,"passthru":true,"outs":"all","topic":"topic","topicType":"msg","min":0,"max":"100","step":1,"x":60,"y":120,"wires":[["4854c25f.d1f03c","907fbb43.fea228","76ff15f1.2fda5c","13d74806.e2081"]]},{"id":"4854c25f.d1f03c","type":"ui_gauge","z":"302ecf16.a616f","name":"","group":"cf7546fc50cad290","order":2,"width":0,"height":0,"gtype":"gage","title":"actuelle","label":"°C","format":"{{value}}","min":0,"max":"100","colors":["#4da1b2","#63e632","#ca3838"],"seg1":"","seg2":"","x":750,"y":120,"wires":[]},{"id":"907fbb43.fea228","type":"function","z":"302ecf16.a616f","name":"","func":"var maxi =flow.get(\"val_max\");\n//node.warn(\"la valeur de maxi est: \" + maxi);\nvar lecture = msg.payload;\n//node.warn(\"la valeur entrée: \" + lecture);\nif (lecture >= maxi)\n{\n    flow.set(\"val_max\",lecture);\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nflow.set(\"val_max\",0);","finalize":"","x":390,"y":180,"wires":[["a4ecd893.56dfe8"]]},{"id":"76ff15f1.2fda5c","type":"function","z":"302ecf16.a616f","name":"","func":"var mini =flow.get(\"val_min\");\n//node.warn(\"la valeur de maxi est: \" + maxi);\nvar lecture = msg.payload;\n//node.warn(\"la valeur entrée: \" + lecture);\nif (lecture < mini)\n{\n    flow.set(\"val_min\",lecture);\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nflow.set(\"val_min\",100);","finalize":"","x":390,"y":240,"wires":[["903772f7.cb9548"]]},{"id":"13d74806.e2081","type":"ui_chart","z":"302ecf16.a616f","name":"","group":"cf7546fc50cad290","order":3,"width":0,"height":0,"label":"température (°C)","chartType":"line","legend":"false","xformat":"HH:mm","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"100","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"86400","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"x":770,"y":80,"wires":[[]]},{"id":"a4ecd893.56dfe8","type":"ui_gauge","z":"302ecf16.a616f","name":"","group":"cf7546fc50cad290","order":4,"width":"3","height":"2","gtype":"gage","title":"maxi","label":"°C","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":760,"y":180,"wires":[]},{"id":"903772f7.cb9548","type":"ui_gauge","z":"302ecf16.a616f","name":"","group":"cf7546fc50cad290","order":5,"width":"3","height":"2","gtype":"gage","title":"mini","label":"°C","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":760,"y":240,"wires":[]},{"id":"a1304eb7.2e766","type":"change","z":"302ecf16.a616f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"},{"t":"set","p":"val_min","pt":"flow","to":"100","tot":"num"},{"t":"set","p":"val_max","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":240,"y":320,"wires":[["a4ecd893.56dfe8","903772f7.cb9548"]]},{"id":"8d3068f1.7921a8","type":"ui_button","z":"302ecf16.a616f","name":"","group":"cf7546fc50cad290","order":6,"width":0,"height":0,"passthru":false,"label":"raz","tooltip":"","color":"","bgcolor":"","icon":"restore","payload":"0","payloadType":"num","topic":"topic","topicType":"msg","x":60,"y":360,"wires":[["af82514b.5256e","88431289.1ae8d","a1304eb7.2e766"]]},{"id":"af82514b.5256e","type":"change","z":"302ecf16.a616f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$now('[D01].[M01].[Y0001] à [H01]:[m01]:[s01]', '+0100')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":360,"wires":[["45a4f89b.bf7a48"]]},{"id":"88431289.1ae8d","type":"debug","z":"302ecf16.a616f","name":"raz","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":240,"y":420,"wires":[]},{"id":"45a4f89b.bf7a48","type":"ui_text","z":"302ecf16.a616f","group":"cf7546fc50cad290","order":7,"width":0,"height":0,"name":"","label":"raz le ","format":"{{msg.payload}}","layout":"row-center","x":760,"y":300,"wires":[]},{"id":"cf7546fc50cad290","type":"ui_group","name":"température extérieure","tab":"6eca944f9b78667a","order":1,"disp":true,"width":"6","collapse":false},{"id":"6eca944f9b78667a","type":"ui_tab","name":"ma petite station","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

[edit]
flow min should be sett high and msg.payload 0
i have adjusted the flow json above

At risk of repeating myself...

You seem to have a problem with Raz and the minimum value. Once you reset it stays firmly at 0.
You could reset flow.val_min to 100 or to a new variable flow.val_curr.

[{"id":"d715b1cf3e372f0c","type":"ui_slider","z":"31112c4beedfab1b","name":"","label":"slider","tooltip":"","group":"36860d57c02ea099","order":1,"width":0,"height":0,"passthru":true,"outs":"all","topic":"topic","topicType":"msg","min":0,"max":"100","step":1,"className":"","x":330,"y":60,"wires":[["1f42790ea26ce9a8","a45e41ecd254f1f2","2f3a5090134e2463"]]},{"id":"a45e41ecd254f1f2","type":"ui_gauge","z":"31112c4beedfab1b","name":"","group":"36860d57c02ea099","order":2,"width":0,"height":0,"gtype":"gage","title":"actuelle","label":"°C","format":"{{value}}","min":0,"max":"100","colors":["#4da1b2","#63e632","#ca3838"],"seg1":"","seg2":"","className":"","x":820,"y":100,"wires":[]},{"id":"3feb2eaedf674eab","type":"ui_gauge","z":"31112c4beedfab1b","name":"","group":"36860d57c02ea099","order":4,"width":"3","height":"2","gtype":"gage","title":"maxi","label":"°C","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","className":"","x":810,"y":140,"wires":[]},{"id":"01c0e1201fdf1ae0","type":"ui_gauge","z":"31112c4beedfab1b","name":"","group":"36860d57c02ea099","order":5,"width":"3","height":"2","gtype":"gage","title":"mini","label":"°C","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","className":"","x":810,"y":180,"wires":[]},{"id":"5f98b2da1cb8bae1","type":"ui_button","z":"31112c4beedfab1b","name":"","group":"36860d57c02ea099","order":6,"width":0,"height":0,"passthru":false,"label":"raz","tooltip":"","color":"","bgcolor":"","className":"","icon":"restore","payload":"0","payloadType":"num","topic":"topic","topicType":"msg","x":330,"y":220,"wires":[["2f3a5090134e2463","58fd60bd675e68fc"]]},{"id":"4908a3b26c280fa6","type":"ui_text","z":"31112c4beedfab1b","group":"36860d57c02ea099","order":7,"width":0,"height":0,"name":"","label":"raz le ","format":"{{msg.payload}}","layout":"row-center","className":"","x":810,"y":220,"wires":[]},{"id":"1f42790ea26ce9a8","type":"ui_chart","z":"31112c4beedfab1b","name":"","group":"36860d57c02ea099","order":3,"width":0,"height":0,"label":"température (°C)","chartType":"line","legend":"false","xformat":"HH:mm","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"100","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"86400","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"className":"","x":850,"y":60,"wires":[[]]},{"id":"2f3a5090134e2463","type":"switch","z":"31112c4beedfab1b","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"val_max","vt":"flow"},{"t":"eq","v":"0","vt":"num"},{"t":"lte","v":"val_min","vt":"flow"}],"checkall":"true","repair":false,"outputs":3,"x":470,"y":160,"wires":[["3e06b7fab600d4e1"],["3e06b7fab600d4e1","28059d4d12ad4d2f"],["28059d4d12ad4d2f"]]},{"id":"3e06b7fab600d4e1","type":"change","z":"31112c4beedfab1b","name":"","rules":[{"t":"set","p":"val_max","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":140,"wires":[["3feb2eaedf674eab"]]},{"id":"28059d4d12ad4d2f","type":"change","z":"31112c4beedfab1b","name":"Set flow.val_min","rules":[{"t":"change","p":"payload","pt":"msg","from":"0","fromt":"num","to":"100","tot":"num"},{"t":"set","p":"val_min","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":180,"wires":[["01c0e1201fdf1ae0"]]},{"id":"58fd60bd675e68fc","type":"change","z":"31112c4beedfab1b","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$now('[D01].[M01].[Y0001] à [H01]:[m01]:[s01]', '+0100')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":220,"wires":[["4908a3b26c280fa6"]]},{"id":"36860d57c02ea099","type":"ui_group","name":"Default","tab":"539e2869c9c56bed","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"539e2869c9c56bed","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

It works better thanks, I just modify the first set in the new change node to msg.payload instead of 0
Like this, the effect on button is to init the gauges with the actuel value of the slider, it's exactly what I wanted !!

Again, a good solution ... thanks

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