# Input topic convert to 16bit integer

**URL:** <https://discourse.nodered.org/t/input-topic-convert-to-16bit-integer/32179>\
**Category:** General\
**Created:** [29 August 2020 11:05 UTC](https://discourse.nodered.org/t/input-topic-convert-to-16bit-integer/32179 "2020-08-29T11:05:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kampfwurst](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kampfwurst/32/26264_2.png) [@Kampfwurst](https://discourse.nodered.org/u/Kampfwurst)\
**Post date:** [29 August 2020 11:05 UTC](https://discourse.nodered.org/t/input-topic-convert-to-16bit-integer/32179/1 "2020-08-29T11:05:47Z")

</div>

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 😉  
The output is at the moment "NaN"

Where is the mistake

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 August 2020 11:24 UTC](https://discourse.nodered.org/t/input-topic-convert-to-16bit-integer/32179/2 "2020-08-29T11:24:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 August 2020 12:35 UTC](https://discourse.nodered.org/t/input-topic-convert-to-16bit-integer/32179/3 "2020-08-29T12:35:57Z")

</div>

> [@Kampfwurst](#):
>
> ```auto
> var i;
> if(i===0)
> 
> ```

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.

---

<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:** [28 October 2020 12:36 UTC](https://discourse.nodered.org/t/input-topic-convert-to-16bit-integer/32179/4 "2020-10-28T12:36:10Z")

</div>

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