# How to write code for a switch node?

**URL:** <https://discourse.nodered.org/t/how-to-write-code-for-a-switch-node/63737>\
**Category:** General\
**Tags:** node-red-dashboard, function-node\
**Created:** [10 June 2022 16:42 UTC](https://discourse.nodered.org/t/how-to-write-code-for-a-switch-node/63737 "2022-06-10T16:42:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JJ22](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@JJ22](https://discourse.nodered.org/u/JJ22)\
**Post date:** [10 June 2022 16:42 UTC](https://discourse.nodered.org/t/how-to-write-code-for-a-switch-node/63737/1 "2022-06-10T16:42:56Z")

</div>

Hello, everyone  
How to write code in function so that the switch node, for example, if "1" that is transistor, if "0" then there is no transistor? I've tried everything, nothing works.  
 ![ек](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/04f410286cf086aa7e318401b18f4f8181abf0f6.jpeg)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 June 2022 17:17 UTC](https://discourse.nodered.org/t/how-to-write-code-for-a-switch-node/63737/2 "2022-06-10T17:17:18Z")

</div>

Why not use the `switch` node?  
If you are talking about something els, then maybe explain yourself a bit more

---

<div class="post-metadata">

**Author:** ![JJ22](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@JJ22](https://discourse.nodered.org/u/JJ22)\
**Post date:** [10 June 2022 17:31 UTC](https://discourse.nodered.org/t/how-to-write-code-for-a-switch-node/63737/3 "2022-06-10T17:31:07Z")

</div>

I need to compile a node in the form of a PLC configurator. To use else in the code. The switch node I want to make a choice, if the user needs a transistor, he clicks on the checkmark, if not, then he doesn’t click anything

---

<div class="post-metadata">

**Author:** ![MecatronicaMADE](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@MecatronicaMADE](https://discourse.nodered.org/u/MecatronicaMADE)\
**Post date:** [10 June 2022 17:31 UTC](https://discourse.nodered.org/t/how-to-write-code-for-a-switch-node/63737/4 "2022-06-10T17:31:46Z")

</div>

There are two nodes with **"switch"** name:  
_1.- Dashboard node._  
_2.- Function node._  
as mentioned @zenofmud is easer use the swtich function node for your logic:

![Screenshot from 2022-06-10 12-26-04](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/2/8237409117f09e049dff33bd9774d4df8f7785e3.png)  
here is the JSON of this example:

```auto
[{"id":"6e43f45829fbf3e7","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"59c979d4b0849d9c","type":"switch","z":"6e43f45829fbf3e7","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":550,"y":260,"wires":[["fd98bbff3c2ff4cf"],["34207bf4c4149d73"]]},{"id":"618f38e6c8a9e918","type":"ui_switch","z":"6e43f45829fbf3e7","name":"","label":"switch","tooltip":"","group":"feee56445f470f84","order":1,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"1","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","animate":false,"className":"","x":410,"y":260,"wires":[["59c979d4b0849d9c"]]},{"id":"fd98bbff3c2ff4cf","type":"debug","z":"6e43f45829fbf3e7","name":"is transistor","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":240,"wires":[]},{"id":"34207bf4c4149d73","type":"debug","z":"6e43f45829fbf3e7","name":"is no transistor","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":720,"y":280,"wires":[]},{"id":"feee56445f470f84","type":"ui_group","name":"Default","tab":"0ed4eeb8e062e528","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"0ed4eeb8e062e528","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

```

I hope its helps you!!

---

<div class="post-metadata">

**Author:** ![JJ22](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@JJ22](https://discourse.nodered.org/u/JJ22)\
**Post date:** [10 June 2022 17:44 UTC](https://discourse.nodered.org/t/how-to-write-code-for-a-switch-node/63737/5 "2022-06-10T17:44:24Z")

</div>

Thank you very much!

---

<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:** [9 August 2022 17:44 UTC](https://discourse.nodered.org/t/how-to-write-code-for-a-switch-node/63737/6 "2022-08-09T17:44:27Z")

</div>

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