Input topic convert to 16bit integer

Hi,

I would like to sum different topics and payloads together.
The output should be a integer value.

But i have the problem that it doesn't not work as intended :wink:
The output is at the moment "NaN"

Where is the mistake

var statusbits1=0;
var statusbits2=0;
var statusbits3=0;
var statusbits4=0;
var statusbits5=0;
var statusbits6=0;
var statusbits7=0;
var statusbits8=0;

var i;
if(i===0)
{
flow.set('statusbits1',0);
flow.set('statusbits2',0);
flow.set('statusbits3',0);
flow.set('statusbits4',0);
flow.set('statusbits5',0);
flow.set('statusbits6',0);
flow.set('statusbits7',0);
flow.set('statusbits8',0);
i=1;
}


statusbits1=flow.get('statusbits1');
statusbits2=flow.get('statusbits2');
statusbits3=flow.get('statusbits3');
statusbits4=flow.get('statusbits4');
statusbits5=flow.get('statusbits5');
statusbits6=flow.get('statusbits6');
statusbits7=flow.get('statusbits7');
statusbits8=flow.get('statusbits8');



    if(msg.topic=='1')
    {
        if (msg.payload<'3.5')
        {
            flow.set('statusbits1',1);
            statusbits1=1;
        }
        else
        {
            flow.set('statusbits1',0);
            statusbits1=0;
        }
    }
    if(msg.topic=='2')
    {
        if (msg.payload<'3.5')
        {
            flow.set('statusbits2',2);
            statusbits2=2;
        }
        else
        {
            flow.set('statusbits2',0);
            statusbits2=0;
        }
    }
    if(msg.topic=='3')
    {
        if (msg.payload<'3.5')
        {
            flow.set('statusbits3',4);
            statusbits3=4;
        }
        else
        {
            flow.set('statusbits3',0);
            statusbits3=0;
        }
    }
    if(msg.topic=='4')
    {
        if (msg.payload==1)
        {
            flow.set('statusbits4',8);
            statusbits4=8;
        }
        else
        {
            flow.set('statusbits4',0);
            statusbits4=0;
        }
    }
    if(msg.topic=='5')
    {
        if (msg.payload==1)
        {
            flow.set('statusbits5',16);
            statusbits5=16;
        }
        else
        {
            flow.set('statusbits5',0);
            statusbits5=0;
        }
    }
    if(msg.topic=='6')
    {
        if (msg.payload==1)
        {
            flow.set('statusbits6',32);
            statusbits6=32;
        }
        else
        {
            flow.set('statusbits6',0);
            statusbits6=0;
        }
    }
    if(msg.topic=='7')
    {
        if (msg.payload==1)
        {
            flow.set('statusbits7',64);
            statusbits7=64;
        }
        else
        {
            flow.set('statusbits7',0);
            statusbits7=0;
        }
    }
    if(msg.topic=='8')
    {
        if (msg.payload==1)
        {
            flow.set('statusbits8',128);
            statusbits8=128;
        }
        else
        {
            flow.set('statusbits8',0);
            statusbits8=0;
        }
    }

msg.payload=(statusbits1+statusbits2+statusbits3+statusbits4+statusbits5+statusbits6+statusbits7+statusbits8);
msg.topic="status";
return msg;

See the node red docs Writing Functions which will provide suggestions for how to debug a function. In particular the use of node.warn() to follow what is going on.

To get you going, i will never be 0 at that point, it will always be undefined since you have not set it to anything after declaring it. Also note that the value of i will not be held over from one run through the function to the next, so even if i was 0 then setting it to 1 later will have no effect the next time through. A better way to accomplish what you are trying to do is to use, for example
statusbits1=flow.get('statusbits1') || 0
which will set it to 0 if there is not a value in the flow context.

Are you using those context variables anywhere else? If not then use context.get/set instead of flow, then the data is local to the function.

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