How to Declare Variable to be used in the Same NodeRed Flow

How to Declare Variable to be used in the Same NodeRed Flow

In this post i will step through  2 Different method on how to  Declare Variable to be used in the Same NodeRed Flow.

Method 1

For Method 1 I will use Change node to Set / declare the Flow Variable

Step 1

Drag out and inject Node  and Configure as Below

Pass a String Value ” This is A String ” to the msg.payload

Step 2

Drag Out a Change Node  and Configure as below . The Configuration shows that it will pass the data from msg.payload to flow.firstString

After this Step , you will be able to use “firstString” Variable within the Flow

Step 3

Connect the Nodes together.

Now you have completed the node where when trigger it will inject ” This is A String ” to the msg.payload. The Change Node will read the msg.payload Value

and Pass the Value into the flow Variable “firstString”

Step 4

Retrieve the  flow Variable “firstString” Value

Drag out the inject Node and Configure as below .

The Inject Node will pass the  “firstString “flow Variable value into msg.payload

 

Step 5

Drag Out a Debug Node , and configure as Below

Step 6

Joint  the Nodes, trigger both inject node and Observe the results

Results

Method 2

For Method 2 I will use Function node to Set / declare the Flow Variable

Step 1

Drag out and inject Node  and Configure as Below

Pass a String Value ” This is A String 3 ” to the msg.payload

Step 2

Drag Out a Function Node  and Configure as below . The Configuration shows that it will pass the data from msg.payload to flow.secondString

After this Step , you will be able to use “secondString” Variable within the Flow.

Copy and paste the code below and paste it into the Text Area inside the  “On Message ” Tab

Code as below

flow.set(‘secondString’, msg.payload)
return msg;

Step 3

Connect the Nodes together.

Now you have completed the node where when trigger it will inject ” This is A String 3 ” to the msg.payload. The Function Node will read the msg.payload Value

and Pass the Value into the flow Variable “secondString”

 

Step 4

Retrieve the  flow Variable “secondString” Value

Drag out the inject Node and Configure as below .

The Inject Node will pass the  “secondString “flow Variable value into msg.payload

 

Step 5

Drag Out a Debug Node , and configure as Below

 

Step 6

Joint  the Nodes, trigger both inject node and Observe the results

Results

 

check out

How To Extract JSon Values From NodeRed JSon Object

Leave a Reply

Your email address will not be published. Required fields are marked *

11 − five =