Disable flow / injection using a button on the dashboard

Hi all,
I am totally new here and I am not really familiar with software development and things like this...
I have created an example with a gauge on a dashboard feed with a random number activated using a inject node firing every second.
Is it possible to stop the inject node firing using a button on the dashboard?
I did see some answers about similar topics in this forum but I did not understand them because they have been not detailed enough for me.
Thx.

Code:

[{"id":"9aace6cea28b481c","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"cc5776f80f3c2057","type":"inject","z":"9aace6cea28b481c","name":"Fire every second","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"0.1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":130,"y":200,"wires":[["d37757ff7a3c7709"]]},{"id":"d37757ff7a3c7709","type":"function","z":"9aace6cea28b481c","name":"Generate random number","func":"msg.payload = Math.round(Math.random()*100);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":200,"wires":[["dabb6428037d7bb8","21bc70e9f271307e"]]},{"id":"dabb6428037d7bb8","type":"ui_text","z":"9aace6cea28b481c","group":"180ab5fa68364540","order":0,"width":0,"height":0,"name":"Data output","label":"text","format":"{{msg.payload}}","layout":"row-spread","className":"","x":650,"y":200,"wires":[]},{"id":"21bc70e9f271307e","type":"ui_gauge","z":"9aace6cea28b481c","name":"","group":"180ab5fa68364540","order":1,"width":0,"height":0,"gtype":"gage","title":"gauge","label":"units","format":"{{value}}","min":0,"max":"100","colors":["#ff0000","#e6e600","#00ff00"],"seg1":"","seg2":"","className":"","x":660,"y":340,"wires":[]},{"id":"a03e133dad2cd187","type":"ui_spacer","z":"9aace6cea28b481c","name":"spacer","group":"042b2df9480e2a11","order":2,"width":6,"height":1},{"id":"238321a2c1bfc676","type":"ui_spacer","z":"9aace6cea28b481c","name":"spacer","group":"042b2df9480e2a11","order":3,"width":6,"height":1},{"id":"180ab5fa68364540","type":"ui_group","name":"Dashboard Demo","tab":"1fb3b04341d0523d","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"1fb3b04341d0523d","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

Hi, Try this. [{"id":"8bd8bcc8e3cedec4","type":"ui_switch","z":"9aace6cea28b481c","name":"","label":"switch","tooltip":"","group":"180ab5fa68364540","order":2,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"onOff","topicType":"str","style":"","onvalue":"on","onvalueType":"str","onicon":"","oncolor":"","offvalue":"off","offvalueType":"str","officon":"","offcolor":"","animate":false,"className":"","x":170,"y":260,"wires":[["d37757ff7a3c7709"]]},{"id":"180ab5fa68364540","type":"ui_group","name":"Dashboard Demo","tab":"1fb3b04341d0523d","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"1fb3b04341d0523d","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

Change the switch from true and false to "on" and "off" (so that you can use x=flow.get(name) || default) and set the topic to the string "onOff".

You can simply block the flow of messages from the inject.

Here is an example (with code) : Cancel inputs for a period of time - #6 by Steve-Mcl

Simply fire those block/unblock messages from dashboard buttons.

Here's an example not to stop the inject from firing, but from the random number to be generated with a button.

[
    {
        "id": "cc5776f80f3c2057",
        "type": "inject",
        "z": "9aace6cea28b481c",
        "name": "Fire every second",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "v": "active",
                "vt": "flow"
            }
        ],
        "repeat": "1",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "date",
        "x": 130,
        "y": 200,
        "wires": [
            [
                "d37757ff7a3c7709"
            ]
        ]
    },
    {
        "id": "d37757ff7a3c7709",
        "type": "function",
        "z": "9aace6cea28b481c",
        "name": "Generate random number",
        "func": "if(msg.topic){\n    msg.payload = Math.round(Math.random()*100);\n    return msg;\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 380,
        "y": 200,
        "wires": [
            [
                "dabb6428037d7bb8",
                "21bc70e9f271307e"
            ]
        ]
    },
    {
        "id": "dabb6428037d7bb8",
        "type": "ui_text",
        "z": "9aace6cea28b481c",
        "group": "180ab5fa68364540",
        "order": 2,
        "width": 0,
        "height": 0,
        "name": "Data output",
        "label": "text",
        "format": "{{msg.payload}}",
        "layout": "row-spread",
        "className": "",
        "x": 650,
        "y": 200,
        "wires": []
    },
    {
        "id": "21bc70e9f271307e",
        "type": "ui_gauge",
        "z": "9aace6cea28b481c",
        "name": "",
        "group": "180ab5fa68364540",
        "order": 1,
        "width": 0,
        "height": 0,
        "gtype": "gage",
        "title": "gauge",
        "label": "units",
        "format": "{{value}}",
        "min": 0,
        "max": "100",
        "colors": [
            "#ff0000",
            "#e6e600",
            "#00ff00"
        ],
        "seg1": "",
        "seg2": "",
        "className": "",
        "x": 660,
        "y": 340,
        "wires": []
    },
    {
        "id": "ff6aa2f8163f356c",
        "type": "ui_button",
        "z": "9aace6cea28b481c",
        "name": "Start/stop",
        "group": "180ab5fa68364540",
        "order": 3,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "{{msg.topic}}",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "active",
        "payloadType": "flow",
        "topic": "topic",
        "topicType": "msg",
        "x": 620,
        "y": 460,
        "wires": [
            [
                "a8faaa345e6c693e"
            ]
        ]
    },
    {
        "id": "def91a73842b9394",
        "type": "change",
        "z": "9aace6cea28b481c",
        "name": "stop",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "Start",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "active",
                "pt": "flow",
                "to": "false",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 730,
        "y": 600,
        "wires": [
            [
                "ff6aa2f8163f356c"
            ]
        ]
    },
    {
        "id": "a8faaa345e6c693e",
        "type": "switch",
        "z": "9aace6cea28b481c",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "true"
            },
            {
                "t": "false"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 610,
        "y": 620,
        "wires": [
            [
                "def91a73842b9394"
            ],
            [
                "0355040b45d4d788"
            ]
        ]
    },
    {
        "id": "89512f847c49aebd",
        "type": "inject",
        "z": "9aace6cea28b481c",
        "name": "set label",
        "props": [
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "Stop",
        "x": 160,
        "y": 460,
        "wires": [
            [
                "c1981fffa939b67a"
            ]
        ]
    },
    {
        "id": "0355040b45d4d788",
        "type": "change",
        "z": "9aace6cea28b481c",
        "name": "start",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "Stop",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "active",
                "pt": "flow",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 730,
        "y": 640,
        "wires": [
            [
                "ff6aa2f8163f356c"
            ]
        ]
    },
    {
        "id": "c1981fffa939b67a",
        "type": "change",
        "z": "9aace6cea28b481c",
        "name": "set state of button",
        "rules": [
            {
                "t": "set",
                "p": "active",
                "pt": "flow",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 390,
        "y": 460,
        "wires": [
            [
                "ff6aa2f8163f356c"
            ]
        ]
    },
    {
        "id": "180ab5fa68364540",
        "type": "ui_group",
        "name": "Dashboard Demo",
        "tab": "1fb3b04341d0523d",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "1fb3b04341d0523d",
        "type": "ui_tab",
        "name": "Home",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]
1 Like

Thank you for quick response. I will try to understand and check it. But it will take some time...

Same:
Thank you for quick response. I will try to understand and check it. But it will take some time...

You are welcome. One thing to bear in mind is that there are many ways in node-red to achieve the same result and there is no best/right way but simplicity is generally the best way forward.

1 Like

Thank you very much for your quick response and your example. It did directly work and it is a solution for what I was looking for: Disable "something" on the dashboard using a button on the dashboard. Now I am trying to understand the details...

Don't hesitate to ask additional questions about it if you don't understand something about it. :wink:

1 Like

I would use a global/flow variable as a filter, then a switch node that checks its value to redirect or stop the messages.

Ok, this is what I understand:

  • node inject 'set label': Setting the first value of the label of the Start/stop button, starting with 'Stop' -> everything shall be active in initial state

  • node change 'set state of button': This confuses me. In this node the flow is somehow set to active. Why is it named 'set state of button'?

  • node ui_button 'Start/stop': Is labeled using {{msg.topic}}, is sending payload (flow.active?) and msg.topic (the current value of the label)

  • node switch 'switch': is forwarding 'mgs.payload=true' to output 1 and 'msg.payload=false' to output 2

  • node change 'stop': connected to to output1, forwarding msg.topic=Start to node ui_button 'Start/stop' and setting flow.active=false

  • node change 'start': just the other...

Question: What is the difference between msg.topc and msg.payload?

Can you post your flow to help anchor a reply ?

msg is a data structure. So msg.topic and msg.payload are two different keys to two pieces of data. The data in either case could be a simple type like string or boolean but they could also be keys into another data structure.

Sorry just realised which flow you are analysing. I give way to @Razio for an explanation.

1 Like

We save the state of the generator in a variable. Because we want to use the variable in other places in the flow we use the flowcontext, so we can extract the value saved in the variable in other nodes in the flow. The term 'active' is a self chosen term. This can be whatever you want it to be.
https://nodered.org/docs/creating-nodes/context

The name of the change button is indeed wrongly chosen. I changed that. In this node we put the value true in the flow.active variable to indicate that the initial state of the generator is active.

  • Because we want to change the label of the button when it is clicked, we put the value of something in a msg or variable as label. Here I went for putting the labeltext in msg.topic.

  • When the button is clicked we put the value of the variable in the msg.payload.

  • The switch node is diverting the msg to either the first output or the second depending msg.payload.

  • if the generator is running (flow.active=true) the moment we click. Then we change the label from stop to start. As we use msg.topic for our labeltext we have to set msg.topic to start and send it back to our button. And the other way around for when we click the button when it is not running.

  • In the fire-inject button we put the flow.active value in the msg.topic of those messages. So every time we press the button, the changed value of flow.active is passed along in the msg.topic.

  • In your function we do a check to see if the generator should run or not by checking the value passed along in msg.topic.

Here you have the flow slightly altered where the value of flow.active is extracted in your function instead of passed along in the msg.topic in your inject. Other way, same result.

[
    {
        "id": "cc5776f80f3c2057",
        "type": "inject",
        "z": "9aace6cea28b481c",
        "name": "Fire every second",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "1",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "date",
        "x": 130,
        "y": 200,
        "wires": [
            [
                "d37757ff7a3c7709"
            ]
        ]
    },
    {
        "id": "d37757ff7a3c7709",
        "type": "function",
        "z": "9aace6cea28b481c",
        "name": "Generate random number",
        "func": "var isActive = flow.get('active');\nif(isActive){\n    msg.payload = Math.round(Math.random()*100);\n    return msg;\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 380,
        "y": 200,
        "wires": [
            [
                "dabb6428037d7bb8",
                "21bc70e9f271307e"
            ]
        ]
    },
    {
        "id": "dabb6428037d7bb8",
        "type": "ui_text",
        "z": "9aace6cea28b481c",
        "group": "180ab5fa68364540",
        "order": 2,
        "width": 0,
        "height": 0,
        "name": "Data output",
        "label": "text",
        "format": "{{msg.payload}}",
        "layout": "row-spread",
        "className": "",
        "x": 650,
        "y": 200,
        "wires": []
    },
    {
        "id": "21bc70e9f271307e",
        "type": "ui_gauge",
        "z": "9aace6cea28b481c",
        "name": "",
        "group": "180ab5fa68364540",
        "order": 1,
        "width": 0,
        "height": 0,
        "gtype": "gage",
        "title": "gauge",
        "label": "units",
        "format": "{{value}}",
        "min": 0,
        "max": "100",
        "colors": [
            "#ff0000",
            "#e6e600",
            "#00ff00"
        ],
        "seg1": "",
        "seg2": "",
        "className": "",
        "x": 660,
        "y": 340,
        "wires": []
    },
    {
        "id": "ff6aa2f8163f356c",
        "type": "ui_button",
        "z": "9aace6cea28b481c",
        "name": "Start/stop",
        "group": "180ab5fa68364540",
        "order": 3,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "{{msg.topic}}",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "active",
        "payloadType": "flow",
        "topic": "topic",
        "topicType": "msg",
        "x": 620,
        "y": 460,
        "wires": [
            [
                "a8faaa345e6c693e"
            ]
        ]
    },
    {
        "id": "def91a73842b9394",
        "type": "change",
        "z": "9aace6cea28b481c",
        "name": "stop",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "Start",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "active",
                "pt": "flow",
                "to": "false",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 730,
        "y": 600,
        "wires": [
            [
                "ff6aa2f8163f356c"
            ]
        ]
    },
    {
        "id": "a8faaa345e6c693e",
        "type": "switch",
        "z": "9aace6cea28b481c",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "true"
            },
            {
                "t": "false"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 610,
        "y": 620,
        "wires": [
            [
                "def91a73842b9394"
            ],
            [
                "0355040b45d4d788"
            ]
        ]
    },
    {
        "id": "89512f847c49aebd",
        "type": "inject",
        "z": "9aace6cea28b481c",
        "name": "set label",
        "props": [
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "Stop",
        "x": 160,
        "y": 460,
        "wires": [
            [
                "c1981fffa939b67a"
            ]
        ]
    },
    {
        "id": "0355040b45d4d788",
        "type": "change",
        "z": "9aace6cea28b481c",
        "name": "start",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "Stop",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "active",
                "pt": "flow",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 730,
        "y": 640,
        "wires": [
            [
                "ff6aa2f8163f356c"
            ]
        ]
    },
    {
        "id": "c1981fffa939b67a",
        "type": "change",
        "z": "9aace6cea28b481c",
        "name": "initial state generator",
        "rules": [
            {
                "t": "set",
                "p": "active",
                "pt": "flow",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 400,
        "y": 460,
        "wires": [
            [
                "ff6aa2f8163f356c"
            ]
        ]
    },
    {
        "id": "180ab5fa68364540",
        "type": "ui_group",
        "name": "Dashboard Demo",
        "tab": "1fb3b04341d0523d",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "1fb3b04341d0523d",
        "type": "ui_tab",
        "name": "Home",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

A msg is an object which can contain different values in different sub-namespaces. You can add any sub-namespace to your msg you want. There are some standard used sub-namespaces that are used within node-red. msg.payload is the most used one.
https://nodered.org/docs/user-guide/messages

Hope this help a bit.

1 Like

@Razio this helps a lot!
Thank you very much for your help!

For the benefit of anyone finding this discussion in the future, a fairly simple way to deal with the original problem would be to use the node-red-contrib-simple-gate node. The second example in that node's Library subfolder shows how to control the gate from the dashboard. This approach has the advantage that no coding or familiarity with context variables is needed. Obviously, the disadvantages include having to download a contributed node as well as not having access to the flow variable defining the state of the gate, although that information is available in the node status.

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