Bitwise syntax error ("error in JavaScript code")

Hello. New to Node Red and Javascript.

I have the code below in a function, but even though it works as I expect, the function gets "error in JavaScript code".

msg.zone = ((msg.payload[1 + 1] >> 2) & (0x01 == 0x01));

How can I get rid of this "error in JavaScript code"?

From https://beautifytools.com/ I get this:
Unexpected use of '>>'
Unexpected use of '&'

I think I have to use something like this but I'm not sure

/*jslint bitwise: true */

Thanks

What is this meant to do? It will always compile to true so seems a bit pointless?

This is also a bit pointless: msg.payload[2] is the same and assumes that the payload is an array - is it? Please share example content of msg.payload.

Since bitwise AND turns both operands to 32bit integers, (0x01 == 0x01) becomes TRUE which becomes 00000000000000000000000000000001 - you might as well have simply put in the number 1.

As you can see I'm not very good with programming.

Long story short, I took a code in golang and passed it to EspHome, but now I use it in Node Red :smile:

I use 1 + 1 as "offset", because there are several functions and each of them with several lines

msg.zone = ((msg.payload[1 + 1] >> 0) & (0x01 == 0x01))
msg.violated = ((msg.payload[1 + 8 + 1] >> 0) & (0x01 == 0x01))
msg.anulated = ((msg.payload[1 + 16 + 1] >> 0) & (0x01 == 0x01))
msg.tamper = ((msg.payload[1 + 43 + 1] >> 0) & (0x01 == 0x01))
msg.shortcircuit = ((msg.payload[1 + 44 + 1] >> 0) & (0x01 == 0x01))
msg.lowbattery = ((msg.payload[1 + 46 + 1] >> 0) & (0x01 == 0x01))
return msg;

Returns true or false for multiple binary sensors.

37 E9 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 52 01 00 03 0C 0F 38 01 0A 16 00 00 00 00 FF 0F 00 00 00 00 00 00 00 00 00 00 00 00 00 E2```

Is the msg.payload actually an array? That looks more like a buffer. Can you output the msg to a debug node and show what the debug panel thinks it is?

The logic appears to be checking whether the input number is odd or even?

image

But a lot rides on the actual data type of the payload.

No, it really doesn't. Not unless you are going to replace one of those values with a variable instead. (0x01 == 0x01) always returns true at the moment. Also, a value of true in that part of the equation is meaningless because it gets converted to a 32bit integer with the value 1.

I forgot to reply, yes it's a buffer

Forget the E9(I don't use it),, let's focus on the 04.

I need to check if third bit of buffer[4] is 1 or 0

msg.zone = ((msg.payload[3 + 1] >> 2) & (0x01 == 0x01))

It's working this way, it just returns the message "The workspace contains some nodes that are not properly configured:" when I deploy and the node gets this error:

node3

There's probably a better way to do this and get these errors out, but this was the best I could get.

The golang code is something like this:

(b[4]>>2&0x01 == 0x01)

Returns 1 or 0 for sensor and for attributes, sensor is interpreted as on or off, true or false:

I don't understand what you mean. The code (0x01 == 0x01) will always return the value true, since obviously 0x01 is always equal to 0x01.

1 Like

I think that something was lost in the translation from golang to JavaScript (by adding brackets in the wrong place)

In the golang it is shifting the value, ANDing it to mask the lowest bit off and then doing a test with 1.

The JavaScript presented earlier is doing it in the wrong order. It seems to be testing 1 against 1 and then ANDing it with the rest.

I think exactly that code would work in javascript, at most you might need to add some parentheses
((b[4]>>2)&0x01 == 0x01)
I would have to test it to be sure.

I suspect it is doing bitwise & followed by equality == then shift >>
See here: Operator precedence - JavaScript | MDN

You can memorise operator precedence (or refer to docs) OR use brackets :slight_smile:

Hi Steve, I was referring to this JavaScript code which was given above. As I said the brackets have been put in the wrong place, and overrides the precedence used by the golang code it is trying to emulate.

msg.zone = ((msg.payload[1 + 1] >> 2) & (0x01 == 0x01));

In that case, perhaps your code should read:

msg.zone = ( (msg.payload[1 + 1] >> 2) & 0x01 ) == 0x01 )

Which is unambiguous in any language

Or, if you want 1/0 rather than true/false use
msg.zone = (msg.payload[1 + 1] >> 2) & 0x01

Well, yes, had it been my code, that’s what I would have done.

Or even

msg.zone = (msg.payload[1 + 1] >> 2) & 1

:slight_smile:

I think you are missing the point, where the code works. The problem is just the error message present in Node Red.

It works too, but the error message is still present

It works too, but the error message is still present

Like I said, I'm not good with programming. I don't know why, but it returns 1 or 0 with the code below.
Each byte represents a group of 8 zones from my alarm, each bit represents 1 zone.
If zone 19 is open (buffer[4] bit 2)
msg.zone = ((msg.payload[3 + 1] >> 2) & (0x01 == 0x01)) this returns 1
If zone 19 is closed (buffer[4] bit 2)
msg.zone = ((msg.payload[3 + 1] >> 2) & (0x01 == 0x01)) this returns 0

Please select that function node and Export it and paste it here.

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

See this post for more details - How to share code or flow json

If you are simply wanting to extract data from a buffer, then the buffer-parser node is designed to do this the "no-code" way...

[{"id":"9b8474f8aa18762a","type":"buffer-parser","z":"4c5ad8c7caa80822","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bool","name":"zone19open","offset":4,"length":1,"offsetbit":2,"scale":"1","mask":""},{"type":"bool","name":"zone20open","offset":4,"length":1,"offsetbit":3,"scale":"1","mask":""}],"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":2150,"y":860,"wires":[["f39a39559f771680"]]},{"id":"e403c7115f8776c7","type":"inject","z":"4c5ad8c7caa80822","name":"simulating your data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"0x37\",\"0x39\",\"0x0\",\"0x0\",\"0x4\"]","payloadType":"bin","x":1950,"y":860,"wires":[["9b8474f8aa18762a"]]},{"id":"f39a39559f771680","type":"debug","z":"4c5ad8c7caa80822","name":"debug 94","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2310,"y":860,"wires":[]},{"id":"83d494286ca2baa4","type":"inject","z":"4c5ad8c7caa80822","name":"simulating your data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"0x37\",\"0x39\",\"0x0\",\"0x0\",\"0x8\"]","payloadType":"bin","x":1950,"y":920,"wires":[["9b8474f8aa18762a"]]}]
1 Like

When I insert into a function block I also get the error.
But if I put an extra & in, see the next function, there
are no errors.

[{"id":"87969c3818de0019","type":"function","z":"f4b8c2e3d4ea3ab1","name":"function 25","func":"msg.zone = ((msg.payload[1 + 1] >> 2) & (0x01 == 0x01));\n","outputs":1,"noerr":1,"initialize":"","finalize":"","libs":[],"x":290,"y":1300,"wires":[[]]},{"id":"531d304dcec94662","type":"function","z":"f4b8c2e3d4ea3ab1","name":"function 26","func":"msg.zone = ((msg.payload[1 + 1] >> 2) && (0x01 == 0x01));\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":1360,"wires":[[]]},{"id":"935753bc85eadea1","type":"comment","z":"f4b8c2e3d4ea3ab1","name":"msg.zone = ((msg.payload[1 + 1] >> 2) & (0x01 == 0x01));","info":"","x":650,"y":1300,"wires":[]},{"id":"c6638f8f65e78844","type":"comment","z":"f4b8c2e3d4ea3ab1","name":"msg.zone = ((msg.payload[1 + 1] >> 2) && (0x01 == 0x01));","info":"","x":650,"y":1360,"wires":[]}]

The right hand part (0x01 == 0x01) evaluates to a Boolean & cannot be used with the bitwise & operator.

If you hover on the red squiggle in the code editor, what does it say?