# Noob set flow variable question

**URL:** <https://discourse.nodered.org/t/noob-set-flow-variable-question/40245>\
**Category:** General\
**Created:** [2 February 2021 14:57 UTC](https://discourse.nodered.org/t/noob-set-flow-variable-question/40245 "2021-02-02T14:57:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vkuehn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vkuehn/32/34805_2.png) [@vkuehn](https://discourse.nodered.org/u/vkuehn)\
**Post date:** [2 February 2021 14:57 UTC](https://discourse.nodered.org/t/noob-set-flow-variable-question/40245/1 "2021-02-02T14:57:11Z")

</div>

Accroding to the documentation it should work to set variables from a function node to the flow like that.

```auto
if (flow.get("bot_command")!==undefined){
    flow.set("bot_command", "c0,0");
}
msg.payload = flow.get('bot_command');
return msg;

```

Also that didn't work  
 ![ChangeNode](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/1/31171ae262ffcd486ee5231a625946852f9104a6.png)

do I have to prepare something before using flow variables ?

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [2 February 2021 15:16 UTC](https://discourse.nodered.org/t/noob-set-flow-variable-question/40245/2 "2021-02-02T15:16:41Z")

</div>

I think the expression should be  
== 'undefined'

---

<div class="post-metadata">

**Author:** ![vkuehn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vkuehn/32/34805_2.png) [@vkuehn](https://discourse.nodered.org/u/vkuehn)\
**Post date:** [2 February 2021 15:20 UTC](https://discourse.nodered.org/t/noob-set-flow-variable-question/40245/3 "2021-02-02T15:20:31Z")

</div>

@gerry That did actually work, thanks.  
Would you know how to do the same in a change node ?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [2 February 2021 15:27 UTC](https://discourse.nodered.org/t/noob-set-flow-variable-question/40245/4 "2021-02-02T15:27:31Z")

</div>

You cannot do a 'set if not defined' type of rule in the Change node, but aside from that, you can set a flow context value. In the screenshot you've shared, I can see you were using `msg.paylod` and not `msg.payload`.

---

<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:** [2 February 2021 15:31 UTC](https://discourse.nodered.org/t/noob-set-flow-variable-question/40245/5 "2021-02-02T15:31:16Z")

</div>

> [@vkuehn](#):
>
> `if (flow.get("bot_command")!==undefined)`

That says that if it is _not_ undefined then set it. Perhaps you meant if it _is_ undefined then set it.

```auto
if (flow.get("bot_command")===undefined){

```

that syntax is frowned on, however, as the name `undefined` can be set to a value. An alternative is

```auto
if (typeof(flow.get("bot_command")) === "undefined"){

```

or, if know that the variable should never be zero or `false` or an empty string then you can use

```auto
msg.payload = flow.get("bot_command") || "c0,0"
flow.set("bot_command",msg.payload)
return msg

```

---

<div class="post-metadata">

**Author:** ![vkuehn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vkuehn/32/34805_2.png) [@vkuehn](https://discourse.nodered.org/u/vkuehn)\
**Post date:** [2 February 2021 15:36 UTC](https://discourse.nodered.org/t/noob-set-flow-variable-question/40245/6 "2021-02-02T15:36:53Z")

</div>

@knolleary thanks changing it to payload did work....sorry posting a typo  
it would be nice if that is possible  
@Colin thanks for the hints

---

<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:** [3 April 2021 15:37 UTC](https://discourse.nodered.org/t/noob-set-flow-variable-question/40245/7 "2021-04-03T15:37:01Z")

</div>

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