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

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

[{"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"]]}]

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.

because there was a match, "otherwise" is skipped

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

Unfortunately this is not quite true:

[{"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.

This is a known condition and has been debated before:

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

1 Like

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:

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 :+1:

'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.

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

payload is 2 and the result is this:

[{"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!

1 Like