Variable number in a flow

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.

You can have the other node set a context var Working with context : Node-RED
You can then use the context var in the switch node as the property, and compare this to the incoming msg property.

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

Brilliant thank you

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.

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

[{"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

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 ```)

``` 
   code goes here 
```

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

See this post for more details - How to share code or flow json

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

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

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".


I understand, I want to change the 0.00 in this node for a number from another node which changes.

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

Then in Node-red Ctrl-i to import
image

Paste and import, deploy.

Will give you this flow

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.

I think that the example flow does precisely that?

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

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