Return only latest max number in message


#1

I have a couple of function nodes in which I am using context in order to compare the current with older message received and check which one is greater than the other and save the new value to node context in case it is the case.

What I am trying to achieve with the code below is to bring the latest value preserved with context when the button is off, but the problem is that I never receive msg in case the button is off, I guess I am ding something wrong here especially with the returning the msg part.

var prevlit = context.get('prevlit')||0;
    if(prevlit !== 0)
    {
        if(prevlit > msg.payload)
        {
            context.set('prevlit',prevlit);
            msg.payload = prevlit;
        }
        else
        {
            context.set('prevlit',msg.payload);
        }
        
    }
    else
    {
        context.set('prevlit',msg.payload);
    }

if (context.global.stat == "off")
{
    return msg; //I want to return the value here when the status is off
}

Thank you for your help with this :slight_smile:


#2

You can use node.warn(“message”+variable);
to pass a message from the function to the debug panel

If you have a function that’s not doing what you want, place node.warn statements through your code so you can “see” messages passing through your function


#3

Try changing this part of the code. Make sure the global variable is set to "off" somehwere in your flow.

if (global.get("stat") == "off")
{
    return msg; //I want to return the value here when the status is off
}

#4

Thank you Andrei, it was declared on another node, I have followed different approach to have a function node directly connected from switch node in order to detect only the change when the switch/btn is changed.


#5

Thank you ukmoose, I have tried but there were no problem to detect, so I have managed to resolve it with different approach.