# How to control Ui elements (e.g. Switch Node) by code (Function Node)?

**URL:** <https://discourse.nodered.org/t/how-to-control-ui-elements-e-g-switch-node-by-code-function-node/15743>\
**Category:** General\
**Created:** [21 September 2019 07:50 UTC](https://discourse.nodered.org/t/how-to-control-ui-elements-e-g-switch-node-by-code-function-node/15743 "2019-09-21T07:50:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)\
**Post date:** [21 September 2019 07:50 UTC](https://discourse.nodered.org/t/how-to-control-ui-elements-e-g-switch-node-by-code-function-node/15743/1 "2019-09-21T07:50:55Z")

</div>

Hey ho ppl,  
I try to switch the state of a switch node (restore status after reboot) by using a function node  
but failed. I ve no Idea what the switch awaits for a msg object to only switch the state.  
These are my switch settings: [click](http://puu.sh/EjEI9/f4ce10af21.png)  
thx 🙂

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [21 September 2019 08:10 UTC](https://discourse.nodered.org/t/how-to-control-ui-elements-e-g-switch-node-by-code-function-node/15743/2 "2019-09-21T08:10:21Z")

</div>

You have to send `msg.payload` with a value of `on`

You can use an inject node and check the checkbox to inject it once (this will also be triggered when you deploy the flows).

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 September 2019 08:19 UTC](https://discourse.nodered.org/t/how-to-control-ui-elements-e-g-switch-node-by-code-function-node/15743/3 "2019-09-21T08:19:59Z")

</div>

Actually I think you have to send exactly the string you have specified for on payload or off payload.

---

<div class="post-metadata">

**Author:** ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)\
**Post date:** [21 September 2019 08:22 UTC](https://discourse.nodered.org/t/how-to-control-ui-elements-e-g-switch-node-by-code-function-node/15743/4 "2019-09-21T08:22:41Z")

</div>

Yes but I only want to set the switch state without sending the string. Sending the string will cause an action which I want avoid.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 September 2019 09:08 UTC](https://discourse.nodered.org/t/how-to-control-ui-elements-e-g-switch-node-by-code-function-node/15743/5 "2019-09-21T09:08:48Z")

</div>

In that case clear the box that says to pass the message on if it matches.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 September 2019 08:53 UTC](https://discourse.nodered.org/t/how-to-control-ui-elements-e-g-switch-node-by-code-function-node/15743/6 "2019-09-24T08:53:51Z")

</div>

@WhiteLion Did that help?

---

<div class="post-metadata">

**Author:** ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)\
**Post date:** [2 October 2019 21:57 UTC](https://discourse.nodered.org/t/how-to-control-ui-elements-e-g-switch-node-by-code-function-node/15743/7 "2019-10-02T21:57:09Z")

</div>

No, I made a more ore less complicated solution using function nodes with multiple outputs and setted the switch to true / false.  
[click](http://puu.sh/Eo6Nv/30e9c48faf.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 October 2019 08:06 UTC](https://discourse.nodered.org/t/how-to-control-ui-elements-e-g-switch-node-by-code-function-node/15743/8 "2019-10-03T08:06:30Z")

</div>

So with that checkbox cleared it still passed on an input message to the output?

---

<div class="post-metadata">

**Author:** ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)\
**Post date:** [3 October 2019 12:48 UTC](https://discourse.nodered.org/t/how-to-control-ui-elements-e-g-switch-node-by-code-function-node/15743/9 "2019-10-03T12:48:32Z")

</div>

the checkbox of the switch is cleared too, thats right 🙂  
But for my case it was just a part of the solution. Like I said the flow is totaly reworked and the switch node has to be changed from string to bool.
