Return only latest max number in message

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:

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

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
}

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.

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