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)`
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 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)
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.
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.
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. 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.