Function node code problems

I am trying to write some code for a function node and I've been trying for a while and can't get close.

I have three inputs -
24h time (cron node) topic - lights (1 on and 0 off depending on time of day)
MQTT node send 1 and 0 for open and close topic - raspberry
Payload with a number 1-100

If topic lights is a 0 -
msg on/off is sent following MQTT node. 0 send off 1 sends 0n.
If light topic is 1 -
msg on/off is sent following <> set points of payload.col11 input - hh

The on/off works for payload.col11 in a different node that was just doing that. Can't work out how to switch actions when light topic changes.

I hope this actually made some sense.

Thank you

[
    {
        "id": "762c3999b0ff6fb2",
        "type": "function",
        "z": "477a1a1df2873848",
        "name": "",
        "func": "let h = msg.payload.col11;\nlet hh = (h * 100);\n\nIf (msg.topic === light)\n0 = \nif (hh <= 63) msg.payload = \"on\";\nif (hh >63) msg.payload = \"off\";\n\n1 = \nmsg.topic === raspberry 0 = msg.payload = \"off\";\nmsg.topic === raspberry 1 = msg.payload = \"on\";\n\nreturn msg;",
        "outputs": 1,
        "noerr": 12,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 300,
        "y": 760,
        "wires": [
            [
                "960c09733082d103"
            ]
        ]
    }
]

I tried to import your flow - only item I get is a single 'function' node.
Can you re-check you have exported the complete flow.

1 Like
    {
        "id": "d58a0e9fb6feb9d6",
        "type": "tab",
        "label": "Flow 3",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "06d2e18ef69d8abd",
        "type": "csv",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "sep": ",",
        "hdrin": "",
        "hdrout": "none",
        "multi": "one",
        "ret": "\\n",
        "temp": "",
        "skip": "0",
        "strings": true,
        "include_empty_strings": "",
        "include_null_values": "",
        "x": 250,
        "y": 200,
        "wires": [
            [
                "91b18a714e7ed5f3"
            ]
        ]
    },
    {
        "id": "6e90d368a5032f98",
        "type": "mqtt in",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "topic": "123/test",
        "qos": "2",
        "datatype": "auto",
        "broker": "618e5d5400c6168e",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 90,
        "y": 200,
        "wires": [
            [
                "06d2e18ef69d8abd"
            ]
        ]
    },
    {
        "id": "91b18a714e7ed5f3",
        "type": "function",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "func": "delete msg.payload.col2\ndelete msg.payload.col3\ndelete msg.payload.col4\ndelete msg.payload.col5\ndelete msg.payload.col6\ndelete msg.payload.col7\ndelete msg.payload.col8\ndelete msg.payload.col9\ndelete msg.payload.col10\ndelete msg.payload.col12\ndelete msg.payload.col13\ndelete msg.payload.col14\ndelete msg.payload.col15\ndelete msg.payload.col16\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 260,
        "y": 320,
        "wires": [
            [
                "b5c3e425c9bc66fc"
            ]
        ]
    },
    {
        "id": "b5c3e425c9bc66fc",
        "type": "function",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "func": "let h = msg.payload.col11;\nlet hh = (h * 100);\n\nIf (msg.topic.light = 0)\nif (hh <= 63) msg.payload = \"on\";\nif (hh >63) msg.payload = \"off\";\n\nIf (msg.topic.light = 1) \nmsg.topic.raspberry = 0 = msg.payload = \"off\";\nmsg.topic.raspberry = 1 = msg.payload = \"on\";\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 280,
        "y": 420,
        "wires": [
            [
                "a6575cbf05985683"
            ]
        ]
    },
    {
        "id": "e6efe18c7be773a7",
        "type": "mqtt in",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "topic": "raspberry",
        "qos": "2",
        "datatype": "json",
        "broker": "f03eb60e5379e1a3",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 100,
        "y": 420,
        "wires": [
            [
                "b5c3e425c9bc66fc",
                "b10ae6d3f75ee816"
            ]
        ]
    },
    {
        "id": "625132ea514f5838",
        "type": "cronplus",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "outputField": "payload",
        "timeZone": "",
        "persistDynamic": false,
        "commandResponseMsgOutput": "output1",
        "outputs": 1,
        "options": [
            {
                "name": "schedule1",
                "topic": "light",
                "payloadType": "num",
                "payload": "1",
                "expressionType": "cron",
                "expression": "0 00 09 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule2",
                "topic": "light",
                "payloadType": "num",
                "payload": "0",
                "expressionType": "cron",
                "expression": "0 00 03 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            }
        ],
        "x": 100,
        "y": 500,
        "wires": [
            [
                "b5c3e425c9bc66fc",
                "b10ae6d3f75ee816"
            ]
        ]
    },
    {
        "id": "a6575cbf05985683",
        "type": "Sonoff device Enhanced",
        "z": "d58a0e9fb6feb9d6",
        "mode": "0",
        "broker": "618e5d5400c6168e",
        "device": "dry",
        "name": "H",
        "onValue": "ON",
        "offValue": "OFF",
        "toggleValue": "toggle",
        "cmdPrefix": "cmnd",
        "statPrefix": "stat",
        "telePrefix": "tele",
        "x": 450,
        "y": 420,
        "wires": [
            []
        ]
    },
    {
        "id": "b10ae6d3f75ee816",
        "type": "function",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "func": "let h = msg.payload.col11;\nlet hh = (h * 100);\n\nIf (msg.topic.light = 0)\nif (hh >= 65) msg.payload = \"on\";\nif (hh <= 65) msg.payload = \"off\";\n\nIf (msg.topic.light = 1) \nmsg.topic.raspberry = 1 = msg.payload = \"off\";\nmsg.topic.raspberry = 0 = msg.payload = \"on\";\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 280,
        "y": 500,
        "wires": [
            [
                "a01f4f92316c5cb0"
            ]
        ]
    },
    {
        "id": "a01f4f92316c5cb0",
        "type": "Sonoff device Enhanced",
        "z": "d58a0e9fb6feb9d6",
        "mode": "0",
        "broker": "618e5d5400c6168e",
        "device": "hum",
        "name": "D",
        "onValue": "ON",
        "offValue": "OFF",
        "toggleValue": "toggle",
        "cmdPrefix": "cmnd",
        "statPrefix": "stat",
        "telePrefix": "tele",
        "x": 450,
        "y": 500,
        "wires": [
            []
        ]
    },
    {
        "id": "618e5d5400c6168e",
        "type": "mqtt-broker",
        "name": "Mosquitto",
        "broker": "localhost",
        "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": {},
        "sessionExpiry": ""
    },
    {
        "id": "f03eb60e5379e1a3",
        "type": "mqtt-broker",
        "name": "rasp1",
        "broker": "192.168.0.59",
        "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": {},
        "sessionExpiry": ""
    }
]

Sorry I just copied the function node not the whole flow. I deleted the bits of the flow that doesn't have anything to do with this and posted the whole thing. The rest of the flow is handling reading from sensors and spitting them out to graph.

Umm, the code in your second function is

If (msg,topic = light)
0 = (msg.payload = )





return msg;

That is not valid code....

and in the other function node you have

let h = msg.payload.col11;
let hh = (h * 100);

If (msg.topic.light = 0)
if (hh <= 63) msg.payload = "on";
if (hh >63) msg.payload = "off";

If (msg.topic.light = 1) 
msg.topic.raspberry = 0 = msg.payload = "off";
msg.topic.raspberry = 1 = msg.payload = "on";

return msg;
  1. two of the if statements use a capital I
  2. are you intending to set msg.topic.light to the value of zero
If (msg.topic.light = 0)
  1. What are you attempting to do in this code:
If (msg.topic.light = 1) 
msg.topic.raspberry = 0 = msg.payload = "off";
msg.topic.raspberry = 1 = msg.payload = "on";

please explain it in words, i.s. "if msg.topic.light is equal to 1 then..."

1 Like

Adding to zenofmud comments.
These messages seem to be in separate messages inputs, so would probably need joining or saving to context, To be available in the function at the same time.

1 Like

The second function node will be the same as the first just the other way around. I thought I had made that adjustment, evidently not, sorry.

The raspberry MQTT node is sending out a '1 or 0' to the function node depending on what state a relay is in.

If the cron-plus node has sent a '1' then the function node sends a 'on' when the raspberry MQTT node sends a '1' and sends 'off' when it sends a '0'.

If the cron-plus has sent a '0' then the function node uses the msg.payload.coll11 to send 'on and off' commands to the tasmota nodes.

I have changed the flow with the updated details in the second node. Sorry for being a little thick.

Thank you very much for your reply.

Do they all need to be available at the same time as they are coming in at different times?

The cron-plus node is just sending two messages a day. A '1' at 9 am and a '0' at 3am.

The msg.payload.coll11 comes in every 30 seconds.

The Raspberry is switching every couple of minutes.

No its the value coming into the function node from the cron-plus node. 1 at 9 am and 0 at 3am. Its topic is light.

Sorry if that was obvious.

Yes they need to be in same message

this example may help

[{"id":"ecec141171102f04","type":"inject","z":"d58a0e9fb6feb9d6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"123/test","payload":"1,2,3,4,5,6,7,8,910,11,12","payloadType":"str","x":90,"y":240,"wires":[["14e7309b7e439f28"]]},{"id":"14e7309b7e439f28","type":"csv","z":"d58a0e9fb6feb9d6","name":"","sep":",","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":",,,,,,,,,test,","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":250,"y":220,"wires":[["ac69e144c7d17db6"]]},{"id":"ac69e144c7d17db6","type":"change","z":"d58a0e9fb6feb9d6","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.test","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":260,"wires":[["756075921f397694"]]},{"id":"756075921f397694","type":"join","z":"d58a0e9fb6feb9d6","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":250,"y":400,"wires":[["6942b0463bec6d09"]]},{"id":"4718f84ba02723ee","type":"inject","z":"d58a0e9fb6feb9d6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"raspberry","payload":"1","payloadType":"num","x":80,"y":340,"wires":[["756075921f397694"]]},{"id":"b97fb616c359c0ed","type":"inject","z":"d58a0e9fb6feb9d6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"raspberry","payload":"0","payloadType":"num","x":80,"y":380,"wires":[["756075921f397694"]]},{"id":"e624cc07c16ecc98","type":"inject","z":"d58a0e9fb6feb9d6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cron","payload":"1","payloadType":"num","x":70,"y":480,"wires":[["756075921f397694"]]},{"id":"361f4e96428ae8f1","type":"inject","z":"d58a0e9fb6feb9d6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cron","payload":"0","payloadType":"num","x":70,"y":520,"wires":[["756075921f397694"]]},{"id":"6942b0463bec6d09","type":"debug","z":"d58a0e9fb6feb9d6","name":"debug 308","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":350,"y":460,"wires":[]}]

After the 3 messages are received the join will output, then it will output when ever any of the messages are received. You can obviously can initiate the first 3 messages with injects , more so the cron as it will only send twice a day.

[edit]
You coud probably do the rest with 3 switch nodes using msg.payload.cron, msg.payload.raspberry, msg.payload["123/test"] as the three switch properties. No need for any function node/s.

1 Like

Excellent, thank you. I just tried it and see exactly what you mean.

Sorry I mean the join. Still trying to work out the switch nodes. Thank you very much for your help.

Thank you all for your help. I think I am nearly there, but the function node has an error - 'function tried to send a message of type string'.

Couldn't get my head around how to make it work with switch nodes.

[
    {
        "id": "d58a0e9fb6feb9d6",
        "type": "tab",
        "label": "Flow 3",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "b5c3e425c9bc66fc",
        "type": "function",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "func": "let h = msg.payload.col1\nlet r = msg.payload.raspberry\nlet l = msg.payload.light\n\nif (l==0){\nif (h <= 63) msg.payload = \"on\";\nif (h >63) msg.payload = \"off\";\n}\n\nif (l==1){\nif (r == 0)\nreturn [msg.payload = \"on\"]\nif (r == 1)\nreturn [msg.payload = \"off\"]\n}\n\nreturn msg;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 520,
        "y": 780,
        "wires": [
            [
                "a6575cbf05985683",
                "f27e8e3d8502edd8"
            ]
        ]
    },
    {
        "id": "a6575cbf05985683",
        "type": "Sonoff device Enhanced",
        "z": "d58a0e9fb6feb9d6",
        "mode": "0",
        "broker": "618e5d5400c6168e",
        "device": "dry",
        "name": "H",
        "onValue": "ON",
        "offValue": "OFF",
        "toggleValue": "toggle",
        "cmdPrefix": "cmnd",
        "statPrefix": "stat",
        "telePrefix": "tele",
        "x": 730,
        "y": 780,
        "wires": [
            []
        ]
    },
    {
        "id": "a01f4f92316c5cb0",
        "type": "Sonoff device Enhanced",
        "z": "d58a0e9fb6feb9d6",
        "mode": "0",
        "broker": "618e5d5400c6168e",
        "device": "hum",
        "name": "D",
        "onValue": "ON",
        "offValue": "OFF",
        "toggleValue": "toggle",
        "cmdPrefix": "cmnd",
        "statPrefix": "stat",
        "telePrefix": "tele",
        "x": 730,
        "y": 840,
        "wires": [
            []
        ]
    },
    {
        "id": "8c365de7bc347d93",
        "type": "inject",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "123/test",
        "payload": "64",
        "payloadType": "str",
        "x": 180,
        "y": 660,
        "wires": [
            [
                "1f4de87ca2d02a18"
            ]
        ]
    },
    {
        "id": "e51d38ab37b6ee27",
        "type": "join",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": true,
        "timeout": "",
        "count": "3",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 350,
        "y": 820,
        "wires": [
            [
                "bec8151e84814f6a",
                "9ba1419ecb1f5f99",
                "b5c3e425c9bc66fc"
            ]
        ]
    },
    {
        "id": "688d1bf943e84c4b",
        "type": "inject",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "raspberry",
        "payload": "1",
        "payloadType": "num",
        "x": 170,
        "y": 780,
        "wires": [
            [
                "e51d38ab37b6ee27"
            ]
        ]
    },
    {
        "id": "79cda72e5db0ecdd",
        "type": "inject",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "raspberry",
        "payload": "0",
        "payloadType": "num",
        "x": 170,
        "y": 820,
        "wires": [
            [
                "e51d38ab37b6ee27"
            ]
        ]
    },
    {
        "id": "bec8151e84814f6a",
        "type": "debug",
        "z": "d58a0e9fb6feb9d6",
        "name": "debug 308",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 430,
        "y": 900,
        "wires": []
    },
    {
        "id": "631b7a4ecf4d1a79",
        "type": "mqtt in",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "topic": "123/test",
        "qos": "2",
        "datatype": "auto",
        "broker": "618e5d5400c6168e",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 190,
        "y": 600,
        "wires": [
            []
        ]
    },
    {
        "id": "b1071f7e377bab24",
        "type": "function",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "func": "delete msg.payload.col2\ndelete msg.payload.col3\ndelete msg.payload.col4\ndelete msg.payload.col5\ndelete msg.payload.col6\ndelete msg.payload.col7\ndelete msg.payload.col8\ndelete msg.payload.col9\ndelete msg.payload.col10\ndelete msg.payload.col12\ndelete msg.payload.col13\ndelete msg.payload.col14\ndelete msg.payload.col15\ndelete msg.payload.col16\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 360,
        "y": 720,
        "wires": [
            [
                "e51d38ab37b6ee27"
            ]
        ]
    },
    {
        "id": "1f4de87ca2d02a18",
        "type": "csv",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "sep": ",",
        "hdrin": "",
        "hdrout": "none",
        "multi": "one",
        "ret": "\\n",
        "temp": "",
        "skip": "0",
        "strings": true,
        "include_empty_strings": "",
        "include_null_values": "",
        "x": 370,
        "y": 620,
        "wires": [
            [
                "b1071f7e377bab24"
            ]
        ]
    },
    {
        "id": "918e93aa6b4e4fcc",
        "type": "cronplus",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "outputField": "payload",
        "timeZone": "",
        "persistDynamic": false,
        "commandResponseMsgOutput": "output1",
        "outputs": 1,
        "options": [
            {
                "name": "schedule1",
                "topic": "light",
                "payloadType": "num",
                "payload": "1",
                "expressionType": "cron",
                "expression": "0 00 09 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule2",
                "topic": "light",
                "payloadType": "num",
                "payload": "0",
                "expressionType": "cron",
                "expression": "0 00 03 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            }
        ],
        "x": 160,
        "y": 900,
        "wires": [
            [
                "e51d38ab37b6ee27"
            ]
        ]
    },
    {
        "id": "ec4bd2cfac87ace7",
        "type": "mqtt in",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "topic": "raspberry",
        "qos": "2",
        "datatype": "json",
        "broker": "f03eb60e5379e1a3",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 180,
        "y": 720,
        "wires": [
            [
                "e51d38ab37b6ee27"
            ]
        ]
    },
    {
        "id": "e2989af89ef6c16d",
        "type": "inject",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "light",
        "payload": "1",
        "payloadType": "num",
        "x": 170,
        "y": 960,
        "wires": [
            [
                "e51d38ab37b6ee27"
            ]
        ]
    },
    {
        "id": "82a86590ec234930",
        "type": "inject",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "light",
        "payload": "0",
        "payloadType": "num",
        "x": 170,
        "y": 1000,
        "wires": [
            [
                "e51d38ab37b6ee27"
            ]
        ]
    },
    {
        "id": "3294594606d6817d",
        "type": "switch",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "property": "topic",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "raspberry = 1",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "raspberry = 0",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 450,
        "y": 1060,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "76280934705d17de",
        "type": "debug",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 690,
        "y": 900,
        "wires": []
    },
    {
        "id": "9ba1419ecb1f5f99",
        "type": "function",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "func": "let h = msg.payload.col1\nlet r = msg.payload.raspberry\nlet l = msg.payload.light\n\nif (l==0){\nif (h >= 65) msg.payload = \"on\";\nif (h <65) msg.payload = \"off\";\n}\n\nif (l==1){\nif (r == 1)\nreturn [msg.payload = \"on\"]\nif (r == 0)\nreturn [msg.payload = \"off\"]\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 520,
        "y": 840,
        "wires": [
            [
                "76280934705d17de",
                "a01f4f92316c5cb0"
            ]
        ]
    },
    {
        "id": "f27e8e3d8502edd8",
        "type": "debug",
        "z": "d58a0e9fb6feb9d6",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 690,
        "y": 720,
        "wires": []
    },
    {
        "id": "618e5d5400c6168e",
        "type": "mqtt-broker",
        "name": "Mosquitto",
        "broker": "localhost",
        "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": {},
        "sessionExpiry": ""
    },
    {
        "id": "f03eb60e5379e1a3",
        "type": "mqtt-broker",
        "name": "rasp1",
        "broker": "192.168.0.59",
        "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": {},
        "sessionExpiry": ""
    }
]

Thanks again for your help. Could get my head around the switch nodes to do this so went back to the function node, but my coding not up to scratch as still getting an error?

Thanks again for your help. Tried to redo the function node using some of my old code and one of Steve's examples but still getting an error. Updated the flow above.

Look at your code. What is the difference between the two if blocks?

1 Like

Really sorry to be stupid, but I don't understand what you mean?

The return statement exits from a function, passing on an object or an array of objects

msg.payload = "foo"  // msg is already an object
return msg

Or

let msg2 = {}  // a new object
msg.payload = "foo"
msg2.payload = "bar"
msg2.meaningoflife = 42
return [msg, msg2]  // an array of objects

msg.payload = "off" is a perectly valid assignment
but return [msg.payload = "on"] is nonsense because msg.payload = "on" is not an object

Also it sometimes makes sense to have more than one return statement in a function but not in this case.

1 Like

What is the difference between the block of code starting with
if (l == 0)
And the code in the
if (l == 1)

My guess is you are not very familiar with JavaScript so I would suggest you take a tutorial (do a google search and you will find many)

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

1 Like