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.