# Press button then it output no2\_array\[\], But not correct! Why?

**URL:** <https://discourse.nodered.org/t/press-button-then-it-output-no2-array-but-not-correct-why/47872>\
**Category:** General\
**Created:** [1 July 2021 02:30 UTC](https://discourse.nodered.org/t/press-button-then-it-output-no2-array-but-not-correct-why/47872 "2021-07-01T02:30:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlian168](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jlian168/32/43737_2.png) [@jlian168](https://discourse.nodered.org/u/jlian168)\
**Post date:** [1 July 2021 02:30 UTC](https://discourse.nodered.org/t/press-button-then-it-output-no2-array-but-not-correct-why/47872/1 "2021-07-01T02:30:32Z")

</div>

Dear All :

Look at pic red circle, I want output no2\_array[0~3] but only output no2\_array[0]! Where im wrong?

Thank u.

 ![螢幕擷取畫面 2021-07-01 102548](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/2/f28fb0fd48e316e0bc3fc0cac2fcde1ea03fca3e.jpeg)  
 ![螢幕擷取畫面 2021-07-01 102632](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/a/fabbc1d2085c64462814a62bb142792cbb079b3d.jpeg)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [1 July 2021 05:51 UTC](https://discourse.nodered.org/t/press-button-then-it-output-no2-array-but-not-correct-why/47872/2 "2021-07-01T05:51:19Z")

</div>

your screenshot does not show us all of your function code so we cannot answer your question.

Please share the code, not a screenshot, and make sure you format it properly using the `</>` button.

---

<div class="post-metadata">

**Author:** ![jlian168](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jlian168/32/43737_2.png) [@jlian168](https://discourse.nodered.org/u/jlian168)\
**Post date:** [1 July 2021 06:05 UTC](https://discourse.nodered.org/t/press-button-then-it-output-no2-array-but-not-correct-why/47872/3 "2021-07-01T06:05:59Z")

</div>

Dear knolleary :

The function code is at below :

```auto
let no2_array=[0];
if(msg.topic=="no2_out")
{
    if(msg.payload==true)
    {
        msg.payload=no2_array;
        return msg;
    }
}
//
if(msg.topic=="no2_pn")
{
    if(msg.payload=='+')
        no2_array[0]=0;
    else if(msg.payload=='-')
        no2_array[0]=1;

}
//
if(msg.topic=="no2_cali_val")
{
    if(msg.payload<=5000)
    {
        //1.Good,Show is Sring : ["1388", "13", "88"].
        no2_array[1]=Number(msg.payload).toString(16);
        no2_array[2]=msg.payload>>8;
        no2_array[2]=Number(no2_array[2]).toString(16);
        no2_array[3]=msg.payload&0xFF;
        no2_array[3]=Number(no2_array[3]
    }
}

```

Thank u.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 July 2021 06:41 UTC](https://discourse.nodered.org/t/press-button-then-it-output-no2-array-but-not-correct-why/47872/4 "2021-07-01T06:41:05Z")

</div>

The line

no2\_array[3]=Number(no2\_array[3]

is missing )

The only return statement in your function is in the  
if(msg.topic=="no2\_out") { } block.

---

<div class="post-metadata">

**Author:** ![jlian168](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jlian168/32/43737_2.png) [@jlian168](https://discourse.nodered.org/u/jlian168)\
**Post date:** [1 July 2021 06:44 UTC](https://discourse.nodered.org/t/press-button-then-it-output-no2-array-but-not-correct-why/47872/5 "2021-07-01T06:44:42Z")

</div>

Dear jbudd :

1. no2\_array[3]=Number(no2\_array[3];
2. the retun is in "no2\_out".  
Cause I think I input in "no2\_np" and "no2\_cali\_cal" first then output in "no2\_out".

Thank u.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [1 July 2021 07:13 UTC](https://discourse.nodered.org/t/press-button-then-it-output-no2-array-but-not-correct-why/47872/6 "2021-07-01T07:13:23Z")

</div>

Hi @jlian168

I think I understand what you are trying to do. You are trying to create `no2_array` using three different messages, before you send the full value.

The problem is the `no2_array` variable is reset every time the Function node receives a message.

To fix this, you need to use Context - [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#storing-data). This lets you store values between different calls to the Function node.

You can use `context.get` to get a value from context, and `context.set` to set the value.

```auto
// Get the current value from context, or default to `[0]`
let no2_array=context.get("no2_array") || [0];

if(msg.topic=="no2_out")
{
    if(msg.payload==true)
    {
        msg.payload=no2_array;
        return msg;
    }
}
//
if(msg.topic=="no2_pn")
{
    if(msg.payload=='+')
        no2_array[0]=0;
    else if(msg.payload=='-')
        no2_array[0]=1;

}
//
if(msg.topic=="no2_cali_val")
{
    if(msg.payload<=5000)
    {
        //1.Good,Show is Sring : ["1388", "13", "88"].
        no2_array[1]=Number(msg.payload).toString(16);
        no2_array[2]=msg.payload>>8;
        no2_array[2]=Number(no2_array[2]).toString(16);
        no2_array[3]=msg.payload&0xFF;
        no2_array[3]=Number(no2_array[3]
    }
}
// Save it back to context
context.set("no2_array", no2_array) 

```

---

<div class="post-metadata">

**Author:** ![jlian168](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jlian168/32/43737_2.png) [@jlian168](https://discourse.nodered.org/u/jlian168)\
**Post date:** [1 July 2021 07:57 UTC](https://discourse.nodered.org/t/press-button-then-it-output-no2-array-but-not-correct-why/47872/7 "2021-07-01T07:57:02Z")

</div>

Dear knolleary :

Have a question at pic and below :

1. When I new start to open node-red.
2. In the DashBoard, I press send to(button) then just send [0] at debug window.
3. I think its new start, So it will hava a big problem, I need to touch any button or switch or input that can correct use!?

Thank u.

 ![螢幕擷取畫面 2021-07-01 155007](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/f/cf761e31ffecc23ad21835e8ef4fadc86b38fef1.jpeg)  
 ![螢幕擷取畫面 2021-07-01 155041](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/d/3d37dd825c881ab44b13d3b7e9ff57999f8e1ede.jpeg)

---

<div class="post-metadata">

**Author:** ![jlian168](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jlian168/32/43737_2.png) [@jlian168](https://discourse.nodered.org/u/jlian168)\
**Post date:** [1 July 2021 08:18 UTC](https://discourse.nodered.org/t/press-button-then-it-output-no2-array-but-not-correct-why/47872/8 "2021-07-01T08:18:05Z")

</div>

Dear knolleary :

Another question about toggle flag at pic and below :

1. topic is "Test\_Flag".
2. I press Toggle SW(button) then just send 1 to debug window!  
So my code where is wrong?

Thank u.

 ![螢幕擷取畫面 2021-07-01 161438](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/6/165ab34447e690a156aed13c041de06a2cdbfde3.jpeg)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [1 July 2021 08:24 UTC](https://discourse.nodered.org/t/press-button-then-it-output-no2-array-but-not-correct-why/47872/9 "2021-07-01T08:24:45Z")

</div>

@jlian168 please don't use this thread to direct all your questions at me. You have another thread for the flag question.

(But, I will tell you - change the `||[0]` to `||0` because you want `flag` to default to the number `0` not an array as you have for `no2_array`.

> [@jlian168](#):
>
> So it will hava a big problem, I need to touch any button or switch or input that can correct use!?

What problem? I do not know what you want the flow to do, so I do not know what behaviour you want.

My guess is you want to reset `no2_array` after sending it. So where you have:

```auto
        msg.payload=no2_array;
        return msg;

```

You could add a line to reset `no2_array` ready for the next user:

```auto
        msg.payload=no2_array;
        context.set("no2_array",[0]);
        return msg;

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [15 July 2021 08:25 UTC](https://discourse.nodered.org/t/press-button-then-it-output-no2-array-but-not-correct-why/47872/10 "2021-07-15T08:25:30Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
