# SWITCH node not playing the game

**URL:** https://discourse.nodered.org/t/switch-node-not-playing-the-game/31751
**Category:** General
**Created:** [19 August 2020 23:51 UTC](https://discourse.nodered.org/t/switch-node-not-playing-the-game/31751 "2020-08-19T23:51:30Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [19 August 2020 23:51 UTC](https://discourse.nodered.org/t/switch-node-not-playing-the-game/31751/1 "2020-08-19T23:51:31Z")

</div>

I am (again) not seeing the bigger picture.

I am playing with `switch` nodes and it doesn't seem to be behaving.

 ![Screenshot from 2020-08-20 09-36-57](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/a/5a8f3a05d7a7e593f798cdd786e7c9d8c40c926b.png)

See nodes inside red boundary.

This is the code:

```auto
[{"id":"426ead5c.df267c","type":"switch","z":"a1911aa3.c45be8","name":"","property":"device","propertyType":"msg","rules":[{"t":"eq","v":"TABBYCAT","vt":"str"},{"t":"eq","v":"S5","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":550,"y":1760,"wires":[["715936e0.e7d808","1fcc1e18.2ec63a"],[],[]]},{"id":"a3d05ad9.c7cb3","type":"debug","z":"a1911aa3.c45be8","name":"Power message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":810,"y":1680,"wires":[]},{"id":"715936e0.e7d808","type":"fan","z":"a1911aa3.c45be8","name":"","x":690,"y":1730,"wires":[["a3d05ad9.c7cb3","9507ba32.ac3c38"]]},{"id":"e51e76d4.12bf08","type":"function","z":"a1911aa3.c45be8","name":"","func":"msg.device = msg.payload.device;\nmsg.payload = msg.payload.payload\n\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":1760,"wires":[["426ead5c.df267c","edba70d2.09daa","c1e957f9.54b4a"]]},{"id":"edba70d2.09daa","type":"change","z":"a1911aa3.c45be8","name":"Set device","rules":[{"t":"set","p":"device","pt":"flow","to":"device","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":820,"y":1640,"wires":[[]]},{"id":"9507ba32.ac3c38","type":"function","z":"a1911aa3.c45be8","name":"Set LED","func":"var device = flow.get(\"device\")\nif (device == \"TABBYCAT\")\n{\n //\n msg.led = 2;\n} else\nif (device == \"S5\")\n{\n //\n msg.led = 3;\n} else\nif (device == \"Marvin\")\n{\n //\n msg.led = 4;\n}\nreturn msg;","outputs":1,"noerr":0,"x":840,"y":1730,"wires":[["f6034dc3.774ac"]]},{"id":"1fcc1e18.2ec63a","type":"trigger","z":"a1911aa3.c45be8","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"5","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":720,"y":1810,"wires":[[]]},{"id":"c1e957f9.54b4a","type":"debug","z":"a1911aa3.c45be8","name":"LOOK AT ME","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":490,"y":1820,"wires":[]}]

```

The problem is:  
I send messages to it and it seems to not be happy with the second one.

 ![Screenshot from 2020-08-20 09-41-06](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/a/9a58e9568d5d8d653614e53496a7e15744e44352.png)

I send in a message (LOOK AT ME) it goes through the `SWITCH` node and you see the (POWER MESSAGE).  
The `trigger` node is to show the message has been sent to that output.

Ok, I'll expand the message:

![Screenshot from 2020-08-20 09-41-14](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cdf0098edfca1d6ffa9e6a1c62c6d32268ecd478.png)

Now let's look at the `switch` node:

 ![Screenshot from 2020-08-20 09-46-06](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/067e1f84f308f173381480b37577b69fe3a29cac.png)

So, if `msg.device` == "TABBYCAT" it sends it to output 1.

Now, I send a second message:

 ![Screenshot from 2020-08-20 09-41-47](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/6/76a53432e5720446b3feca1282f36d02c7e6d2c2.png)

Yet this time it doesn't send the message to output 1.

# Dumb question: Why?

I know the shot was taken after the 5 second `trigger` node had expired.  
But you can see there are no subsequent messages like before.

I don't really like asking, but I have picked it apart as best I can resolve and I am not getting why it isn't working.

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [20 August 2020 00:04 UTC](https://discourse.nodered.org/t/switch-node-not-playing-the-game/31751/2 "2020-08-20T00:04:05Z")

</div>

In you last screenshot there is an extra space in msg.device: " TABBYCAT" how was that produced ?  
the switch node == comparison expects to compare exactly equal with "TABBYCAT" without extra space

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [20 August 2020 00:05 UTC](https://discourse.nodered.org/t/switch-node-not-playing-the-game/31751/3 "2020-08-20T00:05:43Z")

</div>

(Isn't it always the case?)

Yeah, I just noticed the leading space.

ARGH!

Thanks very much.

The _Problem_ is the device sending the message is a phone and when typing the details I must have pressed the _space bar_ by mistake while entering the name.

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [20 August 2020 05:22 UTC](https://discourse.nodered.org/t/switch-node-not-playing-the-game/31751/4 "2020-08-20T05:22:43Z")

</div>

If you instead use _contains_ in the switch node it will be more forgiving to your typing on the phone 😉

---

<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: [3 September 2020 05:22 UTC](https://discourse.nodered.org/t/switch-node-not-playing-the-game/31751/5 "2020-09-03T05:22:43Z")

</div>

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