# Another question about the \`switch\` node. (not the GUI)

**URL:** <https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574>\
**Category:** General\
**Created:** [31 January 2022 23:58 UTC](https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574 "2022-01-31T23:58:29Z")\
**Posts on this page:** 16\
**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:** [31 January 2022 23:58 UTC](https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574/1 "2022-01-31T23:58:29Z")

</div>

Ok, I've been called up a few times that I do things _back to front_ when filtering messages.  
If I am looking for a message that isn't of a certain type I find all messages of type and send them to output #1 of the `switch` node.  
Then for output #2 I have the `otherwise` set.  
And then connect to the second output.

Yeah, maybe not the best way.... But this one came up today.

I am getting messages and there are two _versions_ of the message. (Topics different)  
So I get (example)  
`topic` `1`  
`topic` `2`  
`topic` `3`  
`topic` `4`  
`topic` `second1`  
`topic` `second2`  
`topic` `second3`  
`topic` `second4`

I want to ignore all messages with `second` in them.  
I can't see an easy way to use the switch node other than how I do it by default.

Basic config:  
`msg.topic`  
`if msg.topic contains second` --\> output 1  
`otherwise` --\> output 2

And I use output 2.

That is the easiest way - yeah?

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [1 February 2022 00:27 UTC](https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574/2 "2022-02-01T00:27:59Z")

</div>

Well you could do this ...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d47aa54d407fbf7ff02d403c20e7df5f75e700cf.png)

```auto
[{"id":"f763a100670d72b2","type":"switch","z":"5f633a28.607134","name":"","property":"topic","propertyType":"msg","rules":[{"t":"jsonata_exp","v":" $not($contains(topic, 'second'))\t","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":1470,"y":620,"wires":[["93cb67ca666a9a45"]]},{"id":"9563ae0998acc181","type":"inject","z":"5f633a28.607134","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"second1","payloadType":"date","x":1320,"y":580,"wires":[["f763a100670d72b2"]]},{"id":"660fe474003c28b2","type":"inject","z":"5f633a28.607134","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"1","payloadType":"date","x":1300,"y":640,"wires":[["f763a100670d72b2"]]},{"id":"93cb67ca666a9a45","type":"debug","z":"5f633a28.607134","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1620,"y":620,"wires":[]}]

```

---

<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:** [1 February 2022 00:29 UTC](https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574/3 "2022-02-01T00:29:36Z")

</div>

Yeah, ok.

But for the _village idiots_ out there - like me....

That is _black magic_. 😉

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [1 February 2022 00:32 UTC](https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574/4 "2022-02-01T00:32:28Z")

</div>

Only took 5 mins to google the voodoo 🤪

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 February 2022 00:39 UTC](https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574/5 "2022-02-01T00:39:29Z")

</div>

> [@Trying\_to\_learn](#):
>
> That is the easiest way - yeah?

Yes. The most efficient way too, probably.  
But maybe it implies you gave insufficient thought to the value of topic back down the line?

Actually I think the flow is easier to grasp visually if you have a switch node with two outputs, one of which has no wires connected.

![Untitled 6](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/47d77746d71cadc91e2f88c1294ee06deb1c295f.jpeg)

---

<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:** [1 February 2022 00:46 UTC](https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574/6 "2022-02-01T00:46:09Z")

</div>

I haven't tried/noticed that before.

I guess I leave the _unused_ output visible - for me - it is an indication that the passing is a **NOT** rather than a match.

But that's just me.

But those labels are wrong on the `switch` node. It is if `NOT contains "second".`

---

<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:** [1 February 2022 00:49 UTC](https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574/7 "2022-02-01T00:49:17Z")

</div>

Yeah, but does it really save anything.  
Putting it like that is basically what the second line in the switch node would do anyway - yeah?

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [1 February 2022 00:54 UTC](https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574/8 "2022-02-01T00:54:06Z")

</div>

Yeah its functionally the same effect, but only has one output.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 February 2022 00:54 UTC](https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574/9 "2022-02-01T00:54:31Z")

</div>

I sometimes do it the same way as you @Trying_to_learn but the resulting unconnected outputs and wires that bend without good reason offend my inner NCO.  
As do long node names. I'd probably just use "second?"

---

<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:** [1 February 2022 00:59 UTC](https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574/10 "2022-02-01T00:59:20Z")

</div>

Yeah, long node names are a bit of a thing.

Also that sometimes I shrink the node to the small 1 x 1 square.  
But then I do give it a good name so if I put the mouse over it, I see what it is and what it does.

---

<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:** [1 February 2022 05:06 UTC](https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574/11 "2022-02-01T05:06:08Z")

</div>

I would question the reason why you receive these topics in the first place (if you don't want them there).

---

<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:** [1 February 2022 05:12 UTC](https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574/12 "2022-02-01T05:12:40Z")

</div>

The messages are endemic messages from a command.

I originally thought the code would not use them. Alas recent events have shown me I was wrong.

So I needed to filter out those from the stream.

$hit happens.

---

<div class="post-metadata">

**Author:** ![xury](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xury/32/25637_2.png) [@xury](https://discourse.nodered.org/u/xury)\
**Post date:** [1 February 2022 14:09 UTC](https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574/13 "2022-02-01T14:09:18Z")

</div>

Always you can delete unneeded message using change node or in function node.  
Or using function node save the message to temporary variable next clean all message and finally  
get important data from temporary variable and prepare a new message to resend it.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [1 February 2022 14:24 UTC](https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574/14 "2022-02-01T14:24:32Z")

</div>

Having a unused output is the current NR way of selecting something that doesn't contain a search string.

I've made a recent feature request to have a "not contains" option as well

And for all the options that currently don't have a not alternative

---

<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:** [1 February 2022 20:05 UTC](https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574/15 "2022-02-01T20:05:19Z")

</div>

@cymplecy  
That's what I was thinking too.

But this is my work around. 😉

@xury  
True.

But for the _cost_ of the `change` (or `function`) node....

I'm not wanting to start counting how many bits of memory are needed for each.

I was just wanting to be sure I wasn't missing anything obvious.

---

<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:** [2 April 2022 20:06 UTC](https://discourse.nodered.org/t/another-question-about-the-switch-node-not-the-gui/57574/16 "2022-04-02T20:06:08Z")

</div>

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