# Switch node - multiple conditions

**URL:** <https://discourse.nodered.org/t/switch-node-multiple-conditions/38978>\
**Category:** General\
**Created:** [11 January 2021 21:44 UTC](https://discourse.nodered.org/t/switch-node-multiple-conditions/38978 "2021-01-11T21:44:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dathor](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@dathor](https://discourse.nodered.org/u/dathor)\
**Post date:** [11 January 2021 21:44 UTC](https://discourse.nodered.org/t/switch-node-multiple-conditions/38978/1 "2021-01-11T21:44:50Z")

</div>

Hello

I am looking for a way to route some messages based on a property, lets say the topic. Using a switch node i can route the message. For example 'Foo' could be routed to output one and 'Bar' could be routed to output two.

What if if wanted to route both 'Foo' or 'Bar' to output one and only 'FooBar' to output two. Does anyone have any suggestions on the best way to do this.

Thanks  
D

---

<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:** [11 January 2021 21:58 UTC](https://discourse.nodered.org/t/switch-node-multiple-conditions/38978/2 "2021-01-11T21:58:23Z")

</div>

Using a switch  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/3/b3dc0a0cd96340d95a440d7f53142cc6c0a0c1b3.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/919b7073ee2da16e5768d18580a3fdac511cad34.png)

or you could use a function...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/23bccc8df42141495bef9d49913528e105b332c8.png)

```auto
switch(msg.topic) {
    case "foo":
    case "bar":
        return [msg,null]; //send msg to output 1
    case "foobar":
        return [null,msg]; //send msg to output 2
}

```

* * *

if you really want just 2 outputs and a switch node...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/531c6a8a1727e2403fe79f786e8183338617a65b.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:** [11 January 2021 22:04 UTC](https://discourse.nodered.org/t/switch-node-multiple-conditions/38978/3 "2021-01-11T22:04:12Z")

</div>

You could either route Foo and Bar to output one and two and FooBar to three and then join outputs 1 and 2 together. Alternatively use a regex test to check for Foo or Bar. Include start and end of string in the regex to make sure it doesn't find any string containing Foo and other stuff. So something like  
`^(Foo|Bar)$`

**[Edit]** I guess that a regex will be more efficient than JSONata

---

<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:** [11 January 2021 22:08 UTC](https://discourse.nodered.org/t/switch-node-multiple-conditions/38978/4 "2021-01-11T22:08:45Z")

</div>

> [@Colin](#):
>
> I guess that a regex will be more efficient than JSONata

Possibly yes (I dont advocate JSONata for something like this (it was a POC) - i'd recommend the change node with three outputs as first choice).

---

<div class="post-metadata">

**Author:** ![dathor](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@dathor](https://discourse.nodered.org/u/dathor)\
**Post date:** [11 January 2021 22:24 UTC](https://discourse.nodered.org/t/switch-node-multiple-conditions/38978/5 "2021-01-11T22:24:58Z")

</div>

Thanks all, two great answers. I cant be thinking in a _low-code_ frame of mind because i didn't think of simply using the wires to route the messages.

---

<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:** [12 March 2021 22:25 UTC](https://discourse.nodered.org/t/switch-node-multiple-conditions/38978/6 "2021-03-12T22:25:05Z")

</div>

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