Function node and handling null as content of message output

I have a function that produces an array of 2 message objects, one or both can be null. If I read the documentation("If null is returned, either by itself or as an element of the array, no message is passed on")., there should not be any message for the null element. However, when I connect the outputs to 2 debug nodes for each output I see 2 messages with the value of null coming to them.
In my flow, the next node does not handle it well, and problems arise. This node after the Function node was a contrib and will not be maintained regularly. I trusted the Function node's null behaviour.

Are you returning null in the payload or the msg.

e.g. this WILL output a message...

msg.payload = null
return msg

e.g. this WILL NOT output a message...

return null

I did say msg = null

Please share a flow that demonstrates your issue (& perhaps paste a screenshot too). Use CTRL+E to export the selected nodes.

Additionally, please state the version of Node-RED you are using.

Hopefully you can investigate this:

[
    {
        "id": "ab2cf5ae7c074038",
        "type": "debug",
        "z": "deca61f3.aac",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1160,
        "y": 240,
        "wires": []
    },
    {
        "id": "d52705674aafd8d1",
        "type": "nefit-easy",
        "z": "deca61f3.aac",
        "easyconfig": "174fc54fb6089627",
        "name": "Verhoog setpoint halve graad indien temp <= setpoint +0,5grC",
        "topic": "nefit-easy",
        "command": "set-temperature",
        "value": "setpoint + 0.5",
        "x": 810,
        "y": 300,
        "wires": [
            [
                "2261b040630bbab3"
            ]
        ]
    },
    {
        "id": "31a36aef529b6397",
        "type": "function",
        "z": "deca61f3.aac",
        "name": "Manupileer  setpoint",
        "func": "let temp_act = parseFloat(msg.payload[\"in house temp\"]);\nlet setpoint = parseFloat(msg.payload[\"temp setpoint\"]);\nlet afgekoeld;\nlet opgewarmd;\nif (flow.get(\"Verlagen\") != true) flow.set(\"Verlagen\", false);\n\nlet msg1 = {};\nlet msg2 = {};\nmsg1.payload = null;\nmsg2.payload = null;\nif (temp_act > setpoint) { opgewarmd = true } else { opgewarmd = false };\nif (msg.payload[\"boiler indicator\"] == \"central heating\" && opgewarmd) {\n    flow.set('Verlagen', true);\n    msg1.payload = 1;\n    msg2.payload=null;\n    node.warn(\"setpoint halve graad \\lager\");\n};\nif (temp_act < (setpoint + 0.5)) { afgekoeld = true } else { afgekoeld = false };\nif (msg.payload[\"boiler indicator\"] == \"off\" && afgekoeld && flow.get(\"Verlagen\")) {\n    flow.set('Verlagen', false);\n    msg1.payload=null;\n    msg2.payload = 2;\n    node.warn(\"setpoint halve graad ^hoger\");\n};\nnode.warn([\"Verlagen = \" + flow.get(\"Verlagen\"), temp_act, setpoint]);\n\nreturn([msg1, msg2]);",
        "outputs": 2,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 190,
        "y": 380,
        "wires": [
            [
                "ab2cf5ae7c074038"
            ],
            [
                "2261b040630bbab3"
            ]
        ]
    },
    {
        "id": "1d110ba7ba5a0873",
        "type": "nefit-easy",
        "z": "deca61f3.aac",
        "easyconfig": "174fc54fb6089627",
        "name": "Verlaag temperatuur 0,5grC indien temp>= setpoint",
        "topic": "nefit-easy",
        "command": "set-temperature",
        "value": "setpoint - 0.5",
        "x": 770,
        "y": 240,
        "wires": [
            [
                "ab2cf5ae7c074038"
            ]
        ]
    },
    {
        "id": "41e6f8eb908732f6",
        "type": "inject",
        "z": "deca61f3.aac",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "2",
        "payloadType": "num",
        "x": 90,
        "y": 300,
        "wires": [
            [
                "65abf292185fe233"
            ]
        ]
    },
    {
        "id": "51b2b60054991b40",
        "type": "inject",
        "z": "deca61f3.aac",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "1",
        "payloadType": "num",
        "x": 90,
        "y": 240,
        "wires": [
            [
                "042fe2214f13a7a7",
                "ab2cf5ae7c074038"
            ]
        ]
    },
    {
        "id": "042fe2214f13a7a7",
        "type": "change",
        "z": "deca61f3.aac",
        "name": "Verlaag commando",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "1",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 430,
        "y": 240,
        "wires": [
            [
                "1d110ba7ba5a0873"
            ]
        ]
    },
    {
        "id": "65abf292185fe233",
        "type": "change",
        "z": "deca61f3.aac",
        "name": "Verhoog commando",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "2",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 440,
        "y": 300,
        "wires": [
            [
                "d52705674aafd8d1"
            ]
        ]
    },
    {
        "id": "2261b040630bbab3",
        "type": "debug",
        "z": "deca61f3.aac",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1160,
        "y": 300,
        "wires": []
    },
    {
        "id": "174fc54fb6089627",
        "type": "nefit-easy-config",
        "serialNumber": "820921714",
        "accessKey": "Gtufnb5VQUS9vUMg",
        "password": "RienbrwrKT",
        "timeout": "30",
        "filterInfoMessages": false
    }
]

Admin Edit. Wrap code in triple backticks

It is the JSON of the piece of code we are talking about! NR version is 4.0.8!

I assume you mean the JavaScript in your function node? (there is no "JavaScript Object Notation" strings (aka JSON) in your flow.

So assuming you mean the function named Manupileer setpoint then as I pointed out in my first response, if you return an object, regardless of its content, the function WILL send a message.

Ok, any suggestion how to not send a message when msg1.payloadf =null on different outputs?

you could use a switch node that tests msg.payload is "something" (allowing it to pass/not pass

or, inside the function, set msg1 to null itself

or do a ternary test on msg.payload!

return [ 
    msg1.payload === null ? null : msg1,
    msg2.payload === null ? null : msg2,
]

or do a check...

if (msg1.payload) {
    node.send(msg1)
}
if (msg2.payload) {
    node.send(msg2)
}
// no return required
1 Like

I tried the first solution and that did the trick. I am not so fluent in writing these things in NR, a bit rusty, soI could not find out
Thanks for the help.

1 Like