Understanding dashboard switch

Good day

So I want to integrate a switch onto my dashboard which will allow a series of pulse through. But that is not happening, am I misunderstanding how the switch node should work in that configuration?

Many Thanks

1 Like

Yes, you are misunderstanding. All the Pass Through checkbox does, when selected, is that if an input 0 or 1 is received then it will not only set the switch off or on but will also pass on that message. With it unchecked then an input message will set the switch but will not pass anything on. Unchecked is the usual setting because otherwise it is easy to get into a loop where pressing the switch sends a message, which for example goes off to MQTT, but if the input of the switch is connected to an MQTT In node subscribed to the same topic then the message will come back from MQTT into the front of the switch. If Pass Through is selected then the message will get passed on, into MQTT, and back into the front of the switch, through the switch and into MQTT again .....

1 Like

Ooooooh okay I see, any recommendations on how I can achieve such functionality then?

A switch on the UI, which when I turn on, allows payload values through and when I turn off, does not allow payload values through?

Many Thanks

Let the switch be the controller of the gate

[{"id":"6f6a991e.4a28d8","type":"function","z":"81c5c63b.6b8288","name":"gate","func":"// read (or initialize) gate state\nvar gateIsOpen = context.get(\"gateIsOpen\") || false;\n\n//message is from switch\nif(msg.topic === \"gate\"){\n gateIsOpen = msg.payload;\n // store the gate state \n context.set(\"gateIsOpen\",gateIsOpen);\n}\n\n// message is from other sources\nelse{\n if(gateIsOpen === true){\n node.status({fill:\"green\",shape:\"ring\",text:\"gate is open\"});\n // send through the original message\n return msg;\n }\n else{\n //just display the status of gate\n node.status({fill:\"red\",shape:\"ring\",text:\"gate is closed\"});\n }\n}\n\n","outputs":1,"noerr":0,"x":650,"y":580,"wires":[["d28937dd.8d5a98"]]},{"id":"58196f37.a5047","type":"ui_switch","z":"81c5c63b.6b8288","name":"","label":"switch","group":"df7325b1.2ce318","order":0,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"gate","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":430,"y":500,"wires":[["6f6a991e.4a28d8"]]},{"id":"e67fead5.6a33c8","type":"inject","z":"81c5c63b.6b8288","name":"","topic":"init","payload":"This is data","payloadType":"str","repeat":"2","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":580,"wires":[["6f6a991e.4a28d8"]]},{"id":"d28937dd.8d5a98","type":"debug","z":"81c5c63b.6b8288","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":850,"y":580,"wires":[]},{"id":"2f34865f.5350aa","type":"comment","z":"81c5c63b.6b8288","name":"Switch controls gate","info":"Switch sends boolean value to gate.\nmsg has topic \"gate\". This way the gate can understand \nthat this message is meant for gate to change it's state ","x":390,"y":440,"wires":[]},{"id":"a1928b73.28be98","type":"comment","z":"81c5c63b.6b8288","name":"Gate ","info":"Gate can be even OPEN or CLOSED (boolean value)\nAccording to the current state the incoming data is sent through or not","x":650,"y":520,"wires":[]},{"id":"df7325b1.2ce318","type":"ui_group","z":"","name":"Default","tab":"245a6127.50b27e","disp":true,"width":"6","collapse":false},{"id":"245a6127.50b27e","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]

2 Likes

Several examples on the flows.nodered.org site also...
including https://flows.nodered.org/flow/a985944e789d90e0cd2008d85b7aaa65

1 Like

Looks pretty annoying to use the switch in a function just for this. (existing the option passthrough)

I suppose most users expects msg.payload to just pass through.

I did workaround with two change nodes (one to set msg.payload to msg.data before and other to set msg.data to msg.payload at end.

Is the location of plugin dashboard available to report this suggestion? Even the status of a switch is not that well refreshed on flow

Here the solution flow:

[
    {
        "id": "532dafe02cfc00c6",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "dee7d6cf4616634e",
        "type": "ui_switch",
        "z": "532dafe02cfc00c6",
        "name": "",
        "label": "entrada real",
        "tooltip": "",
        "group": "21cfa744.d5afe8",
        "order": 3,
        "width": 0,
        "height": 0,
        "passthru": true,
        "decouple": "false",
        "topic": "payload",
        "topicType": "msg",
        "style": "",
        "onvalue": "true",
        "onvalueType": "bool",
        "onicon": "",
        "oncolor": "",
        "offvalue": "false",
        "offvalueType": "bool",
        "officon": "",
        "offcolor": "",
        "animate": false,
        "className": "",
        "x": 450,
        "y": 140,
        "wires": [
            [
                "a7f840103c9933b5"
            ]
        ]
    },
    {
        "id": "ba6d0167c824b723",
        "type": "change",
        "z": "532dafe02cfc00c6",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "data",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 210,
        "y": 140,
        "wires": [
            [
                "dee7d6cf4616634e"
            ]
        ]
    },
    {
        "id": "a7f840103c9933b5",
        "type": "change",
        "z": "532dafe02cfc00c6",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "data",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 700,
        "y": 140,
        "wires": [
            []
        ]
    },
    {
        "id": "21cfa744.d5afe8",
        "type": "ui_group",
        "name": "Entrada",
        "tab": "13fe9b85.e7c214",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "13fe9b85.e7c214",
        "type": "ui_tab",
        "name": "SimulaciĂłn",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]