Msg.payload : undefined

Hello,

I have Hex-string which i am receiving from a Topic and converting in a function to binary then splitting the bits as to see in the function below and then sending the outputs at 8 different LEDS.
By receiving 0xFF there is no problem all LEDs are being green. but say receiving 0x04 ...i think the function parseInt(msg.payload, 16).toString(2) is producing only "100" after debugging and the other LEDs are becoming undefined value. probably because i am trying to access empty memory. but i have no idea how to solve this... anyone have an idea? how can i build for example a loop which fills only 1 in 8 bit long array for example where the 1 are set and keep 0 where is 0 or undefined or empty?



let output = parseInt(msg.payload, 16).toString(2)

var msg0 = { payload: output[0] };

var msg1 = { payload: output[1] };

var msg2 = { payload: output[2] };

var msg3= { payload: output[3] };

var msg4 = { payload: output[4] };

var msg5 = { payload: output[5] };

var msg6 = { payload: output[6] };

var msg7 = { payload: output[7] };

return [msg0,msg1, msg2, msg3, msg4, msg5, msg6, msg7];

here also my exported stuff:

[
    {
        "id": "a1989ef9df599a1c",
        "type": "mqtt in",
        "z": "4235621c632ad702",
        "name": "",
        "topic": "p2007/slaves/00/in/status",
        "qos": "2",
        "datatype": "auto",
        "broker": "0c31295bc495e7ce",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 790,
        "y": 40,
        "wires": [
            [
                "5a6a0d9804aedcce",
                "c372e9d957a298e0"
            ]
        ]
    },
    {
        "id": "5a6a0d9804aedcce",
        "type": "ui_text",
        "z": "4235621c632ad702",
        "group": "39308d72b1563b23",
        "order": 33,
        "width": "3",
        "height": "1",
        "name": "IN_STATUS_0",
        "label": "00: STATUS",
        "format": "{{msg.payload}}",
        "layout": "row-spread",
        "className": "",
        "x": 1000,
        "y": 40,
        "wires": []
    },
    {
        "id": "c372e9d957a298e0",
        "type": "function",
        "z": "4235621c632ad702",
        "name": "function 7",
        "func": "let output = parseInt(msg.payload, 16).toString(2)\n\n\nvar msg0 = { payload: output[0] };\nvar msg1 = { payload: output[1] };\nvar msg2 = { payload: output[2] };\nvar msg3= { payload: output[3] };\nvar msg4 = { payload: output[4] };\nvar msg5 = { payload: output[5] };\nvar msg6 = { payload: output[6] };\nvar msg7 = { payload: output[7] };\n\n\nreturn [msg0,msg1, msg2, msg3, msg4, msg5, msg6, msg7]; ",
        "outputs": 8,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 840,
        "y": 240,
        "wires": [
            [
                "a8a3b00c58e721eb"
            ],
            [
                "542649152d6f0d2d"
            ],
            [
                "ebe8907af4be6534"
            ],
            [
                "45e9e9994c8a7630"
            ],
            [
                "a33f02070930bc20"
            ],
            [
                "a98c48d0c95d9387"
            ],
            [
                "f7c54db71fe14909"
            ],
            [
                "eb647ad2daba09b7"
            ]
        ]
    },
    {
        "id": "45e9e9994c8a7630",
        "type": "ui_led",
        "z": "4235621c632ad702",
        "order": 3,
        "group": "9ff13a5ef6d8935c",
        "width": "1",
        "height": "1",
        "label": "",
        "labelPlacement": "left",
        "labelAlignment": "left",
        "colorForValue": [
            {
                "color": "#808080",
                "value": "0",
                "valueType": "str"
            },
            {
                "color": "#008000",
                "value": "1",
                "valueType": "str"
            },
            {
                "color": "#808080",
                "value": "null",
                "valueType": "str"
            }
        ],
        "allowColorForValueInMessage": true,
        "shape": "square",
        "showGlow": true,
        "name": "BIT[4]",
        "x": 1250,
        "y": 240,
        "wires": []
    },
    {
        "id": "a98c48d0c95d9387",
        "type": "ui_led",
        "z": "4235621c632ad702",
        "order": 14,
        "group": "9ff13a5ef6d8935c",
        "width": "1",
        "height": "1",
        "label": "",
        "labelPlacement": "left",
        "labelAlignment": "left",
        "colorForValue": [
            {
                "color": "#808080",
                "value": "0",
                "valueType": "str"
            },
            {
                "color": "#008000",
                "value": "1",
                "valueType": "str"
            },
            {
                "color": "#808080",
                "value": "null",
                "valueType": "str"
            }
        ],
        "allowColorForValueInMessage": false,
        "shape": "square",
        "showGlow": true,
        "name": "BIT[2]",
        "x": 1250,
        "y": 320,
        "wires": []
    },
    {
        "id": "a33f02070930bc20",
        "type": "ui_led",
        "z": "4235621c632ad702",
        "order": 13,
        "group": "9ff13a5ef6d8935c",
        "width": "1",
        "height": "1",
        "label": "",
        "labelPlacement": "left",
        "labelAlignment": "left",
        "colorForValue": [
            {
                "color": "#808080",
                "value": "0",
                "valueType": "str"
            },
            {
                "color": "#008000",
                "value": "1",
                "valueType": "str"
            },
            {
                "color": "#808080",
                "value": "null",
                "valueType": "str"
            }
        ],
        "allowColorForValueInMessage": false,
        "shape": "square",
        "showGlow": true,
        "name": "BIT[3]",
        "x": 1250,
        "y": 280,
        "wires": []
    },
    {
        "id": "ebe8907af4be6534",
        "type": "ui_led",
        "z": "4235621c632ad702",
        "order": 12,
        "group": "9ff13a5ef6d8935c",
        "width": "1",
        "height": "1",
        "label": "",
        "labelPlacement": "left",
        "labelAlignment": "left",
        "colorForValue": [
            {
                "color": "#808080",
                "value": "0",
                "valueType": "str"
            },
            {
                "color": "#008000",
                "value": "1",
                "valueType": "str"
            },
            {
                "color": "#808080",
                "value": "false",
                "valueType": "bool"
            }
        ],
        "allowColorForValueInMessage": true,
        "shape": "square",
        "showGlow": true,
        "name": "BIT[5]",
        "x": 1250,
        "y": 200,
        "wires": []
    },
    {
        "id": "eb647ad2daba09b7",
        "type": "ui_led",
        "z": "4235621c632ad702",
        "order": 16,
        "group": "9ff13a5ef6d8935c",
        "width": "1",
        "height": "1",
        "label": "",
        "labelPlacement": "left",
        "labelAlignment": "left",
        "colorForValue": [
            {
                "color": "#808080",
                "value": "0",
                "valueType": "str"
            },
            {
                "color": "#008000",
                "value": "1",
                "valueType": "str"
            },
            {
                "color": "#808080",
                "value": "null",
                "valueType": "str"
            }
        ],
        "allowColorForValueInMessage": false,
        "shape": "square",
        "showGlow": true,
        "name": "BIT[0]",
        "x": 1250,
        "y": 400,
        "wires": []
    },
    {
        "id": "f7c54db71fe14909",
        "type": "ui_led",
        "z": "4235621c632ad702",
        "order": 15,
        "group": "9ff13a5ef6d8935c",
        "width": "1",
        "height": "1",
        "label": "",
        "labelPlacement": "left",
        "labelAlignment": "left",
        "colorForValue": [
            {
                "color": "#808080",
                "value": "0",
                "valueType": "str"
            },
            {
                "color": "#008000",
                "value": "1",
                "valueType": "str"
            },
            {
                "color": "#808080",
                "value": "null",
                "valueType": "str"
            }
        ],
        "allowColorForValueInMessage": false,
        "shape": "square",
        "showGlow": true,
        "name": "BIT[1]",
        "x": 1250,
        "y": 360,
        "wires": []
    },
    {
        "id": "542649152d6f0d2d",
        "type": "ui_led",
        "z": "4235621c632ad702",
        "order": 16,
        "group": "9ff13a5ef6d8935c",
        "width": "1",
        "height": "1",
        "label": "",
        "labelPlacement": "left",
        "labelAlignment": "left",
        "colorForValue": [
            {
                "color": "#808080",
                "value": "0",
                "valueType": "str"
            },
            {
                "color": "#008000",
                "value": "1",
                "valueType": "str"
            },
            {
                "color": "#808080",
                "value": "false",
                "valueType": "bool"
            }
        ],
        "allowColorForValueInMessage": true,
        "shape": "square",
        "showGlow": true,
        "name": "BIT[6]",
        "x": 1250,
        "y": 160,
        "wires": []
    },
    {
        "id": "a8a3b00c58e721eb",
        "type": "ui_led",
        "z": "4235621c632ad702",
        "order": 15,
        "group": "9ff13a5ef6d8935c",
        "width": "1",
        "height": "1",
        "label": "",
        "labelPlacement": "left",
        "labelAlignment": "left",
        "colorForValue": [
            {
                "color": "#808080",
                "value": "0",
                "valueType": "str"
            },
            {
                "color": "#008000",
                "value": "1",
                "valueType": "str"
            },
            {
                "color": "#808080",
                "value": "false",
                "valueType": "bool"
            }
        ],
        "allowColorForValueInMessage": true,
        "shape": "square",
        "showGlow": true,
        "name": "BIT[7]",
        "x": 1250,
        "y": 120,
        "wires": []
    },
    {
        "id": "0c31295bc495e7ce",
        "type": "mqtt-broker",
        "name": "IPC",
        "broker": "10.100.20.54",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    },
    {
        "id": "39308d72b1563b23",
        "type": "ui_group",
        "name": "Prozessdaten (INPUT)",
        "tab": "a5b09d62acbae35a",
        "order": 4,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "9ff13a5ef6d8935c",
        "type": "ui_group",
        "name": "00: STATUS:LEDs",
        "tab": "a5b09d62acbae35a",
        "order": 9,
        "disp": true,
        "width": "8",
        "collapse": false,
        "className": ""
    },
    {
        "id": "a5b09d62acbae35a",
        "type": "ui_tab",
        "name": "EC-Produkttest",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

try parseInt("0x04", 16).toString(2).padStart(8, "0")

padStart will fill the start up to 8 spaces (if needed) with "0"

1 Like

Personally, I avoid converting a perfectly good number to a hex string

Try this instead...

const value = msg.payload
const testBit = (v, b) => ((v>>b) % 2 != 0) ? 1 : 0
const msg0 = { payload: testBit(value, 0) };
const msg1 = { payload: testBit(value, 1) };
const msg2 = { payload: testBit(value, 2) };
const msg3 = { payload: testBit(value, 3) };
const msg4 = { payload: testBit(value, 4) };
const msg5 = { payload: testBit(value, 5) };
const msg6 = { payload: testBit(value, 6) };
const msg7 = { payload: testBit(value, 7) };

return [msg0, msg1, msg2, msg3, msg4, msg5, msg6, msg7];
1 Like

Thanks you for the solution!worked :slight_smile:

this worked aswell! many thanks :slight_smile:

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