# Using flow variable in if statement

**URL:** <https://discourse.nodered.org/t/using-flow-variable-in-if-statement/31023>\
**Category:** General\
**Created:** [1 August 2020 20:30 UTC](https://discourse.nodered.org/t/using-flow-variable-in-if-statement/31023 "2020-08-01T20:30:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghstudio](https://avatars.discourse-cdn.com/v4/letter/g/ac8455/32.png) [@ghstudio](https://discourse.nodered.org/u/ghstudio)\
**Post date:** [1 August 2020 20:30 UTC](https://discourse.nodered.org/t/using-flow-variable-in-if-statement/31023/1 "2020-08-01T20:30:32Z")

</div>

I want to save a value in a flow variable once and then compare it to incoming a msg.payload field in an if statement (using a function block)

I have saved the value I want to compare  
then I load it into the colora variable

colora = flow.get(“color”)

now I want to compare that value in color it to a field in an if statement

if (msg.payload.id === colora)  
{msg.payload.new= “blue”}  
else {msg.payload.new=“white”}

my problem is that it never detects when msg.payload.id actually matches the value in the flow.color field so everthing is “white”.....but I can't figure out the syntax to have msg.payload.id compare to the value in colora. (neophyte to node-read)

what's the syntax for this....or should I forget about using calora and just insert the flow.get into the if statement (and again, what's the syntax)?

Any help appreciated...

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [1 August 2020 20:42 UTC](https://discourse.nodered.org/t/using-flow-variable-in-if-statement/31023/2 "2020-08-01T20:42:25Z")

</div>

I'm not at a computer to test it, but have you tried...  
` if (msg.payload.id == colora)` \<\<\< i.e. with two equal signs.

It would be helpful if you published your flow.

---

<div class="post-metadata">

**Author:** ![stefan24](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stefan24/32/23075_2.png) [@stefan24](https://discourse.nodered.org/u/stefan24)\
**Post date:** [1 August 2020 21:22 UTC](https://discourse.nodered.org/t/using-flow-variable-in-if-statement/31023/3 "2020-08-01T21:22:31Z")

</div>

I would put in some debug nodes to check for the values of your msg and if you have used the right variable.

---

<div class="post-metadata">

**Author:** ![ghstudio](https://avatars.discourse-cdn.com/v4/letter/g/ac8455/32.png) [@ghstudio](https://discourse.nodered.org/u/ghstudio)\
**Post date:** [2 August 2020 01:28 UTC](https://discourse.nodered.org/t/using-flow-variable-in-if-statement/31023/4 "2020-08-02T01:28:43Z")

</div>

solved....forgive a newby 🙂 I changed flow to global and now it works. I guess I don't understand when to use flow and when to use global.

---

<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:** [2 August 2020 05:30 UTC](https://discourse.nodered.org/t/using-flow-variable-in-if-statement/31023/5 "2020-08-02T05:30:27Z")

</div>

That doesn't make sense to what you said.

Yes, if you put `flow.get(` instead of `global.get(` but as @dynamicdave said you need to use two `=` signs, not three.

Yes, it is ok to make mistakes when you are new to using Node-red - I've made my fair share.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [2 August 2020 07:53 UTC](https://discourse.nodered.org/t/using-flow-variable-in-if-statement/31023/6 "2020-08-02T07:53:18Z")

</div>

Did you read the documentation? [https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)  
If that confuses you, what confuses you and I'll try to explain.

---

<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:** [1 October 2020 07:53 UTC](https://discourse.nodered.org/t/using-flow-variable-in-if-statement/31023/7 "2020-10-01T07:53:21Z")

</div>

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