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

**URL:** <https://discourse.nodered.org/t/how-to-forbid-to-user-on-off-the-ui-switch-if-the-variable-is-offline/33305>\
**Category:** General\
**Created:** [24 September 2020 17:47 UTC](https://discourse.nodered.org/t/how-to-forbid-to-user-on-off-the-ui-switch-if-the-variable-is-offline/33305 "2020-09-24T17:47:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zerg](https://avatars.discourse-cdn.com/v4/letter/z/6de8d8/32.png) [@Zerg](https://discourse.nodered.org/u/Zerg)\
**Post date:** [24 September 2020 17:47 UTC](https://discourse.nodered.org/t/how-to-forbid-to-user-on-off-the-ui-switch-if-the-variable-is-offline/33305/1 "2020-09-24T17:47:26Z")

</div>

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?

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

```

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [24 September 2020 18:17 UTC](https://discourse.nodered.org/t/how-to-forbid-to-user-on-off-the-ui-switch-if-the-variable-is-offline/33305/2 "2020-09-24T18:17:44Z")

</div>

> [@Zerg](#):
>
> then the user has no possibility to switch "ON"/"OFF" the "ui\_switch".

From the switch node help:

```auto
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).

---

<div class="post-metadata">

**Author:** ![Zerg](https://avatars.discourse-cdn.com/v4/letter/z/6de8d8/32.png) [@Zerg](https://discourse.nodered.org/u/Zerg)\
**Post date:** [24 September 2020 20:04 UTC](https://discourse.nodered.org/t/how-to-forbid-to-user-on-off-the-ui-switch-if-the-variable-is-offline/33305/3 "2020-09-24T20:04:00Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [24 September 2020 20:17 UTC](https://discourse.nodered.org/t/how-to-forbid-to-user-on-off-the-ui-switch-if-the-variable-is-offline/33305/4 "2020-09-24T20:17:35Z")

</div>

> [@Zerg](#):
>
> I have no idea where I should insert this?

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

![a-01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/2/82256fcfd2c361583c7841e77eb5d670d271be46.jpeg)

Flow:

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

```

---

<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:** [8 October 2020 20:17 UTC](https://discourse.nodered.org/t/how-to-forbid-to-user-on-off-the-ui-switch-if-the-variable-is-offline/33305/5 "2020-10-08T20:17:50Z")

</div>

Closed on 8 October 2020 20:17 UTC

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