logo elektroda
logo elektroda
X
logo elektroda

Registration of maximum and minimum temperature in Node-Red: function and negative values

starob 1260 6
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 19307311
    starob
    Level 29  
    I have a noda with a function as below.
    I need to register the maximum and minimum temperature that occurs during the day.
    The function works up to the first occurrence of a negative temperature, the first "minus" is registered and then already the condition t < TMin is not fulfilled.
    What else do I not know? ..I would appreciate your help

    [syntax=javascript]
    var t = (msg.payload*1).toFixed (1);
    var Tmax = flow.get("tempMax");
    var Tmin = flow.get("tempMin");
    node.warn("t="+t);
    node.warn("min="+Tmin);
    node.warn("max="+Tmax);
    
    
    if (t > Tmax){
      flow.set("tempMax",t);  
      node.warn("=max");
    }
    if (t < Tmin){
       flow.set("tempMin",t); 
      node.warn("=min");
    }
    
    return msg;
    [/syntax]
    
    .
  • ADVERTISEMENT
  • Helpful post
    #2 19309943
    xury
    Automation specialist
    The script will not work at all. The reason is that you first read the contents of e.g. flow.get("tempMax"), and if there is no such thing then the variable becomes undefined. Secondly, in order for the comparison operators to work properly, both sides have to be numbers and of the float type (negative data)
    Code: Javascript
    Log in, to see the code
  • ADVERTISEMENT
  • #3 19310835
    starob
    Level 29  
    xury wrote:
    Secondly, in order for the comparison operators to work properly, both sides must be numbers and of type float (negative data)
    .
    That's what I sensed, hence trying to do some arithmetic operation in the hope that it would make a difference (msg.payload*1).toFixed (1)
    node.warn() displayed the correct values, so I deduced the problem was in the "if" conditions.
    Here was a strange issue because from the tests and results the condition operates on absolute values - it buys negative values and treats them as abs.

    xury wrote:
    The script will not work at all. Reason being that you first read the contents of e.g. flow.get("tempMax"),
    .
    I didn't say that, I set the initial conditions in "inject" after the flow starts and at midnight.
    Thanks to this, I found out that you can do so :)
    if (flow.get("tempMax") === undefined){
    flow.set("tempMax",t);
    For which many thanks

    Explain perhaps why you didn't use parseFloat() in Tmax = flow.get("tempMax") - I inserted for all three variables just to be sure;

    Last question: it would seem that such a simple function can be "clickable" with a switch noda?
    I tried that in the beginning, but it worked (i.e. doesn't work) as well as the function.
  • ADVERTISEMENT
  • Helpful post
    #4 19311529
    xury
    Automation specialist
    I didn't use it, because in principle this variable t can't change itself to another type. But if you gave it that, it wouldn't hurt.
    I actually use the node switch very little too. In node function you can also do multiple outputs. In function you can do many actions at once which saves the amount of node.

    Added after 8 [hours] 2 [minutes]: .

    Same thing only a little shorter written:
    Code: Javascript
    Log in, to see the code
    .
    And at midnight send this:
    Code: Javascript
    Log in, to see the code
    .
  • #5 19312961
    starob
    Level 29  
    You probably don't remember, but it was you who quite recently persuaded me to switch from Domoticz to HA and node-red.
    So I am a beginner and hence my elementary questions.
    The code is neat, I like it and it has taught me something new. Intuitively I can feel what it's about and I'll get the details.
    I don't really understand this:
    t > Tmax && flow.set("tempMax",t);
    but I'm more puzzled by this:
    flow.set(["tempMax", "tempMin"],[void 0,void 0]);
    Not in terms of the correctness of the action itself, but the correctness of the algorithm.
    That's my thinking out loud, so if you don't want to you don't have to answer.
    .. at midnight sets max/min to a "non-typical" 0st?

    .. what happens if the measured running temperature over the course of the day never goes past zero (summer, big brain) - one of the values will be a permanent zero (which will not be true)?

    .. seems to me that it should be set to current t , not zero?
  • ADVERTISEMENT
  • #6 19313452
    xury
    Automation specialist
    It doesn't set to an uncharacteristic 0 just deletes the flows.
    That is, as it is after a reboot, before flow.set takes effect.
    As for the first action it is similar to bash
    If the comparison t>Tmax is true then it is executed after &&.
    If it is false it will not execute. It's just like if only for me it's more visual.
  • #7 19313572
    starob
    Level 29  
    Comprehension... probably like any language there are lots of similar "tricks" :) .
    When you don't do it professionally, just ad hoc for yourself, you won't know them and in the end you will forget anyway.
    Many thanks. The topic is closed.

Topic summary

The discussion revolves around a Node-Red function designed to register maximum and minimum temperatures throughout the day. The initial implementation faced issues when negative temperatures were encountered, causing the function to fail in updating the minimum temperature. Users suggested improvements, including ensuring that variables are defined and using `parseFloat()` to maintain numeric types. The final solution involved using logical operators for concise comparisons and resetting temperature values at midnight to avoid incorrect readings. The importance of initializing temperature values and handling negative inputs was emphasized, along with the potential for using a switch node for better control.
Summary generated by the language model.
ADVERTISEMENT