# My switch node does not what I expect

**URL:** https://discourse.nodered.org/t/my-switch-node-does-not-what-i-expect/95705
**Category:** General
**Created:** [28 February 2025 10:23 UTC](https://discourse.nodered.org/t/my-switch-node-does-not-what-i-expect/95705 "2025-02-28T10:23:18Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![micha53](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/micha53/32/97662_2.png) [@micha53](https://discourse.nodered.org/u/micha53)
#### Post date: [28 February 2025 10:23 UTC](https://discourse.nodered.org/t/my-switch-node-does-not-what-i-expect/95705/1 "2025-02-28T10:23:18Z")

</div>

I have the following simple flow and I would expect that my signal passes output 1 of the switch node when my payload of "Plugged" is 1.

```auto
[{"id":"0b7e0b7d175bbb22","type":"debug","z":"7f3f8d551243a1af","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":760,"y":180,"wires":[]},{"id":"70985d276f19ff97","type":"inject","z":"7f3f8d551243a1af","name":"Plugged 0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"wican/7c2c67b3dbc1/auto/plugged","payload":"{\"Plugged\":0}","payloadType":"json","x":120,"y":220,"wires":[["aaafffd89118fb89"]]},{"id":"64112c84e991c110","type":"inject","z":"7f3f8d551243a1af","name":"Plugged 1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"wican/7c2c67b3dbc1/auto/plugged","payload":"{\"Plugged\":1}","payloadType":"json","x":120,"y":260,"wires":[["aaafffd89118fb89"]]},{"id":"aaafffd89118fb89","type":"split","z":"7f3f8d551243a1af","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"key","property":"payload","x":330,"y":180,"wires":[["89cf236158ce97e3","0b22ed02a6139db1"]]},{"id":"89cf236158ce97e3","type":"switch","z":"7f3f8d551243a1af","name":"plugged","property":"payload.Plugged","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":520,"y":180,"wires":[["0b7e0b7d175bbb22"],["6b3895cde63e7855"]]},{"id":"0b22ed02a6139db1","type":"debug","z":"7f3f8d551243a1af","name":"debug 16","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":520,"y":260,"wires":[]},{"id":"6b3895cde63e7855","type":"debug","z":"7f3f8d551243a1af","name":"debug 20","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":760,"y":220,"wires":[]}]

```

But it does not and comes out of output 2, regardless of the value. Can someone help me out here?

 ![2025-02-28 11_25_18-](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/2/52b1d7e718fb7bdf9973c3024015a337e06af22b.png)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [28 February 2025 10:28 UTC](https://discourse.nodered.org/t/my-switch-node-does-not-what-i-expect/95705/2 "2025-02-28T10:28:54Z")

</div>

because you have a split node that is operating on an object, the value is set in `payload` (that is evident by debug node output after the split)

your switch should be checking `payload` not `payload.Plugged` - thats why it does NOT find a `1` in `payload.Plugged` because the split moved the `Plugged` value to `payload` and the key name to `msg.key` (set the debug attached to split node to "show FULL msg object")

Why do you have the split node anyhow? what are you actually trying to achieve?

---

<div class="post-metadata">

### Author: ![micha53](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/micha53/32/97662_2.png) [@micha53](https://discourse.nodered.org/u/micha53)
#### Post date: [28 February 2025 10:54 UTC](https://discourse.nodered.org/t/my-switch-node-does-not-what-i-expect/95705/3 "2025-02-28T10:54:00Z")

</div>

> [@Steve-Mcl](#):
>
> Why do you have the split node anyhow? what are you actually trying to achieve?

It will be part of something more, but at first I wanted to solve this problem. What you wrote, this works.  
I want to release a log flow into a text file when "Plugged" is 1 and stop it when it is 0, but still I have no idea how to realize this. The log flow is simple line by line text. How can I combine my Plugged-Signal with the log flow to control it.  
I see, the split node is not necessary, this was legacy.  
Iam searching for a function instead of the comment/debug node.

```auto
[{"id":"f3d2023a47cd6b86","type":"file","z":"7f3f8d551243a1af","name":"WB-log.csv","filename":"/mnt/nas/homeassistant/Zoe-Daten/WB-log.csv","filenameType":"str","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"utf8","x":630,"y":180,"wires":[[]]},{"id":"0b7e0b7d175bbb22","type":"debug","z":"7f3f8d551243a1af","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":440,"y":180,"wires":[]},{"id":"128a2b5bda1761c2","type":"mqtt in","z":"7f3f8d551243a1af","name":"WiCAN Zoe","topic":"wican/7c2c67b3dbc1/auto/plugged","qos":"2","datatype":"json","broker":"5c944a233ddbd024","nl":false,"rap":true,"rh":0,"inputs":0,"x":130,"y":260,"wires":[["89cf236158ce97e3"]]},{"id":"70985d276f19ff97","type":"inject","z":"7f3f8d551243a1af","name":"Plugged 0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"wican/7c2c67b3dbc1/auto/plugged","payload":"{\"Plugged\":0}","payloadType":"json","x":120,"y":300,"wires":[["89cf236158ce97e3"]]},{"id":"64112c84e991c110","type":"inject","z":"7f3f8d551243a1af","name":"Plugged 1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"wican/7c2c67b3dbc1/auto/plugged","payload":"{\"Plugged\":1}","payloadType":"json","x":120,"y":340,"wires":[["89cf236158ce97e3"]]},{"id":"89cf236158ce97e3","type":"switch","z":"7f3f8d551243a1af","name":"plugged","property":"payload.Plugged","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":340,"y":260,"wires":[["0b7e0b7d175bbb22"],[]]},{"id":"206552ff0bc3f298","type":"inject","z":"7f3f8d551243a1af","name":"Log","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cfos_mqtt/log","payload":"blablabla","payloadType":"str","x":110,"y":180,"wires":[["f3d2023a47cd6b86"]]},{"id":"ebb729a3b5ed2090","type":"comment","z":"7f3f8d551243a1af","name":"","info":"","x":440,"y":180,"wires":[]},{"id":"5c944a233ddbd024","type":"mqtt-broker","name":"Raspi3","broker":"192.168.178.2","port":1883,"clientid":"","autoConnect":true,"usetls":false,"protocolVersion":4,"keepalive":60,"cleansession":true,"autoUnsubscribe":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [28 February 2025 11:28 UTC](https://discourse.nodered.org/t/my-switch-node-does-not-what-i-expect/95705/4 "2025-02-28T11:28:58Z")

</div>

I assume since you named the file \*.csv, you want the object in the mqtt payload to be converted into well formatted CSV row data. In which case, I would suggest adding a timestamp, push it through a CSV node, then pipe that into the file.

e.g:

```auto
[{"id":"f3d2023a47cd6b86","type":"file","z":"6e5ea1e6617b5df3","name":"WB-log.csv","filename":"/mnt/nas/homeassistant/Zoe-Daten/WB-log.csv","filenameType":"str","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"utf8","x":1010,"y":440,"wires":[[]]},{"id":"70985d276f19ff97","type":"inject","z":"6e5ea1e6617b5df3","name":"Plugged 0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"wican/7c2c67b3dbc1/auto/plugged","payload":"{\"Plugged\":0}","payloadType":"json","x":300,"y":480,"wires":[["89cf236158ce97e3"]]},{"id":"64112c84e991c110","type":"inject","z":"6e5ea1e6617b5df3","name":"Plugged 1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"wican/7c2c67b3dbc1/auto/plugged","payload":"{\"Plugged\":1}","payloadType":"json","x":300,"y":520,"wires":[["89cf236158ce97e3"]]},{"id":"89cf236158ce97e3","type":"switch","z":"6e5ea1e6617b5df3","name":"plugged","property":"payload.Plugged","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":520,"y":440,"wires":[["8d8b59b16522efa1"],[]]},{"id":"d98219ab6edb5d45","type":"csv","z":"6e5ea1e6617b5df3","name":"","spec":"rfc","sep":",","hdrin":"","hdrout":"once","multi":"one","ret":"\\r\\n","temp":"ts,Plugged","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":830,"y":440,"wires":[["f3d2023a47cd6b86","2478386691d1b30c"]]},{"id":"8d8b59b16522efa1","type":"change","z":"6e5ea1e6617b5df3","name":"add ts","rules":[{"t":"set","p":"payload.ts","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":440,"wires":[["d98219ab6edb5d45","6ee950baa9b75cee"]]},{"id":"6ee950baa9b75cee","type":"debug","z":"6e5ea1e6617b5df3","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":520,"wires":[]},{"id":"2478386691d1b30c","type":"debug","z":"6e5ea1e6617b5df3","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":910,"y":520,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![micha53](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/micha53/32/97662_2.png) [@micha53](https://discourse.nodered.org/u/micha53)
#### Post date: [28 February 2025 12:25 UTC](https://discourse.nodered.org/t/my-switch-node-does-not-what-i-expect/95705/5 "2025-02-28T12:25:02Z")

</div>

You got my legacy parts again, no, ist is a simple text file and contains a time stamp. I renamed it to WB.log. Here are some typical lines

```auto
2025-02-25T23:59:53.842Z LM MVAR current_SF_mA 10.000000
2025-02-25T23:59:53.846Z LM MVAR current_bat -317.548737
2025-02-25T23:59:53.851Z LM MVAR I_AC_CurrentB 38.000000
2025-02-25T23:59:53.854Z LM MVAR current_SF_mA 10.000000
2025-02-25T23:59:53.858Z LM MVAR current_bat -317.548737
2025-02-25T23:59:53.863Z LM MVAR I_AC_CurrentC 37.000000
2025-02-25T23:59:53.866Z LM MVAR current_SF_mA 10.000000

```

I simply want to suppress writing to the file when "Plugged" is 0. Otherwise I get too big files.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [28 February 2025 12:34 UTC](https://discourse.nodered.org/t/my-switch-node-does-not-what-i-expect/95705/6 "2025-02-28T12:34:03Z")

</div>

> [@micha53](#):
>
> I simply want to suppress writing to the file when "Plugged" is 0

which is what the switch node does. Kinda. It only permits a msg to pass when `payload.Plugged == 1`

* * *

to avoid further confusion, rathr than little examples of code or pseudo examples, try explaining exactly what you want to achieve. for example, the values in that "typical log" were never stated before now and it is unclear from previous messages anything other than a msg with `payload.Plugged == 1` should be logged,

---

<div class="post-metadata">

### Author: ![micha53](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/micha53/32/97662_2.png) [@micha53](https://discourse.nodered.org/u/micha53)
#### Post date: [28 February 2025 13:53 UTC](https://discourse.nodered.org/t/my-switch-node-does-not-what-i-expect/95705/7 "2025-02-28T13:53:00Z")

</div>

I simply want to suppress writing the data from Log to the file wb.log when "Plugged" is 0 and enable it when "Plugged" is 1.

```auto
[{"id":"f3d2023a47cd6b86","type":"file","z":"7f3f8d551243a1af","name":"WB.log","filename":"/mnt/nas/homeassistant/Zoe-Daten/WB.log","filenameType":"str","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"utf8","x":620,"y":180,"wires":[[]]},{"id":"0b7e0b7d175bbb22","type":"debug","z":"7f3f8d551243a1af","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":260,"wires":[]},{"id":"70985d276f19ff97","type":"inject","z":"7f3f8d551243a1af","name":"Plugged 0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"wican/7c2c67b3dbc1/auto/plugged","payload":"{\"Plugged\":0}","payloadType":"json","x":120,"y":300,"wires":[["89cf236158ce97e3"]]},{"id":"64112c84e991c110","type":"inject","z":"7f3f8d551243a1af","name":"Plugged 1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"wican/7c2c67b3dbc1/auto/plugged","payload":"{\"Plugged\":1}","payloadType":"json","x":120,"y":340,"wires":[["89cf236158ce97e3"]]},{"id":"89cf236158ce97e3","type":"switch","z":"7f3f8d551243a1af","name":"plugged","property":"payload.Plugged","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":340,"y":260,"wires":[["0b7e0b7d175bbb22"],[]]},{"id":"206552ff0bc3f298","type":"inject","z":"7f3f8d551243a1af","name":"Log","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cfos_mqtt/log","payload":"blablabla","payloadType":"str","x":110,"y":180,"wires":[["f3d2023a47cd6b86"]]}]

```

Maybe the solution is a function node but I am absolutely not familiar with JavaScript.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [28 February 2025 14:53 UTC](https://discourse.nodered.org/t/my-switch-node-does-not-what-i-expect/95705/8 "2025-02-28T14:53:33Z")

</div>

> [@micha53](#):
>
> I simply want to suppress writing the data from Log to the file wb.log when "Plugged" is 0 and enable it when "Plugged" is 1.

Since that is exactly what the switch does in my example and it doesnt "work" for you, I _think_ there is more to this than what you say.

Reading between the lines, you potentially need something like "whenever Plugged == 1, allow logging (regardless of where it comes from) to the CSV file"?

If yes, then you would simply store the value of `Plugged` in a context variable then use a switch to see if the value of the context variable is `1` and permit the logging to occur.

e.g.  
 ![chrome_xGuFESkkGv](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/b/eb6a32b43a310852cc08c31faeee26ce7fe5a554.gif)

```auto
[{"id":"70985d276f19ff97","type":"inject","z":"2f29e2966e4a81bb","name":"Plugged 0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"wican/7c2c67b3dbc1/auto/plugged","payload":"{\"Plugged\":0}","payloadType":"json","x":340,"y":2060,"wires":[["a6fefd1bea0a191c"]]},{"id":"64112c84e991c110","type":"inject","z":"2f29e2966e4a81bb","name":"Plugged 1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"wican/7c2c67b3dbc1/auto/plugged","payload":"{\"Plugged\":1}","payloadType":"json","x":340,"y":2100,"wires":[["a6fefd1bea0a191c"]]},{"id":"206552ff0bc3f298","type":"inject","z":"2f29e2966e4a81bb","name":"Log","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cfos_mqtt/log","payload":"","payloadType":"date","x":330,"y":2000,"wires":[["853293658630990d"]]},{"id":"a6fefd1bea0a191c","type":"change","z":"2f29e2966e4a81bb","name":"","rules":[{"t":"set","p":"AllowLogs","pt":"global","to":"payload.Plugged","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":2080,"wires":[[]]},{"id":"853293658630990d","type":"switch","z":"2f29e2966e4a81bb","name":"AllowLogs == 1?","property":"AllowLogs","propertyType":"global","rules":[{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":550,"y":2000,"wires":[["fd4b95c4b3c2b3c4"]]},{"id":"fd4b95c4b3c2b3c4","type":"debug","z":"2f29e2966e4a81bb","name":"to log ->","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":750,"y":2000,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![micha53](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/micha53/32/97662_2.png) [@micha53](https://discourse.nodered.org/u/micha53)
#### Post date: [28 February 2025 15:50 UTC](https://discourse.nodered.org/t/my-switch-node-does-not-what-i-expect/95705/9 "2025-02-28T15:50:28Z")

</div>

Global vars... That's a good solution. I didn't know about them nor used them before.  
Thank you for your patience.

---

<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: [28 February 2025 16:15 UTC](https://discourse.nodered.org/t/my-switch-node-does-not-what-i-expect/95705/10 "2025-02-28T16:15:13Z")

</div>

Possibly more 'the node red way' would be to use a [node-red-contrib-simple-gate](https://flows.nodered.org/node/node-red-contrib-simple-gate) to block or allow the messages through, using the plugged value to control it. Then there are no global vars with all the problems that they can introduce.

---

<div class="post-metadata">

### Author: ![micha53](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/micha53/32/97662_2.png) [@micha53](https://discourse.nodered.org/u/micha53)
#### Post date: [15 March 2025 13:49 UTC](https://discourse.nodered.org/t/my-switch-node-does-not-what-i-expect/95705/11 "2025-03-15T13:49:16Z")

</div>

tried this solution too, also works good  
thank you

---

<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: [13 June 2025 13:49 UTC](https://discourse.nodered.org/t/my-switch-node-does-not-what-i-expect/95705/12 "2025-06-13T13:49:50Z")

</div>

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