Display a Variable 'Word' in Dashboard as individual bit LED's

I am struggling finding the right terms and wonder if anyone is able to point me in the right direction.

I have a 'Word' here, a variable made up of individual bits, each bit representing the status of a band of switches.
e.g Let us say the 'word' is as follows and is passed as a message...
0x7FFFF - 01111111111111111111 - 524287

I am trying to display 19 dots in a row on the Dashboard that is either green or red depending upon status (1 or 0).

Any pointers gratefully received.

TIA
Colin J

I think the easiest is making 19 function nodes and feed them all with the "word"
For the first function node, thus the most right bit.

let a= msg.payload
a >>=0 //shift bit to the right
msg.payload = a & 0X00000001; //give the value of the most right bit

For the second function node.

let a= msg.payload
a >>=1 //shift bit to the right
msg.payload = a & 0X00000001; //give the value of the most right bit

Etc...., for the last node.

let a= msg.payload
a >>=18 //shift bit to the right
msg.payload = a & 0X00000001; //give the value of the most right bit

You can do it also 1 function node with a loop and 19 outputs.

Just my 2 cents. (not tested in NR)

Thank you for that, I really appreciate your reply, especially as it reinforces my current thinking.

This is basically how I generate the word. (loops and masks from several ping nodes). I was thinking there might be a 'smart' way that I was missing, as in 'missing a neat trick' here. (Trying to learn new methods!). The single word helps me read things with a control system I use for some automation.

I will effectively reverse my procedure to generate to individual LEDs.

It is actually a word generated from pings to several WiFi devices to see if they are online. One of my Grandchildren managed to switch a socket off at the weekend that is the supply to a couple of Shelly devices controlling LEDs in the Living/Dining room. Spent some time fault finding before my wife asked me "Did you know this switch if off?" :rofl:

There is/are several bit decoders in the node Red pallete. Have you done a search for them in pallete manager? That might be way easier than 16 function nodes.

Thank you, I will have a look.

@edje11 @gerry Thank you both for the help, it is gratefully received and unfroze my brain freeze!

I have now looked at my original function and decided the best course of action was to create the Status Word and an array of colours at the same time concat'ing the colours array and feeding that into a LED bar template I had found previously. This is the result.

[
    {
        "id": "4b2c35400318c2ac",
        "type": "ping",
        "z": "ee0fd918dab89122",
        "protocol": "IPv4",
        "mode": "triggered",
        "name": "Ping Group",
        "host": "XXX.XXX.XXX.XXX, XXX.XXX.XXX.XXX, XXX.XXX.XXX.XXX, XXX.XXX.XXX.XXXX, XX.XXX.XXX.XXX",
        "timer": "20",
        "inputs": 1,
        "x": 310,
        "y": 280,
        "wires": [
            [
                "79c943bdfe2e655f"
            ]
        ]
    },
    {
        "id": "42646dca62aed2b7",
        "type": "inject",
        "z": "ee0fd918dab89122",
        "name": "Shelly Lights",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "str",
        "x": 130,
        "y": 280,
        "wires": [
            [
                "4b2c35400318c2ac"
            ]
        ]
    },
    {
        "id": "79c943bdfe2e655f",
        "type": "join",
        "z": "ee0fd918dab89122",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "5",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 470,
        "y": 280,
        "wires": [
            [
                "f5ac4b576f382216"
            ]
        ]
    },
    {
        "id": "93233b26b4bce3e1",
        "type": "ping",
        "z": "ee0fd918dab89122",
        "protocol": "IPv4",
        "mode": "triggered",
        "name": "Ping Group",
        "host": "XXX.XXX.XXX.XXX, XXX.XXX.XXX.XXX, XXX.XXX.XXX.XXX, XXX.XXX.XXX.XXXX",
        "timer": "20",
        "inputs": 1,
        "x": 310,
        "y": 320,
        "wires": [
            [
                "b36d0ebf907d55b1"
            ]
        ]
    },
    {
        "id": "b36d0ebf907d55b1",
        "type": "join",
        "z": "ee0fd918dab89122",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "4",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 470,
        "y": 320,
        "wires": [
            [
                "37422c92098f6fb3"
            ]
        ]
    },
    {
        "id": "68168aaa6ccae55c",
        "type": "ping",
        "z": "ee0fd918dab89122",
        "protocol": "IPv4",
        "mode": "triggered",
        "name": "Ping Group",
        "host": "XXX.XXX.XXX.XXX, XXX.XXX.XXX.XXX, XXX.XXX.XXX.XXX",
        "timer": "20",
        "inputs": 1,
        "x": 310,
        "y": 360,
        "wires": [
            [
                "cf7d32ce2c356362"
            ]
        ]
    },
    {
        "id": "cf7d32ce2c356362",
        "type": "join",
        "z": "ee0fd918dab89122",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "3",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 470,
        "y": 360,
        "wires": [
            [
                "eee04ccbbca784a0"
            ]
        ]
    },
    {
        "id": "05d006d36707cf84",
        "type": "ping",
        "z": "ee0fd918dab89122",
        "protocol": "IPv4",
        "mode": "triggered",
        "name": "Ping Group",
        "host": "XXX.XXX.XXX.XXX, XXX.XXX.XXX.XXX, XXX.XXX.XXX.XXX",
        "timer": "20",
        "inputs": 1,
        "x": 310,
        "y": 400,
        "wires": [
            [
                "f9d49f59851ba486"
            ]
        ]
    },
    {
        "id": "f9d49f59851ba486",
        "type": "join",
        "z": "ee0fd918dab89122",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "3",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 470,
        "y": 400,
        "wires": [
            [
                "43d64236567895cc"
            ]
        ]
    },
    {
        "id": "8565b12ac7f59f4d",
        "type": "ping",
        "z": "ee0fd918dab89122",
        "protocol": "IPv4",
        "mode": "triggered",
        "name": "Ping Group",
        "host": "XXX.XXX.XXX.XXX, XXX.XXX.XXX.XXX, XXX.XXX.XXX.XXX, XXX.XXX.XXX.XXXX",
        "timer": "20",
        "inputs": 1,
        "x": 310,
        "y": 440,
        "wires": [
            [
                "7623bf4ea4ad90bf"
            ]
        ]
    },
    {
        "id": "7623bf4ea4ad90bf",
        "type": "join",
        "z": "ee0fd918dab89122",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "4",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 470,
        "y": 440,
        "wires": [
            [
                "5abc019a95358832"
            ]
        ]
    },
    {
        "id": "f5ac4b576f382216",
        "type": "function",
        "z": "ee0fd918dab89122",
        "name": "",
        "func": "let connectWiFiMask0 = 0;\nlet connectWiFiMaskX = 0;\nlet colours = [];\nlet arrLengthOld = 0;\nconnectWiFiMask0 = global.get('connectWiFiMask0');\nlet array = Object.entries(msg.payload);\narray = array.sort();\nglobal.set('arrLength', array.length);\n\nfor(let i=0; i<array.length; i++){\n    if(array[i][1] !== false){\n        var mask =  1 << i; // gets the 'i'th bit\n        if ((connectWiFiMaskX & mask) === 0) {\n            connectWiFiMaskX |= mask;\n        }\n        array[i][2] = 1;\n        colours[i] = \"#33ff55\";\n    } else {\n        array[i][2] = 0;\n        colours[i] = \"#FF3333\";\n    }\n}\n\nconnectWiFiMask0 = connectWiFiMask0 + connectWiFiMaskX;\nglobal.set('connectWiFiMask0', connectWiFiMask0);\nglobal.set('colours', colours);\nmsg.colours = colours;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 620,
        "y": 280,
        "wires": [
            [
                "93233b26b4bce3e1"
            ]
        ]
    },
    {
        "id": "ae532e202ea57897",
        "type": "inject",
        "z": "ee0fd918dab89122",
        "name": "KingArt, PZEM",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "str",
        "x": 140,
        "y": 320,
        "wires": [
            [
                "93233b26b4bce3e1"
            ]
        ]
    },
    {
        "id": "c9c9fd6928164f45",
        "type": "inject",
        "z": "ee0fd918dab89122",
        "name": "Sockets",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "str",
        "x": 110,
        "y": 360,
        "wires": [
            [
                "68168aaa6ccae55c"
            ]
        ]
    },
    {
        "id": "61b51c448c97a6cc",
        "type": "inject",
        "z": "ee0fd918dab89122",
        "name": "Relays",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "str",
        "x": 110,
        "y": 400,
        "wires": [
            [
                "05d006d36707cf84"
            ]
        ]
    },
    {
        "id": "9d31a333ae66b297",
        "type": "inject",
        "z": "ee0fd918dab89122",
        "name": "Sensors",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "str",
        "x": 120,
        "y": 440,
        "wires": [
            [
                "8565b12ac7f59f4d"
            ]
        ]
    },
    {
        "id": "3689acf3743bbff1",
        "type": "ui_template",
        "z": "ee0fd918dab89122",
        "group": "c37aa1af20fe7b5e",
        "name": "",
        "order": 0,
        "width": 0,
        "height": 0,
        "format": "<style>\n.dot {\n  height: 8px;\n  width: 8px;\n  background-color: #bbb;\n  border-radius: 50%;\n  display: inline-block;\n}\ndiv.container {\n    width:98%; \n    height:98%; \n    margin:1%;\n  }\n  table#table1 {\n    text-align:center;\n    vertical-align: middle;\n    margin-left:auto; \n    margin-right:auto; \n    width:40px;\n    height:35px;\n  }\n  tr,td {\n    text-align:center;\n    vertical-align: middle;\n  }\n</style>\n<div class=\"container\">\n<table id = \"table1\">\n<tr>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[0]}}\"></div></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[1]}}\"></div></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[2]}}\"></div></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[3]}}\"></div></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[4]}}\"></div></td>\n<td></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[5]}}\"></div></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[6]}}\"></div></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[7]}}\"></div></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[8]}}\"></div></td>\n<td></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[9]}}\"></div></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[10]}}\"></div></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[11]}}\"></div></td>\n<td></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[12]}}\"></div></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[13]}}\"></div></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[14]}}\"></div></td>\n<td></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[15]}}\"></div></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[16]}}\"></div></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[17]}}\"></div></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[18]}}\"></div></td>\n<td><div class=\"dot\" style=\"background-color:{{msg.colours[19]}}\"></div></td>\n</tr>\n</table>\n</div>\n",
        "storeOutMessages": true,
        "fwdInMessages": true,
        "resendOnRefresh": true,
        "templateScope": "local",
        "x": 780,
        "y": 440,
        "wires": [
            []
        ]
    },
    {
        "id": "37422c92098f6fb3",
        "type": "function",
        "z": "ee0fd918dab89122",
        "name": "",
        "func": "let connectWiFiMaskX = 0;\nlet arrLengthOld = global.get('arrLength');\nlet connectWiFiMask0 = global.get('connectWiFiMask0');\nlet coloursExist = global.get('colours');\nlet colours = [];\nlet array = Object.entries(msg.payload);\narray = array.sort();\nglobal.set('arrLength', array.length + arrLengthOld);\n\nfor(let i=0; i<array.length; i++){\n    if(array[i][1] !== false){\n        var mask =  1 << i; // gets the 'i'th bit\n        if ((connectWiFiMaskX & mask) === 0) {\n            connectWiFiMaskX |= mask;\n        }\n        array[i][2] = 1;\n        colours[i] = \"#33ff55\";\n    } else {\n        array[i][2] = 0;\n        colours[i] = \"#FF3333\";\n    }\n}\nconnectWiFiMaskX = connectWiFiMaskX << arrLengthOld;\nconnectWiFiMask0 = connectWiFiMask0 + connectWiFiMaskX;\nglobal.set('connectWiFiMask0', connectWiFiMask0);\nlet coloursNow = coloursExist.concat(colours);\nglobal.set('colours', coloursNow);\nmsg.colours = coloursNow;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 620,
        "y": 320,
        "wires": [
            [
                "68168aaa6ccae55c"
            ]
        ]
    },
    {
        "id": "eee04ccbbca784a0",
        "type": "function",
        "z": "ee0fd918dab89122",
        "name": "",
        "func": "let connectWiFiMaskX = 0;\nlet arrLengthOld = global.get('arrLength');\nlet connectWiFiMask0 = global.get('connectWiFiMask0');\nlet coloursExist = global.get('colours');\nlet colours = [];\nlet array = Object.entries(msg.payload);\narray = array.sort();\nglobal.set('arrLength', array.length + arrLengthOld);\n\nfor(let i=0; i<array.length; i++){\n    if(array[i][1] !== false){\n        var mask =  1 << i; // gets the 'i'th bit\n        if ((connectWiFiMaskX & mask) === 0) {\n            connectWiFiMaskX |= mask;\n        }\n        array[i][2] = 1;\n        colours[i] = \"#33ff55\";\n    } else {\n        array[i][2] = 0;\n        colours[i] = \"#FF3333\";\n    }\n}\nconnectWiFiMaskX = connectWiFiMaskX << arrLengthOld;\nconnectWiFiMask0 = connectWiFiMask0 + connectWiFiMaskX;\nglobal.set('connectWiFiMask0', connectWiFiMask0);\nlet coloursNow = coloursExist.concat(colours);\nglobal.set('colours', coloursNow);\nmsg.colours = coloursNow;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 620,
        "y": 360,
        "wires": [
            [
                "05d006d36707cf84"
            ]
        ]
    },
    {
        "id": "43d64236567895cc",
        "type": "function",
        "z": "ee0fd918dab89122",
        "name": "",
        "func": "let connectWiFiMaskX = 0;\nlet arrLengthOld = global.get('arrLength');\nlet connectWiFiMask0 = global.get('connectWiFiMask0');\nlet coloursExist = global.get('colours');\nlet colours = [];\nlet array = Object.entries(msg.payload);\narray = array.sort();\nglobal.set('arrLength', array.length + arrLengthOld);\n\nfor(let i=0; i<array.length; i++){\n    if(array[i][1] !== false){\n        var mask =  1 << i; // gets the 'i'th bit\n        if ((connectWiFiMaskX & mask) === 0) {\n            connectWiFiMaskX |= mask;\n        }\n        array[i][2] = 1;\n        colours[i] = \"#33ff55\";\n    } else {\n        array[i][2] = 0;\n        colours[i] = \"#FF3333\";\n    }\n}\nconnectWiFiMaskX = connectWiFiMaskX << arrLengthOld;\nconnectWiFiMask0 = connectWiFiMask0 + connectWiFiMaskX;\nglobal.set('connectWiFiMask0', connectWiFiMask0);\nlet coloursNow = coloursExist.concat(colours);\nglobal.set('colours', coloursNow);\nmsg.colours = coloursNow;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 620,
        "y": 400,
        "wires": [
            [
                "8565b12ac7f59f4d"
            ]
        ]
    },
    {
        "id": "5abc019a95358832",
        "type": "function",
        "z": "ee0fd918dab89122",
        "name": "",
        "func": "let connectWiFiMaskX = 0;\nlet arrLengthOld = global.get('arrLength');\nlet connectWiFiMask0 = global.get('connectWiFiMask0');\nlet coloursExist = global.get('colours');\nlet colours = [];\nlet array = Object.entries(msg.payload);\narray = array.sort();\nglobal.set('arrLength', array.length + arrLengthOld);\n\nfor(let i=0; i<array.length; i++){\n    if(array[i][1] !== false){\n        var mask =  1 << i; // gets the 'i'th bit\n        if ((connectWiFiMaskX & mask) === 0) {\n            connectWiFiMaskX |= mask;\n        }\n        array[i][2] = 1;\n        colours[i] = \"#33ff55\";\n    } else {\n        array[i][2] = 0;\n        colours[i] = \"#FF3333\";\n    }\n}\nconnectWiFiMaskX = connectWiFiMaskX << arrLengthOld;\nconnectWiFiMask0 = connectWiFiMask0 + connectWiFiMaskX;\nglobal.set('connectWiFiMask0', connectWiFiMask0);\nlet coloursNow = coloursExist.concat(colours);\nglobal.set('colours', coloursNow);\nmsg.colours = coloursNow;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 620,
        "y": 440,
        "wires": [
            [
                "3689acf3743bbff1"
            ]
        ]
    },
    {
        "id": "c37aa1af20fe7b5e",
        "type": "ui_group",
        "name": "Bargraph",
        "tab": "33f5df14a5341315",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "33f5df14a5341315",
        "type": "ui_tab",
        "name": "Home",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

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