# Using variables

**URL:** <https://discourse.nodered.org/t/using-variables/24681>\
**Category:** General\
**Created:** [12 April 2020 07:58 UTC](https://discourse.nodered.org/t/using-variables/24681 "2020-04-12T07:58:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![speedyrazor](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@speedyrazor](https://discourse.nodered.org/u/speedyrazor)\
**Post date:** [12 April 2020 07:58 UTC](https://discourse.nodered.org/t/using-variables/24681/1 "2020-04-12T07:58:15Z")

</div>

Hi, I am familiar with python, and use of variables, but I just can quite get my head around how to use them in Node Red, so would greatly appreciate a working example.  
This is the essence of what I am trying to achieve:

- When the flow starts, in the first node, set a flow variable called "ToiletLight" to 1
- In the second node, query "ToiletLight", if it == 1 then set "ToiletLight" to 2 then continue to next node
- The value of "ToiletLight should be visible and usable to this entire flow whilst activated

Thats it, very simple, but couldn't get it working.  
Kind regards.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 April 2020 08:09 UTC](https://discourse.nodered.org/t/using-variables/24681/2 "2020-04-12T08:09:06Z")

</div>

It would be worth reading the node red documentation. Especially the sections on working with messages and context. They're short reads and should be easily digestible as you have a programming background.

At this moment, it seems you are thinking in a classic procedural programming mannor and you need to adjust to the flow based programming methods. Tip: Node red is pretty much totally event based

As a hint...  
Use an inject node \> change node (set a flow context variable).

Then use a change node to get value back out of flow context.

---

<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:** [12 April 2020 08:13 UTC](https://discourse.nodered.org/t/using-variables/24681/3 "2020-04-12T08:13:15Z")

</div>

> [@Steve-Mcl](#):
>
> As a hint...  
> Use an inject node \> change node (set a flow context variable).
> 
> Then use a change node to get value back out of flow context.

or use the `change` node to move the value to a msg element like msg.toiletlight then it will be part of the msg going from node to node.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 April 2020 08:23 UTC](https://discourse.nodered.org/t/using-variables/24681/4 "2020-04-12T08:23:59Z")

</div>

What he said ^

As I attempted to allude to in the statements "it seems you are thinking in a classic procedural programming mannor" & "Node red is pretty much totally event based" this is how we do most things in node red.

Just tack the value on to the `msg` then pass it to the next node(s) for processing. No variables, no storage, just event based.

---

<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:** [11 June 2020 08:24 UTC](https://discourse.nodered.org/t/using-variables/24681/5 "2020-06-11T08:24:06Z")

</div>

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