Bit Strip from Int

This should be a simple application but we are struggling. The least significant bit is to the right.
Bit 1 = Input 1 Register read = 1
Bit 2 = Input 2 Register read = 2
Bit 3 = Input 3 Register read = 4
Bit 4 = Input 4 Register read = 8

Can make this work with = command when only one input is on however we cannot figure how to return a "0" when the output is greater or less than >< the value. Believe that would solve it. In many SCADA / HMI software packages there is a Bit Strip function that allows an extraction. Cannot find that in Node-Red. Thanks

If the value read is in msg.payload then something like

const b1 = (msg.payload & 1) 
const b2 = (msg.payload & 2) >> 1
const b3 = (msg.payload & 4) >> 2

etc.
Or if you just want to test whether it is set then

if (msg.payload & 1) {}
if (msg.payload & 2) {}
if (msg.payload & 4) {}

Colin: Many thanks. Would like a bool 1 as an output so each input can be presented in a dashboard as in:
Input 1 to dashboard light 1
Input 2 to dashboard light 2
Input 3 to dashboard light 3
Input 4 to dashboard light 4

They developers were not kind enough to put these in Input Registers so they need to be pulled out of an integer string. Kind of common way to do things in the industrial world.

Here is how I would envision my code but do not know how to express this in Node-Red Json.
Would make for functions block that harvest the number from the register. Seeing between 0 and 15 as expected with a Modbus reader.

Function 1
If Input1 = 1 then msg.payload = 1
If Input1 <>1 then mst.payload = 0

Function 2
If Input1 = 2 then msg.payload = 1
If Input1 <>2 then mst.payload = 0

Function 3
If Input1 = 4 then msg.payload = 1
If Input1 <>4 then mst.payload = 0

Function 4
If Input1 = 8 then msg.payload = 1
If Input1 <>8 then mst.payload = 0

Hope that makes sense. Believe the math works out.

Bit Unloader is giving me this:
image

Do not know how to unpack it

Hans

That isn't JavaScript. What language are you working in and where does the function reside?

The code I posted gives the values as 0 or 1 in JavaScript.

Understand it is not java script. More of what I have worked with before to show and example of logical data flow. Need a 1 output when it = 1, 2, 4 or 8 and a 0 when it does not. That should create and exclusive condition for each number.

const b1 = (msg.payload & 1)
const b2 = (msg.payload & 2) >> 1
const b3 = (msg.payload & 4) >> 2

This Returns what was put into it on the debug Need pull the bits out individually

depends what sort of input your led node can take - if the input can be defined to take msg.payload.b0 etc for example then just do that... for each 'b'it.

If not you will need a set of change nodes (one per led) configured to move msg.payload.b0 to msg.payload .... and same for '.b2', '.b3', etc etc

If that is what you want then you can use

const b0 = (msg.payload & 1) 
const b1 = (msg.payload & 2) >> 1
const b2 = (msg.payload & 4) >> 2
const b3 = (msg.payload & 8) >> 3
msg.payload = {b0: b0, b1: b1, b2: b2, b3: b3}
return msg

Thanks: That code returns this

image

Still do not know how to pull these out as individual item for bool logic and dashboard.

More used to how PLCs each have a bool rung that can be used. How to we split these into four useable objects?

What exactly do you want to do with it? What I have provided does have the four values available to do what you want with them. Unless you tell us exactly what that is then it is impossible to help.

Colin, I think the bit-unloader node already creates that object. And the ask is how to take those bits and feed into a series of leds on a dashboard. But which leds ?

The surprise with bit 31 in JavaScript :blush:

Ah yes, looking back I see you are right.

@HansZnarf as Dave pointed out earlier, you can use a Change node to extract an individual value from the object. So a Change node configured like this would extract b1 as 1 or 0
image

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a lot in about 1 hour. A small investment for a lot of gain.

Good point. In fact my code would be better using the unsigned shift operator

const b0 = (msg.payload & 1) 
const b1 = (msg.payload & 2) >>> 1
const b2 = (msg.payload & 4) >>> 2
const b3 = (msg.payload & 8) >>> 3
msg.payload = {b0: b0, b1: b1, b2: b2, b3: b3}
return msg

or

const b0 = msg.payload & 1
const b1 = (msg.payload >> 1) & 1
const b2 = (msg.payload >> 2) & 1
const b3 = (msg.payload >> 3) & 1
msg.payload = {b0: b0, b1: b1, b2: b2, b3: b3}
return msg

JavaScript converts on all bit operation the value first to a 32bit signed integer.
Bit 31 is then the sign.
This is the surprise on bit operation with bit 31.

Yes, but I am not sure of the relevance of that here. Do you mean that the modified code I posted will not work?

Or you could use buffer-parser

Deleted by user.

My technique works ok for me.

[{"id":"97b2f6d0c1f1fa4d","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":4100,"wires":[["bccaca5778a53865"]]},{"id":"bccaca5778a53865","type":"function","z":"bdd7be38.d3b55","name":"function 1","func":"const value = Math.pow(2,31)\nmsg.payload = (value >>> 31) & 1\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":4100,"wires":[["c62e5740f8ffb42a"]]},{"id":"c62e5740f8ffb42a","type":"debug","z":"bdd7be38.d3b55","name":"debug 59","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":520,"y":4100,"wires":[]}]

Can you post an example where it doesn't work?