How to "forbid" to user "ON"/"OFF" the "ui switch" if the variable is offline?

Could you advise me?
I get stuck, can't to find are solution, if the variable "connection_cl" is not online, then the user has no possibility to switch "ON"/"OFF" the "ui_switch".
I made such a crutch, but I don't like it!
Maybe there are better solutions?

[{"id":"55387c15.afb574","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"3b774d47.d77032","type":"ui_switch","z":"55387c15.afb574","name":"","label":"Color lamp","tooltip":"","group":"2a370ae2.8d43d6","order":3,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"{\"state\":\"ON\"}","onvalueType":"json","onicon":"fa-lightbulb-o fa-3x","oncolor":"red","offvalue":"{\"state\":\"OFF\"}","offvalueType":"json","officon":"fa-lightbulb-o fa-3x","offcolor":"gray","x":290,"y":200,"wires":[["b7eba7fd.5188a8","72187439.3ccc0c"]]},{"id":"72187439.3ccc0c","type":"function","z":"55387c15.afb574","name":"","func":"var connection_cl = global.get('connection_cl') || \"\";\nvar flag = flow.get('flag') || \"\";\n\nvar p = {};  \np.state=msg.payload.state;\nmsg.payload=p;\n\nif ( p.state === \"ON\" && flag !=\"ON\" && connection_cl != 'OFF') { \n    flow.set('flag',\"ON\");\n    return [ msg, null ];\n} \nelse if ( p.state === \"OFF\" && flag !=\"OFF\"  && connection_cl != 'OFF') {\n    flow.set('flag',\"OFF\");\n    return [ msg, null ];\n}\nelse if (p.state === \"OFF\"  && flag !=\"OFF\" && connection_cl != 'ON') {\n    flow.set('flag',\"OFF\");\n    msg.payload={\"state\":\"OFF\"};\n    return [ null, msg ];\n}\nelse if (p.state === \"ON\"  && flag !=\"ON\" && connection_cl != 'ON') {\n    flow.set('flag',\"OFF\");\n    msg.payload={\"state\":\"OFF\"};\n    return [ null, msg ];\n}\nelse {\n    return[ null, null ]; \n}\n","outputs":2,"noerr":0,"initialize":"","finalize":"","x":460,"y":140,"wires":[[],["3b774d47.d77032"]]},{"id":"b7eba7fd.5188a8","type":"debug","z":"55387c15.afb574","name":"4444444444","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":470,"y":240,"wires":[]},{"id":"d432c77.b95c938","type":"inject","z":"55387c15.afb574","name":"OFF","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"state\":\"OFF\"}","payloadType":"json","x":90,"y":220,"wires":[["3b774d47.d77032"]]},{"id":"3fb205c5.fa07da","type":"inject","z":"55387c15.afb574","name":"ON","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"state\":\"ON\"}","payloadType":"json","x":90,"y":160,"wires":[["3b774d47.d77032"]]},{"id":"2a370ae2.8d43d6","type":"ui_group","z":"","name":"Reboot","tab":"852f6d3.66c129","order":1,"disp":true,"width":"6","collapse":false},{"id":"852f6d3.66c129","type":"ui_tab","z":"","name":"SERVICE","icon":"fa-cog fa-2x","order":5,"disabled":false,"hidden":false}]

From the switch node help:

Setting msg.enabled to false will disable the switch widget.

You may use this feature to achieve the behaviour you want. You have to design your flow to always send a disable message to the switch node when your variable "connection_cl" is not online (for whatever that means).

1 Like

I have no idea where I should insert this?
Maybe i need to add ""msg.enabled":true," in the"inject" node something like this?

{
    "update_available": true,
    "msg.enabled":true,
    "status": "ON",
    "linkquality": 76,
    "brightness": 254,
    "color_temp": 254,
    "color": {
        "x": 0.4599,
        "y": 0.4106
    }
}
```**strong text**

You want to enable / disable the dashboard switch, so the msg.enabled property should be given to the concerned switch node.

a-01

Flow:

[{"id":"c22901ee.cddb8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"4c18cc5b.2438a4","type":"ui_switch","z":"c22901ee.cddb8","name":"","label":"switch","tooltip":"","group":"298a6214.c57fde","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":590,"y":280,"wires":[[]]},{"id":"b2723bce.8e2718","type":"change","z":"c22901ee.cddb8","name":"Enable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":280,"wires":[["4c18cc5b.2438a4"]]},{"id":"4dc147c7.dfb608","type":"change","z":"c22901ee.cddb8","name":"Disable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":340,"wires":[["4c18cc5b.2438a4"]]},{"id":"90d39172.e8eef","type":"inject","z":"c22901ee.cddb8","name":"Enable","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":280,"wires":[["b2723bce.8e2718"]]},{"id":"dea5ceb5.fced3","type":"inject","z":"c22901ee.cddb8","name":"Disable","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":340,"wires":[["4dc147c7.dfb608"]]},{"id":"298a6214.c57fde","type":"ui_group","z":"","name":"A1","tab":"cf795f11.f1c7b","order":1,"disp":true,"width":"6","collapse":false},{"id":"cf795f11.f1c7b","type":"ui_tab","z":"","name":"TEST","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

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