# Node-red : switch - strange behaviour

**URL:** <https://discourse.nodered.org/t/node-red-switch-strange-behaviour/59529>\
**Category:** General\
**Created:** [8 March 2022 09:55 UTC](https://discourse.nodered.org/t/node-red-switch-strange-behaviour/59529 "2022-03-08T09:55:05Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ylechasseur](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ylechasseur/32/6549_2.png) [@ylechasseur](https://discourse.nodered.org/u/ylechasseur)\
**Post date:** [8 March 2022 09:55 UTC](https://discourse.nodered.org/t/node-red-switch-strange-behaviour/59529/1 "2022-03-08T09:55:05Z")

</div>

OS: Windows 11

Dashboard version: 2.2.0

All my nodes are to the latest version.

Browser: Chrome

Hi,

I am not sure if this strange behaviour happened after a node-red update.

I am using the switch node ( not from the dashboard) for routing messages. If the msg.payload is true or false, it will take action accordingly.

When the switch evaluates the value, it behaves as a loop. What I mean, it will do the same action several times and then will behave normally, in this case, waiting for the user's selection (ok or no)

Per example. This flow from the print screen.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/4/64533efbf0295983c2249b2966a89f1aa283aac3.png)

1. Message popup and wait for user selection.
2. if Ok, it will popup a second message to get user confirmation
3. Then if click OK, data will be deleted in MYSQL db.

The behaviour:

It can loop several times on the first or second popup message and then stop and wait for the user for selection (ok or no) . Then if it is ok, it will delete the information in MySQL DB.

This will happen also in other switches I am using for other tasks as well ( as selection an action if the payload =1 or 2 and so on ..

My Workaround

1. I replace the switch node by function node and I do a switch(msg.payload) in the code and all works nice
2. Or I delete the switch node, drop a new one, put back the parameters and then it works normally.

I don't know if anyone got this in the past

Best

Yves

---

<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:** [8 March 2022 09:59 UTC](https://discourse.nodered.org/t/node-red-switch-strange-behaviour/59529/2 "2022-03-08T09:59:41Z")

</div>

Make sure the `switch` node is checking for the correct type of 1 and 2.

Put a `debug` node just before the `switch` node and look for the messages.

If you see the 1 and 2 as BLUE, they are `numbers`. If they are RED, they are `text.

So in the `switch` node make sure you have the correct type selected.

---

<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:** [8 March 2022 10:13 UTC](https://discourse.nodered.org/t/node-red-switch-strange-behaviour/59529/3 "2022-03-08T10:13:31Z")

</div>

> [@ylechasseur](#):
>
> Dashboard version: 2.2.0

Do you mean that node red is version 2.2.0? You can check this at the bottom of the menu dropdown. You said this happened after a node-red update, but the latest node-red is 2.2.2, though looking at the [release notes](https://github.com/node-red/node-red/releases) I don't see anything there that would cause the problem. It might be worth updating anyway.

Current node-red-dashboard latest is 3.1.6

Assuming it is still failing after the update, get it into the failing mode, mark all those nodes Use Ctrl-E to export to the clipboard and paste it here, using the `</>` button at the top of the forum entry window when pasting it in. Then we can import it, have a look at the settings and test it.

---

<div class="post-metadata">

**Author:** ![ylechasseur](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ylechasseur/32/6549_2.png) [@ylechasseur](https://discourse.nodered.org/u/ylechasseur)\
**Post date:** [8 March 2022 10:17 UTC](https://discourse.nodered.org/t/node-red-switch-strange-behaviour/59529/4 "2022-03-08T10:17:33Z")

</div>

Hi ,  
I should put the information as well. I used the debug nodes as well as the flow debugger.  
It is good you are taking about the numbrs or text type, I did the mistake in the past.

When I use the flow debugger, it shows, in the blue debugger info break point the amount of times it will loop - yesterday It went up to 36 !! it loop the Message Popup Windows 36 times and then wait for that I answer the question ( OK or No) . The day before, without changing anything, it was working just fine..  
As I mentioned, if I delete the switch node and drop a new one and put inside exactly the same condition it is working .. It was doing that this morning and that is what I did and now works just fine.

thanks for the reply,

Best  
Yves

---

<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:** [8 March 2022 10:23 UTC](https://discourse.nodered.org/t/node-red-switch-strange-behaviour/59529/5 "2022-03-08T10:23:06Z")

</div>

I would nearly suggest you post the code, but as they are foreign nodes, it won't be easy for most people to load it.

Can you make a smaller flow with an `inject` node and the `switch` node.  
(Actually a couple of `inject` nodes. One that you want to detect and one that won't be detected. Maybe 2 not to be detected just to be sure)

See if that works. It may help you get a better picture of what is going on.

Sorry, just spit balling ideas.

---

<div class="post-metadata">

**Author:** ![ylechasseur](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ylechasseur/32/6549_2.png) [@ylechasseur](https://discourse.nodered.org/u/ylechasseur)\
**Post date:** [8 March 2022 10:38 UTC](https://discourse.nodered.org/t/node-red-switch-strange-behaviour/59529/6 "2022-03-08T10:38:44Z")

</div>

It is ok and normal, debug process is not always easy. I have an idea, I will take print screen of each action and put the code bellow the print screen in the order they are call.. will see if it is good...

Then it will be easy to follow and might find what could be wrong..

---

<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:** [8 March 2022 10:42 UTC](https://discourse.nodered.org/t/node-red-switch-strange-behaviour/59529/7 "2022-03-08T10:42:14Z")

</div>

I had not realised that you were using non-standard nodes. Tell us what they are and show us what is coming out of those nodes when it is failing. Also how you have configured the switch nodes.

---

<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:** [8 March 2022 10:43 UTC](https://discourse.nodered.org/t/node-red-switch-strange-behaviour/59529/8 "2022-03-08T10:43:53Z")

</div>

> [@ylechasseur](#):
>
> Then it will be easy to follow and might find what could be wrong..

If you configure the debug nodes to ouput to the console then they output to the node red log so you can analyse them later. Give the nodes names.

---

<div class="post-metadata">

**Author:** ![ylechasseur](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ylechasseur/32/6549_2.png) [@ylechasseur](https://discourse.nodered.org/u/ylechasseur)\
**Post date:** [8 March 2022 12:06 UTC](https://discourse.nodered.org/t/node-red-switch-strange-behaviour/59529/9 "2022-03-08T12:06:24Z")

</div>

Here it comes the process

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/7/67bbe9cc5337f01f987ca827ae338c09b0ca2696.png)

1\*\*. Depending the action, a message number is assign to msg.message\*\*

Here what inside Transaction validation node  
Transaction validation used the MYSQL transaction status  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/4/741c00d58e3b468dae6270e62b9bea3fd695687b.png)

Then dependig if 0 or 1 the appropriated message for the Popup Windows

Number asociated to the message.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/d/edf8eae88fe51962c82f0c4651ea33ed54dc5d87.png)

**2. Link to the message center.**  
Message center is a sub flow who handle and assing the appropriate message according to the Integer assigned to msg.message

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/8/9847bb2c91a97fd375648397fff0b6d5bf67c672.png)

**SUB NODE  
Popup Messages NODE**  
When message arrive to message center, it is process by the sub node Popup Message

Message flow in the sub node

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

Switch handle message number and route it to the selected number  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/7/67a6610b84326e25e3ac9609590b27f89714b0ad.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/d/ed1279f51206c54d3e05dee8049d9e539835c574.png)

**5 - LANG\_UI\_BUTTON\_SAVE\_SUCCES**  
The appropriate text is assign from a Global variable according to the selected default language.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/1/f1be292e8d1e446996f26405c926cd519325f61d.png)

**TEMPLATE NODE**  
The HTML code to display the message, Icon and message Header  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/1/a1b076b133f42f31df4ddd018c6bfb690e28e90e.png)

**Action Message Node**  
STD message config.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b263e71e7d9132950dae30b508c13c8f2b22cb23.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/9/69d7dd0d2a065f18badce7cdf8e918bb73ef09f2.png)

Then it return to the calling Tab with the selected user's value  
**END SUB NODE**

**VALIDATION NODE - VERSION WITH FUNCTION NODE**  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/ca815f78e6c95698cb70bfc63937ba2e080b8213.png)

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

Switch node  
Validate if true or false

this part will only happen if the action required a second validation as Delete Data  
**Message 8 - Delete Confirmation**  
_This morning it was looping here (up to 36 times)_  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/34ceae157d117b54e7ae8642caae92aea37a4745.png)

**SUB FLOW**  
Return to Sub Flow to get the same actions as the one describe prior with the same configuration  
**END SUB-FLOW**

Cleaning any left msg.payload  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/1/b153e900fdce66ecefccfef5ea202eef9dd7c759.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/a/3a1500a40135dc635bd9b82bca3cb8393a0156a7.png)

**Answer Validation**  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/1/01298bf2dc1938fe4af6bb0e6e5327424426b7ce.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/f/5ff5eefdfa3ce1d844556ced5d956e9396860c71.png)

**Link**  
Then it links to the next action ( in this case a SQL delete )

I fixed it this morning by deleting change node ( i just realized, this time it wasn;t switch node that I deleted but the change node) and recreated it .  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/5/05481af5a800be40265cbb98a8d2979e68e52f54.png)

What I'll do next, I will try to reproduce the bug and getting the console log to see what I will get.

Yves

---

<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:** [8 March 2022 13:24 UTC](https://discourse.nodered.org/t/node-red-switch-strange-behaviour/59529/10 "2022-03-08T13:24:34Z")

</div>

Often symptoms like you describe are caused by timing issues. Have you considered what would happen if several messages arrive at the front end very close together?

Are you using flow or global context at all?

---

<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:** [8 March 2022 14:09 UTC](https://discourse.nodered.org/t/node-red-switch-strange-behaviour/59529/11 "2022-03-08T14:09:31Z")

</div>

> [@Colin](#):
>
> Are you using flow or global context at all?

Answering my own question I see you are using flow.action. So if two messages arrive close together how are you keeping the flow.action setting separate for the two messages that will be in the flow at the same time?

---

<div class="post-metadata">

**Author:** ![ylechasseur](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ylechasseur/32/6549_2.png) [@ylechasseur](https://discourse.nodered.org/u/ylechasseur)\
**Post date:** [8 March 2022 16:40 UTC](https://discourse.nodered.org/t/node-red-switch-strange-behaviour/59529/12 "2022-03-08T16:40:22Z")

</div>

In fact - for flow.action is attached to Multistate switch. The action is either Add - Add - Delete. - I clean each time the status change and if any save is done, the value is reset to default mode ( edit)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/b/abe3f7c1af87f51a6c89defe4feceed3dbeefb42.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:** [8 March 2022 16:42 UTC](https://discourse.nodered.org/t/node-red-switch-strange-behaviour/59529/13 "2022-03-08T16:42:54Z")

</div>

So have you considered what happens if two messages arrive at the front at almost the same time? There isn't anything in your flow, as far as I can see, to stop them both being actioned in parallel. Will your flow work in that case?

---

<div class="post-metadata">

**Author:** ![ylechasseur](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ylechasseur/32/6549_2.png) [@ylechasseur](https://discourse.nodered.org/u/ylechasseur)\
**Post date:** [8 March 2022 17:23 UTC](https://discourse.nodered.org/t/node-red-switch-strange-behaviour/59529/14 "2022-03-08T17:23:37Z")

</div>

That is a good point. I didn't look closely at it  
Thanks  
Yves

---

<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:** [7 May 2022 17:24 UTC](https://discourse.nodered.org/t/node-red-switch-strange-behaviour/59529/15 "2022-05-07T17:24:04Z")

</div>

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