Function node behaving strangely trying to set a maximum value

I have weather data from a http request node going into a function node. The function node splits the data to display on my dashboard. I also use the function node to set min and max temperatures (that seems to work ok) and a maximum wind gust. The wind gust coming into the function node is in km/h, so I have to convert this to mph. I write the windgust max into a flow variable. This gets reset at midnight (not from within the function node). Now, every time a windgust is higher than before, it should update the windgustmax. But, thats where I have a problem. I see some weird behavior. Looking at this debug, first windgust is 10.2 mph, which sets windgustmax to 10.2 mph. So far so good. Next reading is 8.9 mph. Why does my windgustmax go to 8.9 mph ? This is where I am struggling to understand. On windy days. windgustmax gets stuck at 9.8, even though I can see higher readings. This is how I noticed something was not right.
My feeling is that ithe problem could be the incoming data format. I tried setting up a copy of the function node with a few inject nodes to test. But when I am sending values to the function node I have errors TypeError: Cannot read properties of undefined (reading 'toFixed')
These errors do not occur when using the http request node

This is the function node:

var solarRadiation = msg.payload.observations[0].solarRadiation;
var windgustmax = flow.get('windgustmax') ?? 0;
var tempmax = flow.get('tempmax') ?? -20;
var tempmin = flow.get('tempmin') ?? 50;
var uv = msg.payload.observations[0].uv;
var humidity = msg.payload.observations[0].humidity;
var temp = msg.payload.observations[0].uk_hybrid.temp;
var windChill = msg.payload.observations[0].uk_hybrid.windChill;
var windSpeed = msg.payload.observations[0].uk_hybrid.windSpeed;
var windGust = msg.payload.observations[0].uk_hybrid.windGust;
var pressure = msg.payload.observations[0].uk_hybrid.pressure;
var precipRate = msg.payload.observations[0].uk_hybrid.precipRate;
var precipTotal = msg.payload.observations[0].uk_hybrid.precipTotal;

windSpeed = (windSpeed / 1.609).toFixed(1)
windGust = (windGust / 1.609).toFixed(1)
precipTotal = precipTotal.toFixed(1)

if (windGust > windgustmax) {
    flow.set("windgustmax",windGust)
    windgustmax = windGust
}

if (temp > tempmax) {
    flow.set("tempmax",temp)
    tempmax = temp
}

if (temp < tempmin) {
    flow.set("tempmin", temp)
    tempmin = temp
}

var msg1 = { payload: {value: solarRadiation, status: solarRadiation + " watts/m²"}};
var msg2 = { payload: {value: uv}};
var msg3 = { payload: { value: humidity, status: humidity + " %", name: "Humidity Weather Station"}};
var msg4 = { payload: {value: temp, status: temp + " °C", name: "Temp Weather Station"}};
var msg5 = { payload: {value: windChill, status: windChill + " °C"}};
var msg6 = { payload: {value: windSpeed, status: windSpeed + " mph", name: "Wind Speed"}};
var msg7 = { payload: {value: windGust, status: windGust + " mph", name: "Wind Gust"}};
var msg8 = { payload: {value: pressure, status: pressure + " mb"}};
var msg9 = { payload: {value: precipRate, status: precipRate*25.4 + " mm", name: "Rain Rate"}};
var msg10 = { payload: {value: precipTotal, status: precipTotal*25.4 + " mm", name: "Rain Total"}};
var msg11 = { payload: { value: windgustmax, status: windgustmax + " mph", name: "Wind Gust Max" } };
var msg12 = { payload: { value: tempmin, status: tempmin + " °C", name: "Temp Min" } };
var msg13 = { payload: { value: tempmax, status: tempmax + " °C", name: "Temp Max" } };

if (msg.statusCode == 200) {
return [msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9, msg10, msg11, msg12, msg13];
}

And this is how the data is coming in:

The values are strings, not numbers, so the test does an alphabetic compare rather than numeric. "8.9" is greater then "10.2" because "8" is greater than "1". Think of the equivalent in letters. If instead of "10.2" it was "ba.c" and instead of "8.9" it was "g.h" then you can see that an alphabetic test will say that ba.c is less that g.h because b is less than g.

You can convert a string to a number using
let value = Number(stringValue)

Your data arrives as numbers but you are converting it to strings by using toFixed(). Don't!

windSpeed = (windSpeed / 1.609).toFixed(1)
windGust = (windGust / 1.609).toFixed(1)
precipTotal = precipTotal.toFixed(1)
1 Like

Ok, I have changed that part of the function node, that should work better ?

windSpeed = (windSpeed / 1.609);

windSpeed = Math.round((windSpeed) * 10) / 10;

windGust = (windGust / 1.609);

windGust = Math.round((windGust) * 10) / 10;

precipTotal = Math.round((precipTotal) * 10) / 10;`

I think that should work. You might need to delete the flow variables to get rid of the currently stored strings.

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