# \`Range\` node setting values from variables

**URL:** https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428
**Category:** General
**Created:** [11 August 2020 08:35 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428 "2020-08-11T08:35:20Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [11 August 2020 08:35 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/1 "2020-08-11T08:35:20Z")

</div>

I am wanting to use a `range` node but the scales need to be set by external values.

I don't mind doing all this with a `function` node, but thought I would ask.

(`map` is the command to basically do what the `range` node does)  
(Ok, no it isn't the command.)  
Someone?  
`scale`? `index`?

---

<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: [11 August 2020 08:40 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/2 "2020-08-11T08:40:19Z")

</div>

The help for the Range node tells you what properties it responds to.

In this instance, you'll see it only uses `msg.payload` - there is no way to configure its behaviour using message properties.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [11 August 2020 08:41 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/3 "2020-08-11T08:41:20Z")

</div>

I thought so.

So I am going to have to make a `function` node to do the work - yes?  
Now I'm stuck on what the command is to do what the node does.

---

<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: [11 August 2020 08:45 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/4 "2020-08-11T08:45:32Z")

</div>

It depends on which mode of the Range node you're interested in, but you can see the code for the basic range function here: [https://github.com/node-red/node-red/blob/master/packages/node\_modules/@node-red/nodes/core/function/16-range.js#L43](https://github.com/node-red/node-red/blob/master/packages/node_modules/@node-red/nodes/core/function/16-range.js#L43)

Where `n` is the value being ranged (ie `msg.payload`), and the min/max of the input range is specified by `node.minin` and `node.maxin` and the output range is `node.minout` and `node.maxout`

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [11 August 2020 08:46 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/5 "2020-08-11T08:46:57Z")

</div>

Thanks, that sounds like the node at which I am looking.

Was trying to find the complete name and look at the code, but hadn't found the name before you replied.

Thanks again.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [11 August 2020 09:10 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/6 "2020-08-11T09:10:25Z")

</div>

From the code in the link, I tried to reverse engineer the maths.

This is a test flow which is not quite _cutting it_.

```auto
[{"id":"692eb68e.19b71","type":"function","z":"3caae529.4226aa","name":"","func":"var maxin = parseInt(msg.maxin);\nvar minin = parseInt(msg.minin);\nvar maxout = parseInt(msg.maxout);\nvar minout = parseInt(msg.minout);\nvar x = parseInt(msg.payload);\n\nvar divisor = maxin - minin;\nvar n = ((x - minin) % divisor + divisor) % divisor + minin;\n\nvar value = ((n - minin) / (maxin - minin) * (maxout - minout)) + minout;\n\nmsg.payload = value;\n\n\n\n\nreturn msg;","outputs":1,"noerr":0,"x":570,"y":1530,"wires":[["600b5a5e.b66c14"]]},{"id":"e3295788.2fa6d","type":"change","z":"3caae529.4226aa","name":"Limits","rules":[{"t":"set","p":"maxin","pt":"msg","to":"10","tot":"num"},{"t":"set","p":"minin","pt":"msg","to":"1","tot":"num"},{"t":"set","p":"maxout","pt":"msg","to":"100","tot":"num"},{"t":"set","p":"minout","pt":"msg","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":1530,"wires":[["692eb68e.19b71"]]},{"id":"600b5a5e.b66c14","type":"debug","z":"3caae529.4226aa","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":740,"y":1530,"wires":[]},{"id":"fdeff99.4adb008","type":"inject","z":"3caae529.4226aa","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":1470,"wires":[["e3295788.2fa6d"]]},{"id":"d2bb109f.59a5f8","type":"inject","z":"3caae529.4226aa","name":"","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":1510,"wires":[["e3295788.2fa6d"]]},{"id":"6107f5ed.008bfc","type":"inject","z":"3caae529.4226aa","name":"","topic":"","payload":"3","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":1550,"wires":[["e3295788.2fa6d"]]},{"id":"ae397d2f.9cb35","type":"inject","z":"3caae529.4226aa","name":"","topic":"","payload":"7","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":1590,"wires":[["e3295788.2fa6d"]]}]

```

The important stuff is in the `function` node - of course.  
The rest is just showing how I test it, and to have all cards on the table.

Thanks again in advance.

---

<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: [11 August 2020 09:16 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/7 "2020-08-11T09:16:07Z")

</div>

Which mode of the Range node are you trying to replicate?

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [11 August 2020 09:19 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/8 "2020-08-11T09:19:43Z")

</div>

The one which comes in NR as a _stock_ node.....

I don't know how to get it's _full_ name.

 ![Screenshot from 2020-08-11 19-18-45](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/f/1fb8b04b612ff79eb5ca883e00370e538444e5b8.png)

This one. If that is any help.

---

<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: [11 August 2020 09:21 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/9 "2020-08-11T09:21:44Z")

</div>

Yes, I know which node you are talking about - I pointed you at its source code.

The node offers three different options for its behaviour:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/b/7b5c3264c67bedc5c63ae8ed343c2c7b93730f4a.jpeg)

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [11 August 2020 09:24 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/10 "2020-08-11T09:24:51Z")

</div>

(Blush)

Yes, but I want to adjust the `maxin` `maxout` `minin` and `minout` values.

And after looking at it again and changing the `minout` value in the flow I posted, I think it does what I want.  
I stupidly had the values set as:  
`minin``1`  
`maxin` `10`  
`minout``1`  
`maxout``100`

When `minout` should be `10` for simple translation.

It isn't crucial (well it probably will be because I said it won't be) it is just for testing something else.

---

<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: [11 August 2020 09:27 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/11 "2020-08-11T09:27:48Z")

</div>

I understand you want to change the range values, but without know what type of range function you want, we cannot help you get the algorithm right.

But it sounds like you've figured it out yourself.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [11 August 2020 09:28 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/12 "2020-08-11T09:28:55Z")

</div>

Yes, and again: I am sorry, I also mis-parsed your saying:

> The node offers three different options for its behaviour:

I didn't understand what that means/meant.

---

<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: [12 August 2020 07:43 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/13 "2020-08-12T07:43:10Z")

</div>

What have you got in your function node now?

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [12 August 2020 07:47 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/14 "2020-08-12T07:47:47Z")

</div>

Sorry, it isn't that simple.

The idea is that I get the temperature and _range_ that to control the colour of a NeoPixel LED.

Alas I can't post the flow/code because the machine is now shut down.

If you want next time I have it turned on, I could copy/pate it for you.

---

<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: [12 August 2020 08:05 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/15 "2020-08-12T08:05:39Z")

</div>

I was just interested in the function node you have built that emulates a Range node but with variable range.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [12 August 2020 10:06 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/16 "2020-08-12T10:06:49Z")

</div>

Ok, this is what it is....

Given it receives the temperature and it is sending a message for a Neopixel LED.

```auto
[{"id":"5429065c.0700c","type":"change","z":"3caae529.4226aa","name":"Limits","rules":[{"t":"set","p":"minin","pt":"msg","to":"FanStop","tot":"global"},{"t":"set","p":"maxin","pt":"msg","to":"shutdown_temperature","tot":"global"},{"t":"set","p":"minout","pt":"msg","to":"30","tot":"num"},{"t":"set","p":"maxout","pt":"msg","to":"255","tot":"num"},{"t":"set","p":"Temperature_LED","pt":"msg","to":"Temperature_LED","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":590,"wires":[["eeef5cd5.74fb2"]]},{"id":"eeef5cd5.74fb2","type":"function","z":"3caae529.4226aa","name":"Scale for colour","func":"var maxin = parseInt(msg.maxin);\nvar minin = parseInt(msg.minin);\nvar maxout = parseInt(msg.maxout);\nvar minout = parseInt(msg.minout);\nvar x = parseInt(msg.payload);\n\nif (msg.payload < minin)\n{\n msg.payload = 0;\n return msg;\n}\n\nvar divisor = maxin - minin;\nvar n = ((x - minin) % divisor + divisor) % divisor + minin;\n\nvar value = parseInt(((n - minin) / (maxin - minin) * (maxout - minout)) + minout);\n\nmsg.payload = value;\n\n//msg.Temperature_LED = flow.get(\"Temperature_LED\");\n\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":540,"wires":[["d1bb6aed.c23948"]]}]

```

Though I don't think that part is in this bit.

---

<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: [12 August 2020 10:51 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/17 "2020-08-12T10:51:28Z")

</div>

OK, thanks. I see you are using it like a Range node in wrap-around mode. There is a very small efficiency saving you could make by using `divisor` rather than `maxin-minin` in line 16.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [12 August 2020 10:53 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/18 "2020-08-12T10:53:37Z")

</div>

Well, I looked at the link @knolleary gave me.

I am a bit happy with myself in that I _back engineered_ the maths to get the desired result.

I may look at it later.  
Thanks.

---

<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 October 2020 10:53 UTC](https://discourse.nodered.org/t/range-node-setting-values-from-variables/31428/19 "2020-10-11T10:53:38Z")

</div>

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