Output 0 or 1 based on array

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

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?

This is the "Off" status ( bits 0 and 1 are == 0)
Screen Shot 2022-02-07 at 9.14.16 AM

This is the "On" status (bits 0 and 1 == 1).
Screen Shot 2022-02-07 at 9.15.36 AM

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.

Something like (untested)

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
}

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.

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.

[
    {
        "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.

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.

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.

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

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.

Did you try my suggestion?

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

[
    {
        "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": [
            []
        ]
    }
]

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

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

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.

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

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

You can write that more succinctly using

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

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?

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

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

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

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.

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.

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