# Switch a message depending on another variable

**URL:** <https://discourse.nodered.org/t/switch-a-message-depending-on-another-variable/14548>\
**Category:** General\
**Created:** [20 August 2019 11:59 UTC](https://discourse.nodered.org/t/switch-a-message-depending-on-another-variable/14548 "2019-08-20T11:59:53Z")\
**Posts on this page:** 15\
**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:** [20 August 2019 11:59 UTC](https://discourse.nodered.org/t/switch-a-message-depending-on-another-variable/14548/1 "2019-08-20T11:59:53Z")

</div>

I am wanting to _switch_ a message depending on another variable.

Looking at the `switch` node, that seems beyond the scope of that node.

Anyone got any ideas how to do that?

Example:  
set `flow.variable` in another part of the flow.  
New message comes in.  
Depending on what `flow.variable` is, depends on where the message is sent.

I'm guessing that is getting into the `function` node's territory.

---

<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:** [20 August 2019 12:02 UTC](https://discourse.nodered.org/t/switch-a-message-depending-on-another-variable/14548/2 "2019-08-20T12:02:14Z")

</div>

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

---

<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:** [20 August 2019 12:04 UTC](https://discourse.nodered.org/t/switch-a-message-depending-on-another-variable/14548/3 "2019-08-20T12:04:00Z")

</div>

(Know where I can get a new brain?)

HOW did I miss that?

Thanks.

I just have to check that is actually going to do what I want.  
(That's the next _problem_)

---

<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:** [20 August 2019 12:50 UTC](https://discourse.nodered.org/t/switch-a-message-depending-on-another-variable/14548/4 "2019-08-20T12:50:04Z")

</div>

Ok, I have moved a few squares forward.

I have the `flow.variable` set.

A new message arrives. Initial thoughts were that what I said first off is/was good enough.  
Alas I didn't think it though all the way - typical. I know.

Now I am wanting to know if I can multiple _switch_ messages in the one node.

So:  
If `flow.variable` is _condition_ and if `msg.payload` != `this` or `that`.

I'm guessing it will have to be two (or more) nodes cascaded after each other to get that done.

---

<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:** [20 August 2019 13:33 UTC](https://discourse.nodered.org/t/switch-a-message-depending-on-another-variable/14548/5 "2019-08-20T13:33:24Z")

</div>

You possibly "could" use a JSONAta expression but I'd stick with much clearer and easier solution of sequential switch nodes.

Then later on, convert them to a custom function node if you want to save some editor real estate space 🙂

---

<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:** [20 August 2019 13:44 UTC](https://discourse.nodered.org/t/switch-a-message-depending-on-another-variable/14548/6 "2019-08-20T13:44:54Z")

</div>

(Since we are still talking here....)

This is the next stage of the problem with the `switch` node:

I have `flow.context` data set.  
I build a message with a section set. (Shown)  
Injecting the `switch` seems to send it the wrong way.

It should go to output #1. (not 2)

![Screenshot%20from%202019-08-20%2023-34-38](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d6288e1244820e474b524ffc6ba9b1f498f7bb03.png)

 ![Screenshot%20from%202019-08-20%2023-34-51](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/bde876f0f6db9c561df96be752b23c3e42cc7173.png) ![Screenshot%20from%202019-08-20%2023-35-00](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/77bf67cd4603fa9430204ab24304a2a173e1bfd1.png) ![Screenshot%20from%202019-08-20%2023-35-48](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/fa347780d681c8ed41f12d1dbc7b7faab756bfe3.png)

Is that clear with the piccies?

It shouldn't be going where it is - thus the node is called "NOT WANTED JUST NOW". (last picture)  
The switch says `if (flow.get) == msg.who`  
And as far as I can see it does.

(Weird happening)

Trying something this is now working.  
But look at the red lines showing the names.

I did this (I was trying to reverse the comparison names and forgot to take the `_1` suffix from one of the nodes.

IT WORKS!

Why?

 ![Screenshot%20from%202019-08-20%2023-53-23](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/1a9213c3b8f381f7ddddc130dce8d86bf9eea6ba.png) ![Screenshot%20from%202019-08-20%2023-53-32](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/dfa2b0493975859da643ec2bf0a16603c0b6bfff.png)

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [20 August 2019 13:54 UTC](https://discourse.nodered.org/t/switch-a-message-depending-on-another-variable/14548/7 "2019-08-20T13:54:13Z")

</div>

> [@Trying\_to\_learn](#):
>
> Is that clear with the piccies?

Not for me.  
It would be better to create a separate tab, where you recreate the problem in a simple flow.

---

<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:** [20 August 2019 14:01 UTC](https://discourse.nodered.org/t/switch-a-message-depending-on-another-variable/14548/8 "2019-08-20T14:01:29Z")

</div>

Picture 1  
The `flow` _context_ is set to `D1` called `who_1`  
Picture 2  
You can see the comparison done in the node to determine message switching.  
It looks at `msg.who` and compares it to `flow.who_1`  
Picture 3  
You can see the `change` node setting `msg.who` to `D1`  
Picture 4  
You see the message with `msg.who` set to `D1` going through the `switch` node (double red circles) and coming out on output #2.  
This goes against what was shown if `msg.who` == `flow.who_1`

Now with the latest post that if I make them both `who_1` it works.

To me that is weird.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [20 August 2019 14:09 UTC](https://discourse.nodered.org/t/switch-a-message-depending-on-another-variable/14548/9 "2019-08-20T14:09:14Z")

</div>

Incidentally, I think that you could also use a finite state machine for this kind of routing? Probably only worth it for more complex examples though.

---

<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:** [20 August 2019 14:10 UTC](https://discourse.nodered.org/t/switch-a-message-depending-on-another-variable/14548/10 "2019-08-20T14:10:58Z")

</div>

Thanks, but that is really REALLY beyond my skill.

(And there is another _gremlin_ I realise too)

If the `flow.who` is empty, it has to go to output #1. Although it doesn't match.  
I think I will have to put a `function` node here rather than a `switch`.

Bed calling.  
Night!  
Thanks for the replies.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [20 August 2019 14:11 UTC](https://discourse.nodered.org/t/switch-a-message-depending-on-another-variable/14548/11 "2019-08-20T14:11:55Z")

</div>

> [@Trying\_to\_learn](#):
>
> If the `flow.who` is empty, it has to go to output #1. Although it doesn't match.  
> I think I will have to put a `function` node here rather than a `switch` .

And before you know it, your flow is more complex and difficult than using a FSM 😉

---

<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:** [20 August 2019 14:12 UTC](https://discourse.nodered.org/t/switch-a-message-depending-on-another-variable/14548/12 "2019-08-20T14:12:49Z")

</div>

Yup. I seem to be destined to be . . . . haunted (?) by things I don't understand.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [20 August 2019 14:13 UTC](https://discourse.nodered.org/t/switch-a-message-depending-on-another-variable/14548/13 "2019-08-20T14:13:57Z")

</div>

Well you can be haunted by it or you can embrace it! Keeps you young!!

---

<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:** [20 August 2019 21:00 UTC](https://discourse.nodered.org/t/switch-a-message-depending-on-another-variable/14548/14 "2019-08-20T21:00:14Z")

</div>

True.

I have downloaded the "FSM" node, but it just seems too vague for me at this time.

Oh, there are two.  
FSM and finite-state-machine.

Any preferences?

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [21 August 2019 01:18 UTC](https://discourse.nodered.org/t/switch-a-message-depending-on-another-variable/14548/15 "2019-08-21T01:18:46Z")

</div>

DSM is actually the better and more maintained one - the author is often on the forum also

Craig
