Function node code problems

The difference is supposed to be if -

L = 0 it sends on/off depending on the value h
if
L = 1 it sends on/off depending if r = 0 or 1.

Compare how you are returning the results

1 Like

Here is how it would be done with no code, using switch nodes
I think the logic is correct but had to look at your function so may be wrong.

[{"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":"70","payloadType":"str","x":180,"y":660,"wires":[["1f4de87ca2d02a18"]]},{"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":[["74972ad4ebf0f745"]]},{"id":"d6ac64cdbfdaec55","type":"inject","z":"d58a0e9fb6feb9d6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"123/test","payload":"60","payloadType":"str","x":170,"y":700,"wires":[["1f4de87ca2d02a18"]]},{"id":"74972ad4ebf0f745","type":"change","z":"d58a0e9fb6feb9d6","name":"","rules":[{"t":"move","p":"payload.col1","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"test","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":700,"wires":[["e51d38ab37b6ee27"]]},{"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","3d5240314528daaf"]]},{"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":"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":"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":"3d5240314528daaf","type":"switch","z":"d58a0e9fb6feb9d6","name":"light 0 or 1","property":"payload.light","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":550,"y":820,"wires":[["ab5aa9c4d0b0b214"],["03c0a53faf3d264e"]]},{"id":"ab5aa9c4d0b0b214","type":"switch","z":"d58a0e9fb6feb9d6","name":"123/test <= 63","property":"payload.test","propertyType":"msg","rules":[{"t":"lte","v":"63","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":700,"y":780,"wires":[["d136bcc6f8f1403d","1e6ce190a1ebf511"],["57b068e8d10de517","325d2d64238dfae9"]]},{"id":"03c0a53faf3d264e","type":"switch","z":"d58a0e9fb6feb9d6","name":"1raspbery 0 or 1","property":"payload.raspberry","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":700,"y":840,"wires":[["d136bcc6f8f1403d","1e6ce190a1ebf511"],["57b068e8d10de517","325d2d64238dfae9"]]},{"id":"d136bcc6f8f1403d","type":"change","z":"d58a0e9fb6feb9d6","name":"payload on","rules":[{"t":"set","p":"payload","pt":"msg","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":930,"y":740,"wires":[["f27e8e3d8502edd8"]]},{"id":"57b068e8d10de517","type":"change","z":"d58a0e9fb6feb9d6","name":"payload off","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":930,"y":780,"wires":[["f27e8e3d8502edd8"]]},{"id":"1e6ce190a1ebf511","type":"change","z":"d58a0e9fb6feb9d6","name":"payload off","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":930,"y":880,"wires":[["76280934705d17de"]]},{"id":"325d2d64238dfae9","type":"change","z":"d58a0e9fb6feb9d6","name":"payload on","rules":[{"t":"set","p":"payload","pt":"msg","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":930,"y":840,"wires":[["76280934705d17de"]]},{"id":"f27e8e3d8502edd8","type":"debug","z":"d58a0e9fb6feb9d6","name":"300","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1130,"y":760,"wires":[]},{"id":"76280934705d17de","type":"debug","z":"d58a0e9fb6feb9d6","name":"400","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1130,"y":860,"wires":[]}]
1 Like

Will definitely watch that in a mo. Thanks.

Nearly got it working removing the returns. L = 0 doesn't work, passes previous string, not on/off. It did work for a min, not sure what I changed.

Thank you. That works!

Thank you very much to everyone who helped me. Very much appreciated.

Here is the full working flow if anyone is looking at this thread for ideas.

I will try and get better at code and get it working with the function node. Didn't look like I was too far away.

Have a great day!

[
    {
        "id": "d32e63247484353d",
        "type": "Sonoff device Enhanced",
        "z": "477a1a1df2873848",
        "mode": "0",
        "broker": "618e5d5400c6168e",
        "device": "hum",
        "name": "D",
        "onValue": "ON",
        "offValue": "OFF",
        "toggleValue": "toggle",
        "cmdPrefix": "cmnd",
        "statPrefix": "stat",
        "telePrefix": "tele",
        "x": 910,
        "y": 780,
        "wires": [
            []
        ]
    },
    {
        "id": "960c09733082d103",
        "type": "Sonoff device Enhanced",
        "z": "477a1a1df2873848",
        "mode": "0",
        "broker": "618e5d5400c6168e",
        "device": "dry",
        "name": "H",
        "onValue": "ON",
        "offValue": "OFF",
        "toggleValue": "toggle",
        "cmdPrefix": "cmnd",
        "statPrefix": "stat",
        "telePrefix": "tele",
        "x": 910,
        "y": 680,
        "wires": [
            []
        ]
    },
    {
        "id": "0402be4a53f5a346",
        "type": "change",
        "z": "477a1a1df2873848",
        "name": "",
        "rules": [
            {
                "t": "move",
                "p": "payload.col11",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "test",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 300,
        "y": 580,
        "wires": [
            [
                "0bb675eee7cf2b76"
            ]
        ]
    },
    {
        "id": "3f939e8c0d640e59",
        "type": "join",
        "z": "477a1a1df2873848",
        "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": 330,
        "y": 740,
        "wires": [
            [
                "a69dbcf04839b569",
                "a768af12e5ad67b2"
            ]
        ]
    },
    {
        "id": "02b89014e14395ba",
        "type": "inject",
        "z": "477a1a1df2873848",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "raspberry",
        "payload": "1",
        "payloadType": "num",
        "x": 130,
        "y": 740,
        "wires": [
            [
                "3f939e8c0d640e59"
            ]
        ]
    },
    {
        "id": "877007e8dd221b64",
        "type": "inject",
        "z": "477a1a1df2873848",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "raspberry",
        "payload": "0",
        "payloadType": "num",
        "x": 130,
        "y": 780,
        "wires": [
            [
                "3f939e8c0d640e59"
            ]
        ]
    },
    {
        "id": "ad5dde851ad28d53",
        "type": "inject",
        "z": "477a1a1df2873848",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "light",
        "payload": "1",
        "payloadType": "num",
        "x": 150,
        "y": 900,
        "wires": [
            [
                "3f939e8c0d640e59"
            ]
        ]
    },
    {
        "id": "f3c5d3b3afa30ce8",
        "type": "inject",
        "z": "477a1a1df2873848",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "light",
        "payload": "0",
        "payloadType": "num",
        "x": 150,
        "y": 940,
        "wires": [
            [
                "3f939e8c0d640e59"
            ]
        ]
    },
    {
        "id": "a69dbcf04839b569",
        "type": "switch",
        "z": "477a1a1df2873848",
        "name": "light 0 or 1",
        "property": "payload.light",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 490,
        "y": 740,
        "wires": [
            [
                "4289e36e940eca58"
            ],
            [
                "f6989098c5f9fd0c"
            ]
        ]
    },
    {
        "id": "4289e36e940eca58",
        "type": "switch",
        "z": "477a1a1df2873848",
        "name": "123/test <= 63",
        "property": "payload.test",
        "propertyType": "msg",
        "rules": [
            {
                "t": "lte",
                "v": "63",
                "vt": "str"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 500,
        "y": 680,
        "wires": [
            [
                "07e7b1d747126bf1",
                "185392825f399686"
            ],
            [
                "e030c2f9381d6756",
                "52173a633ee8b27c"
            ]
        ]
    },
    {
        "id": "f6989098c5f9fd0c",
        "type": "switch",
        "z": "477a1a1df2873848",
        "name": "1raspbery 0 or 1",
        "property": "payload.raspberry",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 500,
        "y": 800,
        "wires": [
            [
                "07e7b1d747126bf1",
                "185392825f399686"
            ],
            [
                "e030c2f9381d6756",
                "52173a633ee8b27c"
            ]
        ]
    },
    {
        "id": "07e7b1d747126bf1",
        "type": "change",
        "z": "477a1a1df2873848",
        "name": "payload on",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "on",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 730,
        "y": 660,
        "wires": [
            [
                "960c09733082d103"
            ]
        ]
    },
    {
        "id": "e030c2f9381d6756",
        "type": "change",
        "z": "477a1a1df2873848",
        "name": "payload off",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "off",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 730,
        "y": 700,
        "wires": [
            [
                "960c09733082d103"
            ]
        ]
    },
    {
        "id": "185392825f399686",
        "type": "change",
        "z": "477a1a1df2873848",
        "name": "payload off",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "off",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 730,
        "y": 800,
        "wires": [
            [
                "d32e63247484353d"
            ]
        ]
    },
    {
        "id": "52173a633ee8b27c",
        "type": "change",
        "z": "477a1a1df2873848",
        "name": "payload on",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "on",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 730,
        "y": 760,
        "wires": [
            [
                "d32e63247484353d"
            ]
        ]
    },
    {
        "id": "cb1d46bbc7b63138",
        "type": "mqtt in",
        "z": "477a1a1df2873848",
        "name": "",
        "topic": "raspberry",
        "qos": "2",
        "datatype": "json",
        "broker": "f03eb60e5379e1a3",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 120,
        "y": 700,
        "wires": [
            [
                "3f939e8c0d640e59"
            ]
        ]
    },
    {
        "id": "c590cc035274bb9a",
        "type": "cronplus",
        "z": "477a1a1df2873848",
        "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": 140,
        "y": 860,
        "wires": [
            [
                "3f939e8c0d640e59"
            ]
        ]
    },
    {
        "id": "0bb675eee7cf2b76",
        "type": "calculator",
        "z": "477a1a1df2873848",
        "name": "",
        "inputMsgField": "payload",
        "outputMsgField": "payload",
        "operation": "mult",
        "constant": "100",
        "round": false,
        "decimals": 0,
        "x": 260,
        "y": 640,
        "wires": [
            [
                "3f939e8c0d640e59"
            ]
        ]
    },
    {
        "id": "a768af12e5ad67b2",
        "type": "debug",
        "z": "477a1a1df2873848",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 690,
        "y": 880,
        "wires": []
    },
    {
        "id": "d11f4fe99323110c",
        "type": "inject",
        "z": "477a1a1df2873848",
        "name": "80",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "123/test",
        "payload": "2023/06/08 13:37:33,1,405.133,7.848,2,26.6575,29.5926,3,3.046,27.0,0.80,101.67",
        "payloadType": "str",
        "x": 110,
        "y": 540,
        "wires": [
            [
                "6fc3edac259d0209"
            ]
        ]
    },
    {
        "id": "6f5298f2f9d97d6a",
        "type": "inject",
        "z": "477a1a1df2873848",
        "name": "50",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "123/test",
        "payload": "2023/06/08 13:37:33,1,405.133,7.848,2,26.6575,29.5926,3,3.046,27.0,0.50,101.67",
        "payloadType": "str",
        "x": 110,
        "y": 480,
        "wires": [
            [
                "6fc3edac259d0209"
            ]
        ]
    },
    {
        "id": "6fc3edac259d0209",
        "type": "csv",
        "z": "477a1a1df2873848",
        "name": "",
        "sep": ",",
        "hdrin": "",
        "hdrout": "none",
        "multi": "one",
        "ret": "\\n",
        "temp": "",
        "skip": "0",
        "strings": true,
        "include_empty_strings": "",
        "include_null_values": "",
        "x": 270,
        "y": 360,
        "wires": [
            [
                "0ee70e030db1ecfe",
                "0402be4a53f5a346"
            ]
        ]
    },
    {
        "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": ""
    }
]

you should indent things to make them readable. Here is your original code from one of your functions

let h = msg.payload.col1
let r = msg.payload.raspberry
let l = msg.payload.light

if (l==0){
if (h <= 63) msg.payload = "on";
if (h >63) msg.payload = "off";
}

if (l==1){
if (r == 0)
return [msg.payload = "on"]
if (r == 1)
return [msg.payload = "off"]
}

return msg;
return msg;

Here is it indented with the code for the if (l==1) fixed and the removal of the second return msg. Hopefully this will help you in teh future.

let h = msg.payload.col1
let r = msg.payload.raspberry
let l = msg.payload.light

if (l == 0) {
    if (h <= 63) msg.payload = "on";
    if (h > 63) msg.payload = "off";
}

if (l == 1) {
    if (r == 0)
        msg.payload = "on"
    if (r == 1)
        msg.payload = "off"
}

return msg;

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