Status code decoding / partition function

Hello together,

sorry for my bad english. In the past I could solve problems with Node Red by my own or with reading of this forum but at the moment I'm stuck with a problem or I don't see the obvious solution.

I have a device which sends its status as numeric value (e.g. 84). The device can has multiple status. To "decode" the number there is a table:

1 = status1
2 = status2
4 = status3
8 = status4
16 = status5
32 = status6
64 = status7
128 = status8
256 = status9
512 = status 10

To "decode" the received value of 84 I have to look into the table and strip down as followed:
64 = status7
16 = status5
4 = status3

How can I use a function node to do that in Node-Red? My search in the internet gave me a possible solution called "partition function" or something like this but I have no idea where to start.

It would be very helpful if someone can give me a hint or first sign.

Thank you very much

This is more of a binary / bit pattern thing.

If you really want to do it in a function, search for "JavaScript test bit" then test bits 0 ~ 15 to determine if a bit is ON/true of OFF/false

If you want a low-code solution, node-red-contrib-buffer-parser can do the bit testing...

single result

[{"id":"954cea6fd2a7eed4","type":"inject","z":"1bd2b382.4dd4f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"84","payloadType":"num","x":1214,"y":672,"wires":[["6826f8be4de86f3e"]]},{"id":"153b284cc2768cd2","type":"inject","z":"1bd2b382.4dd4f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"256","payloadType":"num","x":1218,"y":720,"wires":[["6826f8be4de86f3e"]]},{"id":"8f007dd46d1f47a4","type":"inject","z":"1bd2b382.4dd4f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"512","payloadType":"num","x":1218,"y":768,"wires":[["6826f8be4de86f3e"]]},{"id":"6007da183564b6bd","type":"inject","z":"1bd2b382.4dd4f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"64","payloadType":"num","x":1214,"y":624,"wires":[["6826f8be4de86f3e"]]},{"id":"50db0662263cfcf8","type":"debug","z":"1bd2b382.4dd4f4","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1796,"y":688,"wires":[]},{"id":"6826f8be4de86f3e","type":"change","z":"1bd2b382.4dd4f4","name":"to array","rules":[{"t":"set","p":"payload","pt":"msg","to":"[payload]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1444,"y":688,"wires":[["be67a98eebdbd88f"]]},{"id":"be67a98eebdbd88f","type":"buffer-parser","z":"1bd2b382.4dd4f4","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bool","name":"status1","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"bool","name":"status2","offset":0,"length":1,"offsetbit":1,"scale":"1","mask":""},{"type":"bool","name":"status3","offset":0,"length":1,"offsetbit":2,"scale":"1","mask":""},{"type":"bool","name":"status4","offset":0,"length":1,"offsetbit":3,"scale":"1","mask":""},{"type":"bool","name":"status5","offset":0,"length":1,"offsetbit":4,"scale":"1","mask":""},{"type":"bool","name":"status6","offset":0,"length":1,"offsetbit":5,"scale":"1","mask":""},{"type":"bool","name":"status7","offset":0,"length":1,"offsetbit":6,"scale":"1","mask":""},{"type":"bool","name":"status8","offset":0,"length":1,"offsetbit":7,"scale":"1","mask":""},{"type":"bool","name":"status9","offset":0,"length":1,"offsetbit":8,"scale":"1","mask":""},{"type":"bool","name":"status10","offset":0,"length":1,"offsetbit":9,"scale":"1","mask":""},{"type":"bool","name":"status11","offset":0,"length":1,"offsetbit":10,"scale":"1","mask":""},{"type":"bool","name":"status12","offset":0,"length":1,"offsetbit":11,"scale":"1","mask":""},{"type":"bool","name":"status13","offset":0,"length":1,"offsetbit":12,"scale":"1","mask":""},{"type":"bool","name":"status14","offset":0,"length":1,"offsetbit":13,"scale":"1","mask":""},{"type":"bool","name":"status15","offset":0,"length":1,"offsetbit":14,"scale":"1","mask":""},{"type":"bool","name":"status16","offset":0,"length":1,"offsetbit":15,"scale":"1","mask":""}],"swap1":"swap16","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":1606,"y":688,"wires":[["50db0662263cfcf8"]]}]

individual bits...

[{"id":"c169ecc36d0a3f94","type":"buffer-parser","z":"1bd2b382.4dd4f4","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bool","name":"status1","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"bool","name":"status2","offset":0,"length":1,"offsetbit":1,"scale":"1","mask":""},{"type":"bool","name":"status3","offset":0,"length":1,"offsetbit":2,"scale":"1","mask":""},{"type":"bool","name":"status4","offset":0,"length":1,"offsetbit":3,"scale":"1","mask":""},{"type":"bool","name":"status5","offset":0,"length":1,"offsetbit":4,"scale":"1","mask":""},{"type":"bool","name":"status6","offset":0,"length":1,"offsetbit":5,"scale":"1","mask":""},{"type":"bool","name":"status7","offset":0,"length":1,"offsetbit":6,"scale":"1","mask":""},{"type":"bool","name":"status8","offset":0,"length":1,"offsetbit":7,"scale":"1","mask":""},{"type":"bool","name":"status9","offset":0,"length":1,"offsetbit":8,"scale":"1","mask":""},{"type":"bool","name":"status10","offset":0,"length":1,"offsetbit":9,"scale":"1","mask":""},{"type":"bool","name":"status11","offset":0,"length":1,"offsetbit":10,"scale":"1","mask":""},{"type":"bool","name":"status12","offset":0,"length":1,"offsetbit":11,"scale":"1","mask":""},{"type":"bool","name":"status13","offset":0,"length":1,"offsetbit":12,"scale":"1","mask":""},{"type":"bool","name":"status14","offset":0,"length":1,"offsetbit":13,"scale":"1","mask":""},{"type":"bool","name":"status15","offset":0,"length":1,"offsetbit":14,"scale":"1","mask":""},{"type":"bool","name":"status16","offset":0,"length":1,"offsetbit":15,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"return","multipleResult":true,"fanOutMultipleResult":true,"setTopic":true,"outputs":16,"x":1594,"y":1456,"wires":[["ee1c2af4c4e37c2f"],["9d6d4182bda453de"],["5c1f35787dad46aa"],["d64ad8034307db4a"],["221f1e2ab3fd1acb"],["c3f5079664d42e87"],["48c2a333f8877ece"],["c136ec8bd5ef915f"],["2af9cc30c40a6c7d"],["afdb686e0bcb8ace"],["5cbef597c0cf0d7c"],[],[],[],[],[]]},{"id":"ee1c2af4c4e37c2f","type":"debug","z":"1bd2b382.4dd4f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1786,"y":1328,"wires":[]},{"id":"9d6d4182bda453de","type":"debug","z":"1bd2b382.4dd4f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1962,"y":1344,"wires":[]},{"id":"5c1f35787dad46aa","type":"debug","z":"1bd2b382.4dd4f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1786,"y":1376,"wires":[]},{"id":"d64ad8034307db4a","type":"debug","z":"1bd2b382.4dd4f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1962,"y":1392,"wires":[]},{"id":"221f1e2ab3fd1acb","type":"debug","z":"1bd2b382.4dd4f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1786,"y":1424,"wires":[]},{"id":"48c2a333f8877ece","type":"debug","z":"1bd2b382.4dd4f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1786,"y":1472,"wires":[]},{"id":"c136ec8bd5ef915f","type":"debug","z":"1bd2b382.4dd4f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1958,"y":1488,"wires":[]},{"id":"2af9cc30c40a6c7d","type":"debug","z":"1bd2b382.4dd4f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1786,"y":1520,"wires":[]},{"id":"afdb686e0bcb8ace","type":"debug","z":"1bd2b382.4dd4f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1958,"y":1536,"wires":[]},{"id":"5cbef597c0cf0d7c","type":"debug","z":"1bd2b382.4dd4f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1786,"y":1568,"wires":[]},{"id":"c3f5079664d42e87","type":"debug","z":"1bd2b382.4dd4f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1958,"y":1440,"wires":[]},{"id":"6b4437aa7fff296b","type":"inject","z":"1bd2b382.4dd4f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"84","payloadType":"num","x":1202,"y":1440,"wires":[["92ecdf3b59a3f42b"]]},{"id":"2f2f0ae56a48ffaa","type":"inject","z":"1bd2b382.4dd4f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"256","payloadType":"num","x":1206,"y":1488,"wires":[["92ecdf3b59a3f42b"]]},{"id":"c27f42ed498ef441","type":"inject","z":"1bd2b382.4dd4f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"512","payloadType":"num","x":1206,"y":1536,"wires":[["92ecdf3b59a3f42b"]]},{"id":"486650757b292133","type":"inject","z":"1bd2b382.4dd4f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"64","payloadType":"num","x":1202,"y":1392,"wires":[["92ecdf3b59a3f42b"]]},{"id":"92ecdf3b59a3f42b","type":"change","z":"1bd2b382.4dd4f4","name":"to array","rules":[{"t":"set","p":"payload","pt":"msg","to":"[payload]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1432,"y":1456,"wires":[["c169ecc36d0a3f94"]]}]
1 Like

Hi Steve-Mcl,

this is EXACT what I am looking for. Awesome, I am trying the whole day to solve that problem and I think to complicated.

THANK YOU SO MUCH

Also doable in function node, here are a couple of examples

[{"id":"66e9594f.3e079","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"84","payloadType":"num","x":140,"y":2460,"wires":[["c5e5319a.a9819","c4b34bd1.cb5f7"]]},{"id":"c5e5319a.a9819","type":"function","z":"bf9e1e33.030598","name":"","func":"binary_array = Number(msg.payload).toString(2).split(\"\").reverse();\nmsg = binary_array.map((s,i) => (s===\"1\" ? {payload: \"status\"+(i+1)} : null));\nreturn msg;","outputs":10,"noerr":0,"initialize":"","finalize":"","x":370,"y":2440,"wires":[["298f044.73ac97c"],["298f044.73ac97c"],["298f044.73ac97c"],["298f044.73ac97c"],["298f044.73ac97c"],["298f044.73ac97c"],["298f044.73ac97c"],["298f044.73ac97c"],["298f044.73ac97c"],["298f044.73ac97c"]]},{"id":"c4b34bd1.cb5f7","type":"function","z":"bf9e1e33.030598","name":"","func":"binary_array = msg.payload.toString(2).split(\"\").reverse()\nmsg.payload = binary_array.reduce((a,s,i) => s===\"1\" ? a.concat(\"status\"+(i+1)) : a,[])\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":390,"y":2580,"wires":[["298f044.73ac97c"]]},{"id":"449ca6ab.d59ad","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"511","payloadType":"num","x":150,"y":2500,"wires":[["c5e5319a.a9819"]]},{"id":"fb93315a.733d8","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1023","payloadType":"num","x":150,"y":2540,"wires":[["c5e5319a.a9819"]]},{"id":"298f044.73ac97c","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":2600,"wires":[]}]

[edit] changed one reduce() to map()

1 Like

Hello E1cid,

also a big thank to you. This working very well too with only one function node. :+1: :+1: :+1:

Now I have two solutions .....

Thanks to all

1 Like

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