Setting maximum temperature only works above 0 degrees

I am storing daily minimum and maximum temperatures in a flow variable. I have a simple function node to store the temperatures. At midnight, I reset them. I set the minimum temperature to +50 degrees, the max to -20 degrees. That way, the first temerature reading after midnight should reset both to that reading. It works, but if the temperature is below 0 it doesnt, the maximum stays at -20, which is strange.
This is the function node

var tempmax = flow.get('tempmax') || -20;

var tempmin = flow.get('tempmin') || 50;

if (temp > tempmax) {

flow.set("tempmax",temp)

}

if (temp < tempmin) {

flow.set("tempmin", temp)`

Is temp a number or a string?

I think it comes in as a value from the api

OK, well that's a number, as it should be.

Can you please post your complete function code, what you posted above seems to be just an extract.

Here it is:

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)
}

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

if (temp < tempmin) {
    flow.set("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];
}

I cut it down to just the minimum temperature stuff and it seems to set flow.tempmin correctly for me.
But I notice you have this:

if (temp < tempmin) {
    flow.set("tempmin", temp)
}
. . .
var msg12 = { payload: { value: tempmin, status: tempmin + " °C", name: "Temp Min" } };

Should you perhaps have this?

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

Which weather API is this using?

It is using the weather underground api

but I am setting tempmin and tempmax before by flow.set ? And the tempmin is working without a tempmin = temp (so is tempmin if it is above 0)
After midnight, as it was today, with temperatures below 0, tempmax stayed at -20. In the morning, the first time it was above 0 (at +0.1), tempmax updated to +0.1

I don't know if this is the problem, it just looked strange to me.

flow.set("tempmin", temp) sets flow.tempmin. It doesn't set the var tempmin.

Contrariwise, I think

var tempmin = flow.get('tempmin')
tempmin = 55

does set flow.tempmin to 55.
Not good programming style to rely on it though

I just setup a test flow, with a few inject nodes, and noticed something slightly odd. Injecting with a temperature, the output of the function node seems to lag 1 value behind of what I injected. After adding a tempmin = temp and a tempmax = temp, it was sending the right one immediately. So it definetely needed to be in there. Maybe that was causing the issue, although I would have thought I had more readings below 0 after midnight (by the time I checked it was 8am), so it should not have stayed at -20.
Anyway I'll add that to my proper function node now as it needs to be in there. I also added a windgustmax = windGust to that code as that should probably in there too.

It isn't anything to do with the problem, but you can't do it that way. Consider what would happen if flow.get("tempmax") returned zero. It would set tempmax to -20, which I guess is not what you want. Instead you can do
var tempmax = flow.get("tempmax") ?? -20
and the same for the second one. The older way of doing this is to test for undefined but the new Nullish coalescing operator (??) makes this easier.

Also, nowadays, you should be using let or const not var. (google it if you don't know about const and let)

1 Like

Ok thanks for those pointers. I will google var and cons and let. So you think I should change my function nodes from var ? I have quite a few, and use var a lot. But I only started Node Red a month ago, so still learning

I just can't bring myself to do that. Let variablename is too much like an incomplete BASIC statement.
In the list of infuriating syntax decisions it's right up there with indentation as syntax in Python.
Yuk.

Actually, I think this is exactly what happend. When I looked at my min max temps, it was 0 ! So that would then have set it to -20 ?

I learned the hard way about that, I had a bug that only very occasionally appeared, it took me ages to find. The syntax you have is very common and it is fine provided the value at the end is zero, or the value to the left can never be zero (or false). Safer now to use ?? everywhere.

I wouldn't worry about changing existing functions to let and const, in practice it very rarely makes a difference. It is a good habit to get into though.

@jbudd I agree that the word let is horrible. Not much we can do about that though.

Have just goodled let and const. I finally understand the difference (also to var). Very useful

Haha, when I was using Python regularly, I much preferred that to the horrid, over complex (as I saw it) Pascal style syntax with all of the {...} everywhere. :slight_smile: Since anyone with any sense always indents their code anyway, all you needed was a code editor that knew the difference between tab and spaces.

Good call! I keep forgetting about that. Just be careful if writing front-end code as well since not all browsers yet support it. But now we've moved onto node.js v14 as the minimum, things like this are getting much better. Can't wait for node-red's next major release to move to node.js v18 where we should finally get top-level await as well.

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