Press button then it output no2_array[], But not correct! Why?

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.


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.

Dear knolleary :

The function code is at below :

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.

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.

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.

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. 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.

// 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) 

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.


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.

@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.

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:

        msg.payload=no2_array;
        return msg;

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

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

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