Why is this reporting Undefined?

[
    {
        "id": "cdd592f25ffd2417",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "3a363a0b81b1330f",
        "type": "function",
        "z": "cdd592f25ffd2417",
        "name": "All Off, except scheduled",
        "func": "//On True, turn off all other zones\n\nlet minZN=1; let maxZN=65;\nlet onZN=[1,3,5];\n\nlet i = minZN;\n\nnode.status({fill:\"red\", shape:\"ring\", text:\" \"});\n\nznLoop(msg.payload)\n    \nnode.status({fill:\"blue\", shape:\"ring\", text:' '});\n\nfunction znLoop(state) {\n    \n    setTimeout(function(){\n        \n        if (state && onZN.indexOf(i) == -1) {\n            msg.payload.zone = i\n            msg.payload.state = 0\n\n        } else {\n            msg.payload.zone = i\n            msg.payload.state = 1\n\n        }\n        \n        node.send(msg) \n        \n        i++\n        if (i < maxZN) {\n            znLoop(); }\n            \n    },1000)\n\n}\n\nreturn;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 530,
        "y": 320,
        "wires": [
            [
                "766a58d3e5670768"
            ]
        ]
    },
    {
        "id": "42641aeb99af6849",
        "type": "inject",
        "z": "cdd592f25ffd2417",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "true",
        "payloadType": "bool",
        "x": 320,
        "y": 320,
        "wires": [
            [
                "3a363a0b81b1330f"
            ]
        ]
    },
    {
        "id": "766a58d3e5670768",
        "type": "debug",
        "z": "cdd592f25ffd2417",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload.zone",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 800,
        "y": 320,
        "wires": []
    },
    {
        "id": "3444e4d2f15151cf",
        "type": "inject",
        "z": "cdd592f25ffd2417",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "false",
        "payloadType": "bool",
        "x": 320,
        "y": 380,
        "wires": [
            [
                "3a363a0b81b1330f"
            ]
        ]
    }
]

Hi .. you cannot add properties to msg.payload if initially it was a different type.
(in this case boolean)

define it as an object .. and tag on as many properties you like :wink:

 setTimeout(function () {

        msg.payload = {}  //  empty object

        if (state && onZN.indexOf(i) == -1) {
            msg.payload.zone = i
            msg.payload.state = 0
   

        } else {
            msg.payload.zone = i
            msg.payload.state = 1
      
        }

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