# Output 0 or 1 based on array

**URL:** <https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923>\
**Category:** General\
**Created:** [7 February 2022 14:42 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923 "2022-02-07T14:42:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![aallen](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@aallen](https://discourse.nodered.org/u/aallen)\
**Post date:** [7 February 2022 14:42 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/1 "2022-02-07T14:42:35Z")

</div>

Hello,  
I have a 16 bit array in binary. I am only interested in the first 2 bits, or the first 2 positions in the array.  
So far, I've figured out I can use mask in the buffer parser node, or I can use the slice method in a function block

What is the best way to output a 0 if the first 2 elements are [0, 0] or a 1 if the first 2 elements are [1, 1] ?  
I was thinking either to do it in the function block if slice is used, or use a switch block or a split block

---

<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:** [7 February 2022 14:58 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/2 "2022-02-07T14:58:21Z")

</div>

Can you feed the value into a debug node so we can see exactly what form it is in please? There are many ways of holding a 16bit array in binary.

Edit:  
What do you want to do if they are 1 and 0, or 0 and 1?

---

<div class="post-metadata">

**Author:** ![aallen](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@aallen](https://discourse.nodered.org/u/aallen)\
**Post date:** [7 February 2022 15:16 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/3 "2022-02-07T15:16:41Z")

</div>

This is the "Off" status ( bits 0 and 1 are == 0)  
 ![Screen Shot 2022-02-07 at 9.14.16 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/e/aee6cbab459b5e2ffb0afc9fed6988206dbf756e.png)

This is the "On" status (bits 0 and 1 == 1).  
 ![Screen Shot 2022-02-07 at 9.15.36 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/b/6bad5150bd7473096ca578da97a26e4d42fba096.png)

I would like to have other statuses for when they are [0, 1] as well, as that means the device is ramping down. But most important are the off and on.

[1, 0] is not as important, it just means standby, but if its simple to integrate that I would like to as well.

---

<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:** [7 February 2022 15:21 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/4 "2022-02-07T15:21:32Z")

</div>

Something like (untested)

```auto
const bits = msg.payload.item1[0].bits
let answer;
if ( bits[0] === bits[1] ) {
  answer = bits[0]
} else {
  // not the same, so do something else
}

```

---

<div class="post-metadata">

**Author:** ![aallen](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@aallen](https://discourse.nodered.org/u/aallen)\
**Post date:** [7 February 2022 15:27 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/5 "2022-02-07T15:27:00Z")

</div>

Wouldn't this just always execute if its off or on? Since it will be 1,1 or 0,0 that if statement would be always true?

This is what I've written so far.

```auto
VFDstatus = msg.payload.item1[0].bits.slice(0,2);
val1 = VFDstatus[0];
val2 = VFDstatus[1];

if (val1 == 1 && val2 == 1);
    {
        msg.payload = 1;
        return msg;
    }
if (val1 === 0 && val2 === 0);
    {
        msg.payload = 0;
        return msg;
    }

```

But it doesn't work. Always outputs 1.

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [7 February 2022 15:30 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/6 "2022-02-07T15:30:48Z")

</div>

```auto
[
    {
        "id": "84b7a77b43d96a87",
        "type": "function",
        "z": "78d94accf7f5ec3c",
        "name": "UInt16LE \"0000 0011\" \"0000 0000\" ",
        "func": "const buf= new Buffer(2);\nbuf.writeUInt16LE(msg.payload, 0);\nmsg.payload = buf;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 380,
        "y": 3760,
        "wires": [
            [
                "0a65de15fbd2ddfd"
            ]
        ]
    },
    {
        "id": "0a65de15fbd2ddfd",
        "type": "buffer-parser",
        "z": "78d94accf7f5ec3c",
        "name": "Get Bytes out of Bits",
        "data": "payload",
        "dataType": "msg",
        "specification": "spec",
        "specificationType": "ui",
        "items": [
            {
                "type": "8bit",
                "name": "bits",
                "offset": 0,
                "length": 1,
                "offsetbit": 1,
                "scale": "1",
                "mask": "3"
            }
        ],
        "swap1": "",
        "swap2": "",
        "swap3": "",
        "swap1Type": "swap",
        "swap2Type": "swap",
        "swap3Type": "swap",
        "msgProperty": "payload",
        "msgPropertyType": "str",
        "resultType": "keyvalue",
        "resultTypeType": "return",
        "multipleResult": false,
        "fanOutMultipleResult": false,
        "setTopic": true,
        "outputs": 1,
        "x": 680,
        "y": 3760,
        "wires": [
            [
                "c47aeabf65f381d0"
            ]
        ]
    },
    {
        "id": "b7558809d659bd6b",
        "type": "inject",
        "z": "78d94accf7f5ec3c",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "v": "payload",
                "vt": "msg"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "3",
        "payloadType": "num",
        "x": 150,
        "y": 3760,
        "wires": [
            [
                "84b7a77b43d96a87"
            ]
        ]
    },
    {
        "id": "c47aeabf65f381d0",
        "type": "switch",
        "z": "78d94accf7f5ec3c",
        "name": "",
        "property": "payload.bits[0].bit0 = payload.bits[0].bit1",
        "propertyType": "jsonata",
        "rules": [
            {
                "t": "true"
            },
            {
                "t": "false"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 870,
        "y": 3760,
        "wires": [
            [
                "e99c87c8984d8b2b"
            ],
            [
                "a456f699c852c0ae",
                "cef34f1e7acd2ab0"
            ]
        ]
    },
    {
        "id": "e99c87c8984d8b2b",
        "type": "change",
        "z": "78d94accf7f5ec3c",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.bits[0].bit0",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1050,
        "y": 3720,
        "wires": [
            [
                "d781081c682909b2"
            ]
        ]
    },
    {
        "id": "d781081c682909b2",
        "type": "switch",
        "z": "78d94accf7f5ec3c",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 1230,
        "y": 3720,
        "wires": [
            [
                "b892f7592f3a038c"
            ],
            [
                "2edaa9a292554104"
            ]
        ]
    },
    {
        "id": "b892f7592f3a038c",
        "type": "debug",
        "z": "78d94accf7f5ec3c",
        "name": "0",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1370,
        "y": 3700,
        "wires": []
    },
    {
        "id": "2edaa9a292554104",
        "type": "debug",
        "z": "78d94accf7f5ec3c",
        "name": "1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1370,
        "y": 3740,
        "wires": []
    },
    {
        "id": "a456f699c852c0ae",
        "type": "change",
        "z": "78d94accf7f5ec3c",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.bits[0].bit0",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1050,
        "y": 3780,
        "wires": [
            []
        ]
    },
    {
        "id": "cef34f1e7acd2ab0",
        "type": "change",
        "z": "78d94accf7f5ec3c",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.bits[0].bit1",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1050,
        "y": 3860,
        "wires": [
            []
        ]
    }
]

```

Here an example to solve it with a flow - I just edited the change node for equal bits.

---

<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:** [7 February 2022 15:32 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/7 "2022-02-07T15:32:23Z")

</div>

> [@aallen](#):
>
> if statement would be always true?

The if statement is true if they are both 0 or both 1. Then the answer is set to bits[0] which will be 0 if both are 0 or 1 if both are 1.

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [7 February 2022 15:38 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/8 "2022-02-07T15:38:18Z")

</div>

In prinziple with the mask 3 in the buffer node you can directly use a switch node - as only 3 values will be the outcome:

So 3 = 1 [1,1], 0 = [0,0] and 1 and 2 what you want.

---

<div class="post-metadata">

**Author:** ![aallen](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@aallen](https://discourse.nodered.org/u/aallen)\
**Post date:** [7 February 2022 15:41 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/9 "2022-02-07T15:41:52Z")

</div>

Can you elaborate how I can setup the switch node after setting mask to 3 in buffer node?

---

<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:** [7 February 2022 15:42 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/10 "2022-02-07T15:42:32Z")

</div>

If you look at the debug output posted by @aallen, it isn't a buffer, it is just an array of numbers 0 and 1.

---

<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:** [7 February 2022 15:45 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/13 "2022-02-07T15:45:02Z")

</div>

Did you try my suggestion?

---

<div class="post-metadata">

**Author:** ![aallen](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@aallen](https://discourse.nodered.org/u/aallen)\
**Post date:** [7 February 2022 15:46 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/14 "2022-02-07T15:46:10Z")

</div>

I couldn't figure out how to make it work. Im a ultra-novice when it comes to code, especially Java.

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [7 February 2022 15:48 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/15 "2022-02-07T15:48:57Z")

</div>

> [@aallen](#):
>
> Can you elaborate how I can setup the switch node after setting mask to 3 in buffer node?

```auto
[
    {
        "id": "84b7a77b43d96a87",
        "type": "function",
        "z": "78d94accf7f5ec3c",
        "name": "UInt16LE \"0000 0011\" \"0000 0000\" ",
        "func": "const buf= new Buffer(2);\nbuf.writeUInt16LE(msg.payload, 0);\nmsg.payload = buf;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 380,
        "y": 3760,
        "wires": [
            [
                "0a65de15fbd2ddfd"
            ]
        ]
    },
    {
        "id": "0a65de15fbd2ddfd",
        "type": "buffer-parser",
        "z": "78d94accf7f5ec3c",
        "name": "Get Bytes out of Bits",
        "data": "payload",
        "dataType": "msg",
        "specification": "spec",
        "specificationType": "ui",
        "items": [
            {
                "type": "byte",
                "name": "bits",
                "offset": 0,
                "length": 1,
                "offsetbit": 1,
                "scale": "1",
                "mask": "3"
            }
        ],
        "swap1": "",
        "swap2": "",
        "swap3": "",
        "swap1Type": "swap",
        "swap2Type": "swap",
        "swap3Type": "swap",
        "msgProperty": "payload",
        "msgPropertyType": "str",
        "resultType": "value",
        "resultTypeType": "return",
        "multipleResult": false,
        "fanOutMultipleResult": false,
        "setTopic": true,
        "outputs": 1,
        "x": 660,
        "y": 3760,
        "wires": [
            [
                "b2210e6eb07f971c"
            ]
        ]
    },
    {
        "id": "b7558809d659bd6b",
        "type": "inject",
        "z": "78d94accf7f5ec3c",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "v": "payload",
                "vt": "msg"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "455",
        "payloadType": "num",
        "x": 150,
        "y": 3760,
        "wires": [
            [
                "84b7a77b43d96a87"
            ]
        ]
    },
    {
        "id": "b2210e6eb07f971c",
        "type": "switch",
        "z": "78d94accf7f5ec3c",
        "name": "",
        "property": "payload[0]",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "3",
                "vt": "num"
            },
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 850,
        "y": 3760,
        "wires": [
            [
                "9cd4a24aae1fa021"
            ],
            [
                "be0e0708ed048973"
            ]
        ]
    },
    {
        "id": "9cd4a24aae1fa021",
        "type": "change",
        "z": "78d94accf7f5ec3c",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1030,
        "y": 3720,
        "wires": [
            []
        ]
    },
    {
        "id": "be0e0708ed048973",
        "type": "change",
        "z": "78d94accf7f5ec3c",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "false",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1030,
        "y": 3780,
        "wires": [
            []
        ]
    }
]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/2/e22064684f42a08ca4773f0ef7895610a860bbd9.png)

Now you can analyze 1,2 as well - but you will mask all other bytes - so you get only the first 2 bits back.

---

<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:** [7 February 2022 15:59 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/16 "2022-02-07T15:59:47Z")

</div>

> [@aallen](#):
>
> I couldn't figure out how to make it wor

Just feed your message into a function node with the following contents

```auto
const bits = msg.payload.item1[0].bits
let answer;
if ( bits[0] === bits[1] ) {
  answer = bits[0]
} else {
  answer = 2
}
msg.payload = answer
return msg

```

That will return 0 or 1 for both 0 and both 1, and will return 2 for anything else.

By the way your Java knowledge is irrelevant as this is javascript, which is completely different.

---

<div class="post-metadata">

**Author:** ![aallen](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@aallen](https://discourse.nodered.org/u/aallen)\
**Post date:** [7 February 2022 16:18 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/17 "2022-02-07T16:18:56Z")

</div>

> [@Colin](#):
>
> ```auto
> const bits = msg.payload.item1[0].bits
> let answer;
> if ( bits[0] === bits[1] ) {
> answer = bits[0]
> } else {
> answer = 2
> }
> msg.payload = answer
> return msg
> 
> ```

Thank you very much. I ended up using this and got successful results:

```auto
const bits = msg.payload.item1[0].bits
let answer;
if ( bits[0] === 0 && bits[1]===0 ) {
  answer = "Off"
} else {
  if (bits[0] === 1 && bits[1] === 1) {
      answer = "Running"
  }
}
msg.payload = answer
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:** [7 February 2022 17:19 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/18 "2022-02-07T17:19:22Z")

</div>

You can write that more succinctly using

```auto
const bits = msg.payload.item1[0].bits
let answer;
if ( bits[0] === bits[1] ) {
  if ( bits[0] === 0 ) {
    answer = "Off"
  } else {
      answer = "Running"
  }
}
msg.payload = answer
return msg

```

or even

```auto
const bits = msg.payload.item1[0].bits
let answer;
if ( bits[0] === bits[1] ) {
  answer = bits[0] ? "Running" : "Off"
}
msg.payload = answer
return msg

```

Your function (as do mine) will return `undefined` for the other cases. Is that what you want?

---

<div class="post-metadata">

**Author:** ![aallen](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@aallen](https://discourse.nodered.org/u/aallen)\
**Post date:** [7 February 2022 17:42 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/19 "2022-02-07T17:42:28Z")

</div>

Is this better? I added all of the possible combinations so it should never return undefined.

```auto
const bits = msg.payload.item1[0].bits // Sequence of bits is 16bit BE after buffer.
let answer; // Manual reference in LE.
if (bits[0] === 0 && bits[1] === 0)
    {
  answer = "Off"
    } 
else if (bits[0] === 1 && bits[1] === 1) 
    {
    answer = "Running"
    } 
    
else if (bits[0] === 1 && bits[1] === 0)
    {
    answer = "Decelerating"
    }
else if (bits[0] === 0 && bits[1] === 1)      
    {
    answer = "Standby"
    }
    
msg.payload = answer
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:** [7 February 2022 21:37 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/20 "2022-02-07T21:37:24Z")

</div>

You are doing loads of unnecessary comparisons there. In the worst case it may do 8 tests. if you do it like this then the worst case is 3 tests

```auto
const bits = msg.payload.item1[0].bits
let answer;
if ( bits[0] === bits[1] ) {
  // the bits are the same, it is Off or Running, determine which
  if ( bits[0] === 0 ) {
    answer = "Off"
  } else {
      answer = "Running"
  }
} else {
  // the bits are different, it is Decelerating or Standby, determine which
  if ( bits[0] === 1 ) {
    answer = "Decelerating"
  } else {
    answer = "Standby"
  }
}
msg.payload = answer
return msg

```

Actually, the worst case for yours is 6 I think because the interpreter will optimise some of them out for you.

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [7 February 2022 21:46 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/21 "2022-02-07T21:46:24Z")

</div>

What is wrong with my simple flow - why you are you programming in function nodes? Is NodeRed not a tool to do it simple´?

If you want to program JS - then use just a trigger and an output - the flow between can be always completely done within a function node - so no further nodes are necessary.

---

<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:** [7 February 2022 21:51 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/22 "2022-02-07T21:51:29Z")

</div>

Does your flow work with the data posted in post #3 where the data is a numeric array, not a buffer?

[Next page](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923.md?page=2)
