I would like to include the value of a slider in a function

[
    {
        "id": "2c2aeb8b4fec5a87",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "315086cb1d9bff21",
        "type": "function",
        "z": "2c2aeb8b4fec5a87",
        "name": "",
        "func": "let msgCount = context.get(\"msgCount\") || 0;//get remembered count\nlet sendMessage = false;\n\nif (msg.payload === 1) {\n    msgCount++; //increment count\n    node.status(\"Count of payload 1: \" + msgCount);\n    if (msgCount >= 500)  {\n        msgCount = 0;//reset counter\n        sendMessage = true;//instruct code to send the msg\n        msg.payload = \"0\"; //set payload to a string of 1 as you requested\n    }\n}\n\ncontext.set(\"msgCount\", msgCount); //remember count\n\nif(sendMessage) {\n    return msg;//send msg to next node\n}\n\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 680,
        "y": 160,
        "wires": [
            [
                "9bd4724dbaf29a4e",
                "66a2372338a5df4e"
            ]
        ]
    },
    {
        "id": "9bd4724dbaf29a4e",
        "type": "debug",
        "z": "2c2aeb8b4fec5a87",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1100,
        "y": 120,
        "wires": []
    },
    {
        "id": "7c06fe830b7a1ebf",
        "type": "inject",
        "z": "2c2aeb8b4fec5a87",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "0",
        "payloadType": "num",
        "x": 390,
        "y": 260,
        "wires": [
            [
                "315086cb1d9bff21"
            ]
        ]
    },
    {
        "id": "efaf1d014a100842",
        "type": "rpi-gpio in",
        "z": "2c2aeb8b4fec5a87",
        "name": "",
        "pin": "10",
        "intype": "tri",
        "debounce": "25",
        "read": false,
        "bcm": true,
        "x": 170,
        "y": 200,
        "wires": [
            [
                "315086cb1d9bff21"
            ]
        ]
    },
    {
        "id": "57670cb0da43a95c",
        "type": "inject",
        "z": "2c2aeb8b4fec5a87",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "0.1",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "1",
        "payloadType": "num",
        "x": 290,
        "y": 80,
        "wires": [
            [
                "315086cb1d9bff21"
            ]
        ]
    },
    {
        "id": "53cfe7c73f8b7a32",
        "type": "ui_button",
        "z": "2c2aeb8b4fec5a87",
        "name": "",
        "group": "85f720af4449497c",
        "order": 4,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "button",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "1",
        "payloadType": "num",
        "topic": "topic",
        "topicType": "msg",
        "x": 510,
        "y": 60,
        "wires": [
            [
                "9bd4724dbaf29a4e",
                "66a2372338a5df4e"
            ]
        ]
    },
    {
        "id": "66a2372338a5df4e",
        "type": "rpi-gpio out",
        "z": "2c2aeb8b4fec5a87",
        "name": "",
        "pin": "5",
        "set": true,
        "level": "0",
        "freq": "",
        "out": "out",
        "bcm": true,
        "x": 1080,
        "y": 260,
        "wires": []
    },
    {
        "id": "85f720af4449497c",
        "type": "ui_group",
        "name": "Refill",
        "tab": "643eb5c118adb915",
        "order": 2,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "643eb5c118adb915",
        "type": "ui_tab",
        "name": "Watering",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

Background: Water flow sensor that pulls the pin high in pulses.
Counts the pulses untill it reaches a set limit then send 0 to a pin

The inject node is just simulating a pulse
I have the raspberry pi nodes installed

I have this working code but the only way i can edit the if (msgCount = 500) is by editing the function. I would like the number (500) to be editable by a slider on the UI

I am sorry if this is formatted wrong. This is my first post

Hi @kieron1.

You are already saving the number of messages received as a context variable context.msgCount.
Why not store the slider value in a flow level variable flow.msgLimit?

let msgLimit = flow.get("msgLimit") || 500;

1 Like

Could you post the code to the edited flow please? I really appreciate your help.

I am trying to implement it but I am finding it hard to put everything in the right place

[
    {
        "id": "2c2aeb8b4fec5a87",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "315086cb1d9bff21",
        "type": "function",
        "z": "2c2aeb8b4fec5a87",
        "name": "",
        "func": "let msgCount = context.get(\"msgCount\") || 0;//get remembered count\nlet msgLimit = flow.get(\"msgLimit\") || 500;\nlet sendMessage = false;\n\nif (msg.payload === 1) {\n    msgCount++; //increment count\n    node.status(\"Count of payload 1: \" + msgCount);\n    if (msgCount >= msgLimit)  {\n        msgCount = 0;//reset counter\n        sendMessage = true;//instruct code to send the msg\n        msg.payload = \"0\"; //set payload to a string of 1 as you requested\n    }\n}\n\ncontext.set(\"msgCount\", msgCount); //remember count\n\nif(sendMessage) {\n    return msg;//send msg to next node\n}\n\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 680,
        "y": 160,
        "wires": [
            [
                "9bd4724dbaf29a4e",
                "66a2372338a5df4e"
            ]
        ]
    },
    {
        "id": "9bd4724dbaf29a4e",
        "type": "debug",
        "z": "2c2aeb8b4fec5a87",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1100,
        "y": 120,
        "wires": []
    },
    {
        "id": "7c06fe830b7a1ebf",
        "type": "inject",
        "z": "2c2aeb8b4fec5a87",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "0",
        "payloadType": "num",
        "x": 390,
        "y": 260,
        "wires": [
            [
                "315086cb1d9bff21"
            ]
        ]
    },
    {
        "id": "efaf1d014a100842",
        "type": "rpi-gpio in",
        "z": "2c2aeb8b4fec5a87",
        "name": "",
        "pin": "10",
        "intype": "tri",
        "debounce": "25",
        "read": false,
        "bcm": true,
        "x": 170,
        "y": 200,
        "wires": [
            [
                "315086cb1d9bff21"
            ]
        ]
    },
    {
        "id": "57670cb0da43a95c",
        "type": "inject",
        "z": "2c2aeb8b4fec5a87",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "1",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "1",
        "payloadType": "num",
        "x": 290,
        "y": 80,
        "wires": [
            [
                "315086cb1d9bff21"
            ]
        ]
    },
    {
        "id": "53cfe7c73f8b7a32",
        "type": "ui_button",
        "z": "2c2aeb8b4fec5a87",
        "name": "",
        "group": "85f720af4449497c",
        "order": 4,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "button",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "1",
        "payloadType": "num",
        "topic": "topic",
        "topicType": "msg",
        "x": 510,
        "y": 60,
        "wires": [
            [
                "9bd4724dbaf29a4e",
                "66a2372338a5df4e"
            ]
        ]
    },
    {
        "id": "66a2372338a5df4e",
        "type": "rpi-gpio out",
        "z": "2c2aeb8b4fec5a87",
        "name": "",
        "pin": "5",
        "set": true,
        "level": "0",
        "freq": "",
        "out": "out",
        "bcm": true,
        "x": 1280,
        "y": 180,
        "wires": []
    },
    {
        "id": "c6a1871033340449",
        "type": "ui_slider",
        "z": "2c2aeb8b4fec5a87",
        "name": "",
        "label": "slider",
        "tooltip": "",
        "group": "0b2865f895170492",
        "order": 4,
        "width": 0,
        "height": 0,
        "passthru": true,
        "outs": "all",
        "topic": "topic",
        "topicType": "msg",
        "min": 0,
        "max": 10,
        "step": 1,
        "className": "",
        "x": 690,
        "y": 420,
        "wires": [
            [
                "892c80cc35263811"
            ]
        ]
    },
    {
        "id": "892c80cc35263811",
        "type": "change",
        "z": "2c2aeb8b4fec5a87",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "msgLimit",
                "pt": "flow",
                "to": "limit",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1010,
        "y": 460,
        "wires": [
            []
        ]
    },
    {
        "id": "85f720af4449497c",
        "type": "ui_group",
        "name": "Refill",
        "tab": "643eb5c118adb915",
        "order": 2,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "0b2865f895170492",
        "type": "ui_group",
        "name": "Leaf",
        "tab": "643eb5c118adb915",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "643eb5c118adb915",
        "type": "ui_tab",
        "name": "Watering",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

This is what I have tried

OK ...

The Slider outputs it's value in msg.payload, so you need to set flow.msgLimit from that, not from msg.limit (which as far as I can see does not exist).

You can set the minimum and maximum Slider values, maybe 1 to 500 is better than 0 to 10.

Set the slider to output a value only on release.


What do I set to the value? and also it this function ok?

Your function looks ok at first glance. :upside_down_face:

I had a space in-between msg and Limit :upside_down_face:

Thankyou for your help today! All works successfully

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