# Variable number in a flow

**URL:** <https://discourse.nodered.org/t/variable-number-in-a-flow/90051>\
**Category:** General\
**Created:** [8 August 2024 13:12 UTC](https://discourse.nodered.org/t/variable-number-in-a-flow/90051 "2024-08-08T13:12:15Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dacron](https://avatars.discourse-cdn.com/v4/letter/d/aca169/32.png) [@Dacron](https://discourse.nodered.org/u/Dacron)\
**Post date:** [8 August 2024 13:12 UTC](https://discourse.nodered.org/t/variable-number-in-a-flow/90051/1 "2024-08-08T13:12:15Z")

</div>

I have a number in a switch node that sends the payload one of two ways depending if the incoming number is higher or lower than the set value. I would like to be able to update the set value to a number from another node. Thanks for any help.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [8 August 2024 13:23 UTC](https://discourse.nodered.org/t/variable-number-in-a-flow/90051/2 "2024-08-08T13:23:23Z")

</div>

You can have the other node set a context var [Working with context : Node-RED](https://nodered.org/docs/user-guide/context)  
You can then use the context var in the switch node as the property, and compare this to the incoming msg property.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 August 2024 13:23 UTC](https://discourse.nodered.org/t/variable-number-in-a-flow/90051/3 "2024-08-08T13:23:39Z")

</div>

Set a context variable eg `flow.compare` to the currently chosen value. Your switch node can compare against this variable.

---

<div class="post-metadata">

**Author:** ![Dacron](https://avatars.discourse-cdn.com/v4/letter/d/aca169/32.png) [@Dacron](https://discourse.nodered.org/u/Dacron)\
**Post date:** [8 August 2024 14:25 UTC](https://discourse.nodered.org/t/variable-number-in-a-flow/90051/4 "2024-08-08T14:25:29Z")

</div>

Brilliant thank you

---

<div class="post-metadata">

**Author:** ![Dacron](https://avatars.discourse-cdn.com/v4/letter/d/aca169/32.png) [@Dacron](https://discourse.nodered.org/u/Dacron)\
**Post date:** [8 August 2024 17:52 UTC](https://discourse.nodered.org/t/variable-number-in-a-flow/90051/5 "2024-08-08T17:52:42Z")

</div>

Sorry if I have this wrong but context doesn't seem to do what I want. I have a value of temperature that changes at the beginning of the flow going through a switch node which filters the value depending if it's over or under lets say 10. I would like to change the 10 to the value of another node, so the temperature value is filtered against 3 or 6 what ever the independent nodes value changes to. Hope that makes sense.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [8 August 2024 18:21 UTC](https://discourse.nodered.org/t/variable-number-in-a-flow/90051/6 "2024-08-08T18:21:53Z")

</div>

Should work fine. You must be doing something wrong. Hard to say what with no information. Would probably be best to give an example flow.

Have a look at this example

```auto
[{"id":"39ee97df2f51c895","type":"inject","z":"d1395164b4eec73e","name":"incoming temp","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":340,"y":200,"wires":[["c1106d2c5ed7de62"]]},{"id":"c1106d2c5ed7de62","type":"switch","z":"d1395164b4eec73e","name":"","property":"temp_setpoint","propertyType":"flow","rules":[{"t":"lt","v":"payload","vt":"msg"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":530,"y":200,"wires":[["94a2d519aada64c9"],["ba36b9b9e554c9d4"]]},{"id":"94a2d519aada64c9","type":"debug","z":"d1395164b4eec73e","name":"true","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":690,"y":200,"wires":[]},{"id":"ba36b9b9e554c9d4","type":"debug","z":"d1395164b4eec73e","name":"otherwise","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":700,"y":240,"wires":[]},{"id":"401acd18ae3f288f","type":"inject","z":"d1395164b4eec73e","name":"another node 20","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"num","x":320,"y":120,"wires":[["4ea68427e1e8a0e0"]]},{"id":"5382b5fa4d8cd7f0","type":"inject","z":"d1395164b4eec73e","name":"another node 5","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"num","x":320,"y":80,"wires":[["4ea68427e1e8a0e0"]]},{"id":"4ea68427e1e8a0e0","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"temp_setpoint","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":80,"wires":[[]]}]

```

[How to import/export a flow](https://nodered.org/docs/user-guide/editor/workspace/import-export)

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![Dacron](https://avatars.discourse-cdn.com/v4/letter/d/aca169/32.png) [@Dacron](https://discourse.nodered.org/u/Dacron)\
**Post date:** [8 August 2024 18:39 UTC](https://discourse.nodered.org/t/variable-number-in-a-flow/90051/7 "2024-08-08T18:39:08Z")

</div>

My problem is I can't code I just can't get my head around it. I love Node Red because I can use the Nodes and don't have to know code. I'll see if I can figure out how to post the code

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 August 2024 19:09 UTC](https://discourse.nodered.org/t/variable-number-in-a-flow/90051/8 "2024-08-08T19:09:12Z")

</div>

> [@Dacron](#):
>
> a switch node which filters the value depending if it's over or under lets say 10. I would like to change the 10 to the value of another node, so the temperature value is filtered against 3 or 6 what ever the independent nodes value changes to. Hope that makes sense.

I understand what you mean.  
However it does not make sense to speak of "the value of a node".  
Consider this flow:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/1/d1e9a65cc50d040679dcc99240efdfc6ef50d73e.png)

Any payload which emerges from the change node may have the value 42 but once the message has gone down the wire, it ceases to exist.  
The number 42 does not remain magically attached to the change node until you want to refer to it.

In contrast, a context variable _does_ retain it's value from one message to the next.  
That is why @E1cid's flow uses flow.temp\_setpoint to save the value output by "another node".

---

<div class="post-metadata">

**Author:** ![Dacron](https://avatars.discourse-cdn.com/v4/letter/d/aca169/32.png) [@Dacron](https://discourse.nodered.org/u/Dacron)\
**Post date:** [8 August 2024 20:43 UTC](https://discourse.nodered.org/t/variable-number-in-a-flow/90051/9 "2024-08-08T20:43:09Z")

</div>

![IMG_0274](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/1/31409f0ad1692d13470654791aff447d3afe68f9.jpeg)  
I understand, I want to change the 0.00 in this node for a number from another node which changes.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 August 2024 22:44 UTC](https://discourse.nodered.org/t/variable-number-in-a-flow/90051/10 "2024-08-08T22:44:42Z")

</div>

Do you know how to import @E1cid's example?  
Click on this button in his post above

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

Then in Node-red Ctrl-i to import  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/4/142ba010aaa25a64a6932a19ec3d5d94d6034df6.png)

Paste and import, deploy.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/3/439e193c697ecd0c3c9e4d904ae8b4b5b136f8fd.png)

Will give you this flow

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/c/7c2ff6edd22f827df1c1317510c455555fdcad01.png)

It demonstrates that

- You can set a flow context variable to whatever number you like, with 5 and 20 as examples.
- A switch node can compare msg.payload with the value currently held in the context variable.

> [@Dacron](#):
>
> I understand, I want to change the 0.00 in this node for a number from another node which changes.

I think that the example flow does precisely that?

---

<div class="post-metadata">

**Author:** ![Dacron](https://avatars.discourse-cdn.com/v4/letter/d/aca169/32.png) [@Dacron](https://discourse.nodered.org/u/Dacron)\
**Post date:** [9 August 2024 16:26 UTC](https://discourse.nodered.org/t/variable-number-in-a-flow/90051/11 "2024-08-09T16:26:57Z")

</div>

Thanks to all your help ! I've figured it out E1cid your example is perfect I'm just a little slow 😃 Thanks again

---

<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:** [23 August 2024 16:27 UTC](https://discourse.nodered.org/t/variable-number-in-a-flow/90051/12 "2024-08-23T16:27:54Z")

</div>

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