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

**URL:** <https://discourse.nodered.org/t/i-would-like-to-include-the-value-of-a-slider-in-a-function/66542>\
**Category:** General\
**Tags:** node-red-dashboard, function-node\
**Created:** [17 August 2022 09:25 UTC](https://discourse.nodered.org/t/i-would-like-to-include-the-value-of-a-slider-in-a-function/66542 "2022-08-17T09:25:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kieron1](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@kieron1](https://discourse.nodered.org/u/kieron1)\
**Post date:** [17 August 2022 09:25 UTC](https://discourse.nodered.org/t/i-would-like-to-include-the-value-of-a-slider-in-a-function/66542/1 "2022-08-17T09:25:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [17 August 2022 09:41 UTC](https://discourse.nodered.org/t/i-would-like-to-include-the-value-of-a-slider-in-a-function/66542/2 "2022-08-17T09:41:22Z")

</div>

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;`

 ![Untitled 3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b279ecedea9f6596d21a715465a90b3f252a3269.jpeg)

---

<div class="post-metadata">

**Author:** ![kieron1](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@kieron1](https://discourse.nodered.org/u/kieron1)\
**Post date:** [17 August 2022 09:55 UTC](https://discourse.nodered.org/t/i-would-like-to-include-the-value-of-a-slider-in-a-function/66542/3 "2022-08-17T09:55:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kieron1](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@kieron1](https://discourse.nodered.org/u/kieron1)\
**Post date:** [17 August 2022 10:06 UTC](https://discourse.nodered.org/t/i-would-like-to-include-the-value-of-a-slider-in-a-function/66542/4 "2022-08-17T10:06:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kieron1](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@kieron1](https://discourse.nodered.org/u/kieron1)\
**Post date:** [17 August 2022 10:07 UTC](https://discourse.nodered.org/t/i-would-like-to-include-the-value-of-a-slider-in-a-function/66542/5 "2022-08-17T10:07:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [17 August 2022 10:13 UTC](https://discourse.nodered.org/t/i-would-like-to-include-the-value-of-a-slider-in-a-function/66542/6 "2022-08-17T10:13:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kieron1](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@kieron1](https://discourse.nodered.org/u/kieron1)\
**Post date:** [17 August 2022 10:22 UTC](https://discourse.nodered.org/t/i-would-like-to-include-the-value-of-a-slider-in-a-function/66542/7 "2022-08-17T10:22:13Z")

</div>

> [@jbudd](#):
>
> let msgLimit = flow.get("msgLimit") || 500;

 ![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/8/f8cc48d45b7fbebca0653790667d16fd3b23c4d3.png)  
 ![2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/9/09cb06b4a4bc1b4c5ef14ec16613614c3e4ee69a.png)

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [17 August 2022 10:41 UTC](https://discourse.nodered.org/t/i-would-like-to-include-the-value-of-a-slider-in-a-function/66542/8 "2022-08-17T10:41:52Z")

</div>

![Untitled 4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/d/1d878c4a52d66b8a790845169c355ee355d95060.jpeg)

Your function looks ok at first glance. 🙃

---

<div class="post-metadata">

**Author:** ![kieron1](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@kieron1](https://discourse.nodered.org/u/kieron1)\
**Post date:** [17 August 2022 10:47 UTC](https://discourse.nodered.org/t/i-would-like-to-include-the-value-of-a-slider-in-a-function/66542/9 "2022-08-17T10:47:06Z")

</div>

I had a space in-between msg and Limit 🙃

Thankyou for your help today! All works successfully

---

<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:** [31 August 2022 10:47 UTC](https://discourse.nodered.org/t/i-would-like-to-include-the-value-of-a-slider-in-a-function/66542/10 "2022-08-31T10:47:53Z")

</div>

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