Function node help

Hi

I must be missing something simple but i am trying to return from the below number to zero decimal places, nothing i try seems to work, any suggestion appreciated

const numbers = [bedroomtemp, bedroomventtemp, loungeventtemp, loungetemp];
let max = Math.max.apply(null, numbers);
let min = Math.min.apply(null, numbers);

What output are you expecting?

The main and max of 4 numbers as integer?
The values and name of which variable was min/max?
The result as an array? Object?

A combination of Array.map & Math.round or Math.ceil will probably be what you want.

I am expecting two variables, one "min" and one "max". I tried Math.round but could't get it to work, quite convinced it was a case of incorrect formatting when i typed it in

image

//dummy data - update as required
const bedroomtemp = 21.2, bedroomventtemp=20.9, loungeventtemp=23.123, loungetemp=18.321
const numbers = [bedroomtemp, bedroomventtemp, loungeventtemp, loungetemp]
msg.payload = {
  max: Math.round(Math.max.apply(null, numbers)),
  min: Math.round(Math.min.apply(null, numbers))
}
return msg

thanks, copied your code and changed to make it how i need it but can't get it through to the next node. If i send msg.control = {min:10, max:80} it works fine and the debug gives me the below image

image

whne i try the round statemement it give me this

image

const numbers = [bedroomtemp, bedroomventtemp, loungeventtemp, loungetemp]
msg.control = {
minmax: {min:(Math.round(Math.min.apply(null, numbers))), max:(Math.round(Math.max.apply(null, numbers)))}

}
return msg

//msg.control = {min:10, max:80}

if thats what you need, then dont add the minmax property!

const numbers = [bedroomtemp, bedroomventtemp, loungeventtemp, loungetemp]
msg.control = { 
  min: (Math.round(Math.min.apply(null, numbers))), 
  max:(Math.round(Math.max.apply(null, numbers)))
}
return msg

or if it makes more sense to you...

const numbers = [bedroomtemp, bedroomventtemp, loungeventtemp, loungetemp]
msg.control = { };
msg.control.min = Math.round(Math.min.apply(null, numbers)); 
msg.control.max = Math.round(Math.max.apply(null, numbers));
return msg
1 Like

Thanks, works perfectly and the second option made it clear in my head, don't do much of this and sometimes can't see the wood for the trees

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