Scaling Question

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?

With a bit of work. :rofl:

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!

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

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;

Well if you are trying to

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

1 Like

Something like this:

/// 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)
1 Like

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