# Switch node, check all mode and otherwise - why does otherwise not match

**URL:** https://discourse.nodered.org/t/switch-node-check-all-mode-and-otherwise-why-does-otherwise-not-match/96794
**Category:** Core Development
**Created:** [29 April 2025 06:29 UTC](https://discourse.nodered.org/t/switch-node-check-all-mode-and-otherwise-why-does-otherwise-not-match/96794 "2025-04-29T06:29:42Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)
#### Post date: [29 April 2025 06:29 UTC](https://discourse.nodered.org/t/switch-node-check-all-mode-and-otherwise-why-does-otherwise-not-match/96794/1 "2025-04-29T06:29:42Z")

</div>

Hi there,

Just out of interest because nothing will change this behaviour but why does the "otherwise" rule not _always_ match when using "check all rules" mode in the switch node?

I.e. this flow

```auto
[{"id":"723f1119d7dd88f8","type":"switch","z":"ef91cb280e1bfd72","g":"699a34224f477cdf","name":"check all rules","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"},{"t":"else"}],"checkall":"true","repair":true,"outputs":4,"x":4597,"y":878.5,"wires":[["4a8934e80c751783"],["420dfe30dec4ecae"],["19f613b61a79f2f6"],["e7380ee7d85d56df","f38ded1d50f43efe"]]},{"id":"b53af12b5b817266","type":"inject","z":"ef91cb280e1bfd72","g":"699a34224f477cdf","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"num","x":4361,"y":880,"wires":[["723f1119d7dd88f8"]]}]

```

 ![Screenshot 2025-04-29 at 08.25.17](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/1/c1bba11801d21fb1f71b93a00a929e26361ce970.png)

Payload is 2, switch has four rules checking for 1,2,3 and otherwise and is in "check all rules" mode.

I would have expected that the rule equal 2 and the otherwise rule would match but only the equal 2 rule matches. this behaviour is the same as the stop after first match mode.

Seems illogical to me but perhaps I've missed something here.

---

<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: [29 April 2025 06:38 UTC](https://discourse.nodered.org/t/switch-node-check-all-mode-and-otherwise-why-does-otherwise-not-match/96794/2 "2025-04-29T06:38:35Z")

</div>

> [@gregorius](#):
>
> I would have expected that the rule equal 2 and the otherwise rule

because there was a match, "otherwise" is skipped

> [@gregorius](#):
>
> this behaviour is the same as the stop after first match mode.

It makes more sense when the options are not discrete options e.g..

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0cff609f687aee1d2189243883500ca0e5d5f672.png)

---

<div class="post-metadata">

### Author: ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)
#### Post date: [29 April 2025 06:47 UTC](https://discourse.nodered.org/t/switch-node-check-all-mode-and-otherwise-why-does-otherwise-not-match/96794/3 "2025-04-29T06:47:22Z")

</div>

> [@Steve-Mcl](#):
>
> because there was a match, "otherwise" is skipped

Unfortunately this is not quite true:

```auto
[{"id":"723f1119d7dd88f8","type":"switch","z":"f487e910abd71599","g":"699a34224f477cdf","name":"check all rules","property":"payload","propertyType":"msg","rules":[{"t":"else"},{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"}],"checkall":"true","repair":true,"outputs":4,"x":730,"y":786.5,"wires":[["e7380ee7d85d56df"],["4a8934e80c751783"],["420dfe30dec4ecae"],["19f613b61a79f2f6"]]},{"id":"b53af12b5b817266","type":"inject","z":"f487e910abd71599","g":"699a34224f477cdf","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"num","x":494,"y":788,"wires":[["723f1119d7dd88f8"]]}]

```

moving the "otherwise" to the top of the rules causes it to match _and_ the payload = 2 also to match.

Move the otherwise to the bottom of the list and it doesn't match.

this is for check all mode.

In "stop after one" mode, the otherwise matches but not the payload = 2 rule.

So the otherwise rule depends on where it's located, i.e. before or after a rule already matched.

EDIT: so the behaviour I would expect would happen if the otherwise was the first rule in a check all mode node.

---

<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: [29 April 2025 06:49 UTC](https://discourse.nodered.org/t/switch-node-check-all-mode-and-otherwise-why-does-otherwise-not-match/96794/4 "2025-04-29T06:49:43Z")

</div>

> [@gregorius](#):
>
> moving the "otherwise" to the top of the rules causes it to match _and_ the payload = 2 also to match.
> 
> Move the otherwise to the bottom of the list and it doesn't match.

This is a known condition and has been debated before:

- [[Feature Request]Help prevent crossing wires from Switch node - #43 by dceejay](https://discourse.nodered.org/t/feature-request-help-prevent-crossing-wires-from-switch-node/75233/43)
- [[Feature Request]Help prevent crossing wires from Switch node - #51 by knolleary](https://discourse.nodered.org/t/feature-request-help-prevent-crossing-wires-from-switch-node/75233/51)

> [@gregorius](#):
>
> In "stop after one" mode, the otherwise matches but not the payload = 2 rule.

As above. And if you think about it, it makes sense (it stopped after one match)

---

<div class="post-metadata">

### Author: ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)
#### Post date: [29 April 2025 07:01 UTC](https://discourse.nodered.org/t/switch-node-check-all-mode-and-otherwise-why-does-otherwise-not-match/96794/5 "2025-04-29T07:01:40Z")

</div>

> [@Steve-Mcl](#):
>
> if you think about it, it makes sense

It does, it's just that my logical thinking thought that if check all does really check _all_ rules, then otherwise _must_ match. But check all is "check all rules that aren't otherwise unless nothing matched" is the logic. Either way, I have a workaround and that is moving otherwise up to the top.

From [you from here](https://discourse.nodered.org/t/feature-request-help-prevent-crossing-wires-from-switch-node/75233/46):

> Agree. "Otherwise" (aka `else` ) should always be last and IMO would make more sense being a simple checkbox option along the lines of `[x] output when no rules match` and would always be the last output.

Disagree. I think we are all grown ups here and should be able to make logical errors if we like. Now that I better understand otherwise, I'll put up to the top if I need to.

Either way, I implemented this incorrectly in Erlang and now have fix it. Damn.

Thanks for the explanation 👍

---

<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: [29 April 2025 07:53 UTC](https://discourse.nodered.org/t/switch-node-check-all-mode-and-otherwise-why-does-otherwise-not-match/96794/6 "2025-04-29T07:53:13Z")

</div>

> [@gregorius](#):
>
> it's just that my logical thinking thought that if check all does really check _all_ rules, then otherwise _must_ match

'Otherwise' is another way of saying 'IF NOT rule 1 AND NOT rule 2 AND ...' So if any of the preceding rules are true then 'Otherwise' is false, so it should not send an output.

---

<div class="post-metadata">

### Author: ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)
#### Post date: [29 April 2025 07:59 UTC](https://discourse.nodered.org/t/switch-node-check-all-mode-and-otherwise-why-does-otherwise-not-match/96794/7 "2025-04-29T07:59:24Z")

</div>

> [@Colin](#):
>
> 'Otherwise' is false, so it should not send an output.

It even gets funkier, try adding multiple otherwises to a switch:

 ![Screenshot 2025-04-29 at 09.55.40](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/b/2bb99513d4c4e7b51dbc3350a9c6af0cd7e547f2.png)

payload is 2 and the result is this:

 ![Screenshot 2025-04-29 at 09.55.51](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/6/56587ad7d05d67d8bd5aa0cdddd870c94fd9047d.png)

```auto
[{"id":"ee82b65092fc6dd4","type":"switch","z":"f487e910abd71599","name":"check all rules - otherwise before and after match","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"else"},{"t":"else"},{"t":"eq","v":"2","vt":"num"},{"t":"else"},{"t":"eq","v":"3","vt":"num"},{"t":"eq","v":"2","vt":"num"}],"checkall":"true","repair":false,"outputs":7,"x":1905,"y":2562,"wires":[["3ac066060a7ac7ac"],["ad6afa0f888505e4"],["5fe8db46a084a7ef"],["968dbb96e3677489"],["abee4609a4b6e8a6"],["e9e104bbee85f94d"],["9dbe1b52f584387a"]]},{"id":"79083bb0f34516aa","type":"inject","z":"f487e910abd71599","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"num","x":1557,"y":2562,"wires":[["ee82b65092fc6dd4"]]},{"id":"3ac066060a7ac7ac","type":"ut-assert-failure","z":"f487e910abd71599","name":"","x":2299,"y":2425.45458984375,"wires":[]},{"id":"968dbb96e3677489","type":"ut-assert-success","z":"f487e910abd71599","name":"","x":2302,"y":2585.272705078125,"wires":[]},{"id":"abee4609a4b6e8a6","type":"ut-assert-failure","z":"f487e910abd71599","name":"","x":2302,"y":2638.181787109375,"wires":[]},{"id":"e9e104bbee85f94d","type":"ut-assert-failure","z":"f487e910abd71599","name":"","x":2302,"y":2691.090869140625,"wires":[]},{"id":"9dbe1b52f584387a","type":"ut-assert-success","z":"f487e910abd71599","name":"","x":2300,"y":2743.999951171875,"wires":[]},{"id":"ad6afa0f888505e4","type":"ut-assert-success","z":"f487e910abd71599","name":"","x":2299,"y":2478.363671875,"wires":[]},{"id":"5fe8db46a084a7ef","type":"ut-assert-failure","z":"f487e910abd71599","name":"","x":2302,"y":2534,"wires":[]}]

```

that is a check all swich where the first otherwise matches and the second and third otherwises don't. The point here is that that the otherwise rule counts as something matching - for the purposes of the otherwise rule.

This is purely academic - there is no really reason - i can see - for having two otherwise rules one after the other but hey, yolo!

---

<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 May 2025 07:59 UTC](https://discourse.nodered.org/t/switch-node-check-all-mode-and-otherwise-why-does-otherwise-not-match/96794/8 "2025-05-13T07:59:42Z")

</div>

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