# Scaling Question

**URL:** <https://discourse.nodered.org/t/scaling-question/70667>\
**Category:** General\
**Created:** [15 November 2022 19:37 UTC](https://discourse.nodered.org/t/scaling-question/70667 "2022-11-15T19:37:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aallen](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@aallen](https://discourse.nodered.org/u/aallen)\
**Post date:** [15 November 2022 19:37 UTC](https://discourse.nodered.org/t/scaling-question/70667/1 "2022-11-15T19:37:01Z")

</div>

I am trying to use the "Range" node with the "Scale the message property action,  
how can I write a function that serves the exact same purpose, but with variable target range?

---

<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:** [15 November 2022 21:47 UTC](https://discourse.nodered.org/t/scaling-question/70667/2 "2022-11-15T21:47:37Z")

</div>

> [@aallen](#):
>
> how can I write a function that serves the exact same purpose, but with variable target range

With a bit of work. 🤣

One thought would be to store the input range in one flow variable (like in.low and low.high) and the output range in another flow variable (like out.low and out.high). Then in your function read in those values and compute the size of the in and the size of the out. Divide the in-size into the out-size, Then take the input number and mutiply it by the out-size to get the result.

Of course you would have to test that the input value is not outside in-range you specify.

You could also examine the code in the range node to see what it does.  
See if there is a javascript libraru that will help ou  
Do a google search to see if there is a math mathematical formular that will do it.

I can't wait to see what you come up with!

---

<div class="post-metadata">

**Author:** ![aallen](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@aallen](https://discourse.nodered.org/u/aallen)\
**Post date:** [15 November 2022 21:51 UTC](https://discourse.nodered.org/t/scaling-question/70667/3 "2022-11-15T21:51:14Z")

</div>

I'm pretty noob when it comes to NR, so this probably isnt formatted in an optimal way. But it sort of works. There is some limits that I could program but this gets the point across

```auto
msg.OldMin = 0;
msg.OldMax = 14;
msg.NewMin = msg.pH_SP - 0.5;
msg.NewMax = msg.pH_SP + 0.5;

msg.Scaled_pH = (msg.NewMin) + (((msg.pH_Value - msg.OldMin) * (msg.NewMax - msg.NewMin)) / (msg.OldMax - msg.OldMin));

return msg;

```

---

<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:** [15 November 2022 22:12 UTC](https://discourse.nodered.org/t/scaling-question/70667/4 "2022-11-15T22:12:19Z")

</div>

Well if you are trying to

> [@aallen](#):
>
> write a function that serves the exact same purpose

then you need to test the input value to make sure it is within the valid input range.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [16 November 2022 04:12 UTC](https://discourse.nodered.org/t/scaling-question/70667/5 "2022-11-16T04:12:29Z")

</div>

Something like this:

```auto
/// not tested

const scale = (value, x1, y1, x2, y2) => (value - x1) * (y2 - x2) / (y1 - x1) + x2;

const input_ph = msg.pH_Value 
const in_min = 0;
const in_max = 14;
const out_min = msg.pH_SP - 0.5;
const out_max = msg.pH_SP + 0.5;

msg.scaled_ph = scale(input_ph, in_min, in_max, out_min, out_max)

```

---

<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:** [15 January 2023 04:12 UTC](https://discourse.nodered.org/t/scaling-question/70667/6 "2023-01-15T04:12:52Z")

</div>

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