# How to create an array from mqtt input

**URL:** <https://discourse.nodered.org/t/how-to-create-an-array-from-mqtt-input/5507>\
**Category:** General\
**Created:** [5 December 2018 17:11 UTC](https://discourse.nodered.org/t/how-to-create-an-array-from-mqtt-input/5507 "2018-12-05T17:11:36Z")\
**Posts on this page:** 3\
**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:** [5 December 2018 17:11 UTC](https://discourse.nodered.org/t/how-to-create-an-array-from-mqtt-input/5507/1 "2018-12-05T17:11:37Z")

</div>

I have a button (on/off) - connected to another node that only shows msg when it is turned on.

I have another mqtt in, that is connected along with the first node to a function node.

What I would like to achieve in node-red is to get the max of all the input msg from mqtt in when the button is on.

I have tried to use the code below in a function but it seems doesn't show the output as array when I check the debug output:

```
var newmsg = {lit:msg.payload};
if( context.global.stat =="on")
{
    var litarray = []; //define empty array to get max later
    while(context.global.stat =="on")
    {
    litarray.push(newmsg);
    return[litarray,null];
    }
        
}

if (context.global.stat == "off")
{
    return[null, null]; 
}
```

---

<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:** [5 December 2018 18:03 UTC](https://discourse.nodered.org/t/how-to-create-an-array-from-mqtt-input/5507/2 "2018-12-05T18:03:41Z")

</div>

Look at the docs [https://nodered.org/docs/writing-functions#storing-data](https://nodered.org/docs/writing-functions#storing-data) to see the standard way of getting and setting a global variable within a function node

---

<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:** [5 December 2018 18:12 UTC](https://discourse.nodered.org/t/how-to-create-an-array-from-mqtt-input/5507/3 "2018-12-05T18:12:06Z")

</div>

> [@ukmoose](#):
>
> to see the standard way of getting and setting a global variable within a function node

Thank you @ukmoose my question is regarding the array specifically, but this gave a good idea that I can implement by continuously comparing the last and the current values and get the max from them! thank you.

Also will be viewing the context part in case I am missing something and will get back to you in case I am stuck again 🙂
