# Return only latest max number in message

**URL:** <https://discourse.nodered.org/t/return-only-latest-max-number-in-message/5535>\
**Category:** General\
**Created:** [6 December 2018 15:01 UTC](https://discourse.nodered.org/t/return-only-latest-max-number-in-message/5535 "2018-12-06T15:01:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MANNAS3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mannas3/32/4317_2.png) [@MANNAS3](https://discourse.nodered.org/u/MANNAS3)\
**Post date:** [6 December 2018 15:01 UTC](https://discourse.nodered.org/t/return-only-latest-max-number-in-message/5535/1 "2018-12-06T15:01:52Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [6 December 2018 15:31 UTC](https://discourse.nodered.org/t/return-only-latest-max-number-in-message/5535/2 "2018-12-06T15:31:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [6 December 2018 19:09 UTC](https://discourse.nodered.org/t/return-only-latest-max-number-in-message/5535/3 "2018-12-06T19:09:15Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![MANNAS3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mannas3/32/4317_2.png) [@MANNAS3](https://discourse.nodered.org/u/MANNAS3)\
**Post date:** [10 December 2018 11:27 UTC](https://discourse.nodered.org/t/return-only-latest-max-number-in-message/5535/4 "2018-12-10T11:27:04Z")

</div>

> [@Andrei](#):
>
> Try changing this part of the code. Make sure the global variable is set to "off" somehwere in your flow.

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.

---

<div class="post-metadata">

**Author:** ![MANNAS3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mannas3/32/4317_2.png) [@MANNAS3](https://discourse.nodered.org/u/MANNAS3)\
**Post date:** [10 December 2018 11:28 UTC](https://discourse.nodered.org/t/return-only-latest-max-number-in-message/5535/5 "2018-12-10T11:28:08Z")

</div>

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