Two inputs joined with logic in functions

I would like to connect two light switches (Sonoff with Tasmota MQTT). To manage a ventilator.

Switch 1 is placed in the wc, and switch 2 in the bathroom.

I'm made a join out of the two input mentioned above.

Output of wc:
06/07/2020, 22:53:26node: WCstat/wc/POWER : msg : Object

object

topic: "stat/wc/POWER"

payload: "OFF"

qos: 0

retain: false

_msgid: "98fec221.3bf3b"

Output of bathroom:
06/07/2020, 22:53:32node: Badkamerstat/badkamer/POWER : msg : Object

object

topic: "stat/badkamer/POWER"

payload: "ON"

qos: 0

retain: false

_msgid: "6e0a6eaa.3f7d6"

Hopefully some one can help to create a function to turn the ventilator on (cmnd/ventilator/power with payload: on) when the wc or the bathroom has the payload: "ON".

And create a delay off 10 minutus when the wc and the bathroom are both switch off and then turning the ventilator off (cmnd/ventilator/power with payload: off).

Thank you for your advice!
Robert

an example function

[{"id":"93b983eb.6c5d","type":"inject","z":"ea03ca5a.9fe438","name":"sw1 on","topic":"stat/badkamer/POWER","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":380,"wires":[["d8aa4095.d7c9b"]]},{"id":"e1d8cbab.ad201","type":"inject","z":"ea03ca5a.9fe438","name":"sw1 off","topic":"stat/badkamer/POWER","payload":"off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":420,"wires":[["d8aa4095.d7c9b"]]},{"id":"4a72d695.e99e88","type":"inject","z":"ea03ca5a.9fe438","name":"sw1 on","topic":"stat/wc/POWER","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":480,"wires":[["d8aa4095.d7c9b"]]},{"id":"a2b51166.6e7af8","type":"inject","z":"ea03ca5a.9fe438","name":"sw2 off","topic":"stat/wc/POWER","payload":"off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":520,"wires":[["d8aa4095.d7c9b"]]},{"id":"d8aa4095.d7c9b","type":"function","z":"ea03ca5a.9fe438","name":"","func":"if (msg.payload === 'on') {\n    node.send({\"reset\": 1});\n    msg.payload = 'on';\n    msg.topic = 'cmnd/ventilator/power';\n    msg.delay = 600000;\n    return msg;\n}else{\n    return [null,msg]\n}","outputs":2,"noerr":0,"x":300,"y":460,"wires":[["c418a275.963478","a63ea0cf.5a0b68"],[]]},{"id":"a63ea0cf.5a0b68","type":"delay","z":"ea03ca5a.9fe438","name":"","pauseType":"delayv","timeout":"6","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":340,"y":520,"wires":[["648e8f27.acc6f8"]]},{"id":"648e8f27.acc6f8","type":"function","z":"ea03ca5a.9fe438","name":"","func":"msg.payload = 'off';\nmsg.topic = 'cmnd/ventilator/power';\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":520,"wires":[["c418a275.963478"]]},{"id":"c418a275.963478","type":"debug","z":"ea03ca5a.9fe438","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":610,"y":460,"wires":[]}]

Only the on's will trigger the fan and delay then turn off. Further on's will reset the timer for off back to 10 mins

Hi E1cid,

Thank you for your quick response!
I copied your code and pasted it to a new tab, and tried to run it. But I get no debug out put.

First question you made two outputs on the first function. The second ouput seems not to bent in use?

[
    {
        "id": "1c6c000e.340b6",
        "type": "tab",
        "label": "Ventilator",
        "disabled": false,
        "info": ""
    },
    {
        "id": "6dc0f491.60d5e4",
        "type": "comment",
        "z": "1c6c000e.340b6",
        "name": "Ventilator",
        "info": "",
        "x": 80,
        "y": 40,
        "wires": []
    },
    {
        "id": "b56f7900.38f1c",
        "type": "ui_button",
        "z": "1c6c000e.340b6",
        "name": "",
        "group": "427c503f.0f30b",
        "order": 3,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "Auto",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "icon": "",
        "payload": "auto",
        "payloadType": "str",
        "topic": "cmnd/ventilator/power",
        "x": 70,
        "y": 100,
        "wires": [
            [
                "dfa1d2ea.f553"
            ]
        ]
    },
    {
        "id": "9a2ab0d5.56b5e8",
        "type": "ui_button",
        "z": "1c6c000e.340b6",
        "name": "",
        "group": "427c503f.0f30b",
        "order": 4,
        "width": 0,
        "height": 0,
        "passthru": true,
        "label": "Handmatig On",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "icon": "",
        "payload": "on",
        "payloadType": "str",
        "topic": "cmnd/ventilator/power",
        "x": 100,
        "y": 160,
        "wires": [
            [
                "dfa1d2ea.f553"
            ]
        ]
    },
    {
        "id": "368ec72b.3b5428",
        "type": "ui_button",
        "z": "1c6c000e.340b6",
        "name": "",
        "group": "427c503f.0f30b",
        "order": 5,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "Handmatig Off",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "icon": "",
        "payload": "off",
        "payloadType": "str",
        "topic": "cmnd/ventilator/power",
        "x": 100,
        "y": 220,
        "wires": [
            [
                "dfa1d2ea.f553"
            ]
        ]
    },
    {
        "id": "dfa1d2ea.f553",
        "type": "mqtt out",
        "z": "1c6c000e.340b6",
        "name": "Ventilator",
        "topic": "cmnd/ventilator/power",
        "qos": "0",
        "retain": "false",
        "broker": "3574ebe9.ce112c",
        "x": 440,
        "y": 120,
        "wires": []
    },
    {
        "id": "d54fe175.d1353",
        "type": "comment",
        "z": "1c6c000e.340b6",
        "name": "Koppeling WC Ventilator",
        "info": "",
        "x": 110,
        "y": 280,
        "wires": []
    },
    {
        "id": "c806b49f.9baa9",
        "type": "mqtt in",
        "z": "1c6c000e.340b6",
        "name": "stat/wc/POWER",
        "topic": "stat/wc/POWER",
        "qos": "2",
        "datatype": "auto",
        "broker": "3574ebe9.ce112c",
        "x": 100,
        "y": 340,
        "wires": [
            [
                "6da6f04a.3c3b3",
                "82f4ab13.995598"
            ]
        ]
    },
    {
        "id": "ed2e7fa0.c4bca8",
        "type": "debug",
        "z": "1c6c000e.340b6",
        "name": "Status Ventilator_WC",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 580,
        "y": 340,
        "wires": []
    },
    {
        "id": "1f04f586.e3f4ea",
        "type": "comment",
        "z": "1c6c000e.340b6",
        "name": "Terugkoppeling Ventilator",
        "info": "",
        "x": 140,
        "y": 800,
        "wires": []
    },
    {
        "id": "e352ab86.3e7f8",
        "type": "mqtt in",
        "z": "1c6c000e.340b6",
        "name": "stat/ventilator/POWER",
        "topic": "stat/ventilator/POWER",
        "qos": "2",
        "datatype": "auto",
        "broker": "3574ebe9.ce112c",
        "x": 130,
        "y": 840,
        "wires": [
            [
                "9e6bcf5.f0e44b"
            ]
        ]
    },
    {
        "id": "9e6bcf5.f0e44b",
        "type": "ui_text_input",
        "z": "1c6c000e.340b6",
        "name": "",
        "label": "Status Ventilator",
        "tooltip": "",
        "group": "427c503f.0f30b",
        "order": 1,
        "width": 0,
        "height": 0,
        "passthru": true,
        "mode": "text",
        "delay": 300,
        "topic": "",
        "x": 350,
        "y": 840,
        "wires": [
            []
        ]
    },
    {
        "id": "ecb1051d.4c62e8",
        "type": "debug",
        "z": "1c6c000e.340b6",
        "name": "Terugkoppeling",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 570,
        "y": 840,
        "wires": []
    },
    {
        "id": "6da6f04a.3c3b3",
        "type": "change",
        "z": "1c6c000e.340b6",
        "name": "",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "ON",
                "fromt": "str",
                "to": "on",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 320,
        "y": 340,
        "wires": [
            [
                "ed2e7fa0.c4bca8"
            ]
        ]
    },
    {
        "id": "4a13b8a.609a148",
        "type": "mqtt in",
        "z": "1c6c000e.340b6",
        "name": "stat/badkamer/POWER",
        "topic": "stat/badkamer/POWER",
        "qos": "2",
        "datatype": "auto",
        "broker": "3574ebe9.ce112c",
        "x": 120,
        "y": 540,
        "wires": [
            [
                "d42f377d.76de4",
                "82f4ab13.995598"
            ]
        ]
    },
    {
        "id": "d42f377d.76de4",
        "type": "debug",
        "z": "1c6c000e.340b6",
        "name": "Status Ventilator_badkamer",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 620,
        "y": 540,
        "wires": []
    },
    {
        "id": "e3ceff1c.3c6de",
        "type": "change",
        "z": "1c6c000e.340b6",
        "name": "",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "ON",
                "fromt": "str",
                "to": "on",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 360,
        "y": 540,
        "wires": [
            []
        ]
    },
    {
        "id": "82f4ab13.995598",
        "type": "join",
        "z": "1c6c000e.340b6",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "topic",
        "propertyType": "msg",
        "key": "payload",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "num",
        "reduceFixup": "",
        "x": 410,
        "y": 420,
        "wires": [
            [
                "a362c356.3880b",
                "1ddf311d.f81227"
            ]
        ]
    },
    {
        "id": "a362c356.3880b",
        "type": "debug",
        "z": "1c6c000e.340b6",
        "name": "Status Join",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload.badkamer",
        "targetType": "msg",
        "x": 570,
        "y": 480,
        "wires": []
    },
    {
        "id": "1ddf311d.f81227",
        "type": "function",
        "z": "1c6c000e.340b6",
        "name": "",
        "func": "\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 570,
        "y": 420,
        "wires": [
            [
                "dfa1d2ea.f553"
            ]
        ]
    },
    {
        "id": "427c503f.0f30b",
        "type": "ui_group",
        "z": "",
        "name": "Ventilator",
        "tab": "f75de09c.325ea8",
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "3574ebe9.ce112c",
        "type": "mqtt-broker",
        "z": "",
        "name": "rpimqtt",
        "broker": "192.168.178.30",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthRetain": "false",
        "birthPayload": "",
        "closeTopic": "",
        "closeRetain": "false",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willRetain": "false",
        "willPayload": ""
    },
    {
        "id": "f75de09c.325ea8",
        "type": "ui_tab",
        "z": "",
        "name": "Ventilator",
        "icon": "dashboard",
        "order": 1,
        "disabled": false,
        "hidden": false
    }
]

This is the code I made so far.

I will look at your code and hope to get it running. Thanks again for your input!

Kind regards,
Robert

please use ``` before and after your code when you post, one it makes it more readable, two it makes it easy to copy as there will be a copy link added.
eg
```
msg ="test";
```

will become

msg ="test";

So can you edit your post please.

Does the flow look like below?

The second output is to stop the msg.payload when it equals "off". All "on" payloads go to first output.

Sorry, I was unable to edit the previous code.

[
    {
        "id": "1c6c000e.340b6",
        "type": "tab",
        "label": "Ventilator",
        "disabled": false,
        "info": ""
    },
    {
        "id": "6dc0f491.60d5e4",
        "type": "comment",
        "z": "1c6c000e.340b6",
        "name": "Ventilator",
        "info": "",
        "x": 80,
        "y": 40,
        "wires": []
    },
    {
        "id": "b56f7900.38f1c",
        "type": "ui_button",
        "z": "1c6c000e.340b6",
        "name": "",
        "group": "427c503f.0f30b",
        "order": 3,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "Auto",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "icon": "",
        "payload": "auto",
        "payloadType": "str",
        "topic": "cmnd/ventilator/power",
        "x": 70,
        "y": 100,
        "wires": [
            [
                "dfa1d2ea.f553"
            ]
        ]
    },
    {
        "id": "9a2ab0d5.56b5e8",
        "type": "ui_button",
        "z": "1c6c000e.340b6",
        "name": "",
        "group": "427c503f.0f30b",
        "order": 4,
        "width": 0,
        "height": 0,
        "passthru": true,
        "label": "Handmatig On",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "icon": "",
        "payload": "on",
        "payloadType": "str",
        "topic": "cmnd/ventilator/power",
        "x": 100,
        "y": 160,
        "wires": [
            [
                "dfa1d2ea.f553"
            ]
        ]
    },
    {
        "id": "368ec72b.3b5428",
        "type": "ui_button",
        "z": "1c6c000e.340b6",
        "name": "",
        "group": "427c503f.0f30b",
        "order": 5,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "Handmatig Off",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "icon": "",
        "payload": "off",
        "payloadType": "str",
        "topic": "cmnd/ventilator/power",
        "x": 100,
        "y": 220,
        "wires": [
            [
                "dfa1d2ea.f553"
            ]
        ]
    },
    {
        "id": "dfa1d2ea.f553",
        "type": "mqtt out",
        "z": "1c6c000e.340b6",
        "name": "Ventilator",
        "topic": "cmnd/ventilator/power",
        "qos": "0",
        "retain": "false",
        "broker": "3574ebe9.ce112c",
        "x": 440,
        "y": 120,
        "wires": []
    },
    {
        "id": "d54fe175.d1353",
        "type": "comment",
        "z": "1c6c000e.340b6",
        "name": "Koppeling WC Ventilator",
        "info": "",
        "x": 110,
        "y": 280,
        "wires": []
    },
    {
        "id": "c806b49f.9baa9",
        "type": "mqtt in",
        "z": "1c6c000e.340b6",
        "name": "stat/wc/POWER",
        "topic": "stat/wc/POWER",
        "qos": "2",
        "datatype": "auto",
        "broker": "3574ebe9.ce112c",
        "x": 100,
        "y": 340,
        "wires": [
            [
                "6da6f04a.3c3b3",
                "82f4ab13.995598"
            ]
        ]
    },
    {
        "id": "ed2e7fa0.c4bca8",
        "type": "debug",
        "z": "1c6c000e.340b6",
        "name": "Status Ventilator_WC",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 580,
        "y": 340,
        "wires": []
    },
    {
        "id": "1f04f586.e3f4ea",
        "type": "comment",
        "z": "1c6c000e.340b6",
        "name": "Terugkoppeling Ventilator",
        "info": "",
        "x": 140,
        "y": 800,
        "wires": []
    },
    {
        "id": "e352ab86.3e7f8",
        "type": "mqtt in",
        "z": "1c6c000e.340b6",
        "name": "stat/ventilator/POWER",
        "topic": "stat/ventilator/POWER",
        "qos": "2",
        "datatype": "auto",
        "broker": "3574ebe9.ce112c",
        "x": 130,
        "y": 840,
        "wires": [
            [
                "9e6bcf5.f0e44b"
            ]
        ]
    },
    {
        "id": "9e6bcf5.f0e44b",
        "type": "ui_text_input",
        "z": "1c6c000e.340b6",
        "name": "",
        "label": "Status Ventilator",
        "tooltip": "",
        "group": "427c503f.0f30b",
        "order": 1,
        "width": 0,
        "height": 0,
        "passthru": true,
        "mode": "text",
        "delay": 300,
        "topic": "",
        "x": 350,
        "y": 840,
        "wires": [
            []
        ]
    },
    {
        "id": "ecb1051d.4c62e8",
        "type": "debug",
        "z": "1c6c000e.340b6",
        "name": "Terugkoppeling",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 570,
        "y": 840,
        "wires": []
    },
    {
        "id": "6da6f04a.3c3b3",
        "type": "change",
        "z": "1c6c000e.340b6",
        "name": "",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "ON",
                "fromt": "str",
                "to": "on",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 320,
        "y": 340,
        "wires": [
            [
                "ed2e7fa0.c4bca8"
            ]
        ]
    },
    {
        "id": "4a13b8a.609a148",
        "type": "mqtt in",
        "z": "1c6c000e.340b6",
        "name": "stat/badkamer/POWER",
        "topic": "stat/badkamer/POWER",
        "qos": "2",
        "datatype": "auto",
        "broker": "3574ebe9.ce112c",
        "x": 120,
        "y": 540,
        "wires": [
            [
                "d42f377d.76de4",
                "82f4ab13.995598"
            ]
        ]
    },
    {
        "id": "d42f377d.76de4",
        "type": "debug",
        "z": "1c6c000e.340b6",
        "name": "Status Ventilator_badkamer",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 620,
        "y": 540,
        "wires": []
    },
    {
        "id": "e3ceff1c.3c6de",
        "type": "change",
        "z": "1c6c000e.340b6",
        "name": "",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "ON",
                "fromt": "str",
                "to": "on",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 360,
        "y": 540,
        "wires": [
            []
        ]
    },
    {
        "id": "82f4ab13.995598",
        "type": "join",
        "z": "1c6c000e.340b6",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "topic",
        "propertyType": "msg",
        "key": "payload",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "num",
        "reduceFixup": "",
        "x": 410,
        "y": 420,
        "wires": [
            [
                "a362c356.3880b",
                "1ddf311d.f81227"
            ]
        ]
    },
    {
        "id": "a362c356.3880b",
        "type": "debug",
        "z": "1c6c000e.340b6",
        "name": "Status Join",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload.badkamer",
        "targetType": "msg",
        "x": 570,
        "y": 480,
        "wires": []
    },
    {
        "id": "1ddf311d.f81227",
        "type": "function",
        "z": "1c6c000e.340b6",
        "name": "",
        "func": "\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 570,
        "y": 420,
        "wires": [
            [
                "dfa1d2ea.f553"
            ]
        ]
    },
    {
        "id": "427c503f.0f30b",
        "type": "ui_group",
        "z": "",
        "name": "Ventilator",
        "tab": "f75de09c.325ea8",
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "3574ebe9.ce112c",
        "type": "mqtt-broker",
        "z": "",
        "name": "rpimqtt",
        "broker": "192.168.178.30",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthRetain": "false",
        "birthPayload": "",
        "closeTopic": "",
        "closeRetain": "false",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willRetain": "false",
        "willPayload": ""
    },
    {
        "id": "f75de09c.325ea8",
        "type": "ui_tab",
        "z": "",
        "name": "Ventilator",
        "icon": "dashboard",
        "order": 1,
        "disabled": false,
        "hidden": false
    }
]

Please use backticks, not single quotes: ' vs `

The backtick is above tab key next to the 1 key. or copy and paste this ```

Again i ask you to edit your previous post and this one.

Cheers that makes it easier to help you now. Next time you can also export your flow in compact mode.

I will take a look at your flow now. and get back to you

should fit in like this, i can not test as i do not use MQTT

[{"id":"96b7f6dc.db7728","type":"tab","label":"Ventilator","disabled":false,"info":""},{"id":"131a47da.958cf","type":"comment","z":"96b7f6dc.db7728","name":"Ventilator","info":"","x":80,"y":40,"wires":[]},{"id":"bfc29c3f.c3ed18","type":"ui_button","z":"96b7f6dc.db7728","name":"","group":"63989ffc.3e90b","order":3,"width":0,"height":0,"passthru":false,"label":"Auto","tooltip":"","color":"","bgcolor":"","icon":"","payload":"auto","payloadType":"str","topic":"cmnd/ventilator/power","x":70,"y":100,"wires":[["adc76393.c0f8f8"]]},{"id":"3f4412eb.bc2c16","type":"ui_button","z":"96b7f6dc.db7728","name":"","group":"63989ffc.3e90b","order":4,"width":0,"height":0,"passthru":true,"label":"Handmatig On","tooltip":"","color":"","bgcolor":"","icon":"","payload":"on","payloadType":"str","topic":"cmnd/ventilator/power","x":100,"y":160,"wires":[["adc76393.c0f8f8"]]},{"id":"603bec55.f06fb4","type":"ui_button","z":"96b7f6dc.db7728","name":"","group":"63989ffc.3e90b","order":5,"width":0,"height":0,"passthru":false,"label":"Handmatig Off","tooltip":"","color":"","bgcolor":"","icon":"","payload":"off","payloadType":"str","topic":"cmnd/ventilator/power","x":100,"y":220,"wires":[["adc76393.c0f8f8"]]},{"id":"adc76393.c0f8f8","type":"mqtt out","z":"96b7f6dc.db7728","name":"Ventilator","topic":"cmnd/ventilator/power","qos":"0","retain":"false","broker":"3902251f.80e562","x":440,"y":120,"wires":[]},{"id":"937d8ac7.28bd2","type":"comment","z":"96b7f6dc.db7728","name":"Koppeling WC Ventilator","info":"","x":110,"y":280,"wires":[]},{"id":"fd7c6304.cd532","type":"mqtt in","z":"96b7f6dc.db7728","name":"stat/wc/POWER","topic":"stat/wc/POWER","qos":"2","datatype":"auto","broker":"3902251f.80e562","x":100,"y":360,"wires":[["537b2541.c79634"]]},{"id":"4156442.925a4bc","type":"debug","z":"96b7f6dc.db7728","name":"Status Ventilator_WC","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":580,"y":340,"wires":[]},{"id":"736d6b91.7e789c","type":"comment","z":"96b7f6dc.db7728","name":"Terugkoppeling Ventilator","info":"","x":140,"y":800,"wires":[]},{"id":"8da2d4e3.8aad48","type":"mqtt in","z":"96b7f6dc.db7728","name":"stat/ventilator/POWER","topic":"stat/ventilator/POWER","qos":"2","datatype":"auto","broker":"3902251f.80e562","x":130,"y":840,"wires":[["61c34558.7324b4"]]},{"id":"61c34558.7324b4","type":"ui_text_input","z":"96b7f6dc.db7728","name":"","label":"Status Ventilator","tooltip":"","group":"63989ffc.3e90b","order":1,"width":0,"height":0,"passthru":true,"mode":"text","delay":300,"topic":"","x":350,"y":840,"wires":[[]]},{"id":"6086da31.9b4f14","type":"debug","z":"96b7f6dc.db7728","name":"Terugkoppeling","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":570,"y":840,"wires":[]},{"id":"537b2541.c79634","type":"change","z":"96b7f6dc.db7728","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"ON","fromt":"str","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":340,"wires":[["4156442.925a4bc","b886b39b.1f2c28"]]},{"id":"dbe01ab1.28053","type":"mqtt in","z":"96b7f6dc.db7728","name":"stat/badkamer/POWER","topic":"stat/badkamer/POWER","qos":"2","datatype":"auto","broker":"3902251f.80e562","x":120,"y":520,"wires":[["e6a66703.450af"]]},{"id":"e6a66703.450af","type":"debug","z":"96b7f6dc.db7728","name":"Status Ventilator_badkamer","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":620,"y":540,"wires":[]},{"id":"1b1f0ac.f41dc75","type":"change","z":"96b7f6dc.db7728","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"ON","fromt":"str","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":540,"wires":[["b886b39b.1f2c28"]]},{"id":"28d20e0.6abd372","type":"debug","z":"96b7f6dc.db7728","name":"auto payload","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":670,"y":420,"wires":[]},{"id":"8db2907f.aade6","type":"delay","z":"96b7f6dc.db7728","name":"","pauseType":"delayv","timeout":"6","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":480,"y":480,"wires":[["86371c57.60314"]]},{"id":"86371c57.60314","type":"function","z":"96b7f6dc.db7728","name":"","func":"msg.payload = 'off';\nmsg.topic = 'cmnd/ventilator/power';\nreturn msg;","outputs":1,"noerr":0,"x":630,"y":480,"wires":[["28d20e0.6abd372","adc76393.c0f8f8"]]},{"id":"b886b39b.1f2c28","type":"function","z":"96b7f6dc.db7728","name":"","func":"if (msg.payload === 'on') {\n    node.send({\"reset\": 1});\n    msg.payload = 'on';\n    msg.topic = 'cmnd/ventilator/power';\n    msg.delay = 6000;\n    return msg;\n}else{\n    return [null,msg]\n}","outputs":2,"noerr":0,"x":365.33331298828125,"y":415.33331298828125,"wires":[["adc76393.c0f8f8","28d20e0.6abd372","8db2907f.aade6"],[]]},{"id":"63989ffc.3e90b","type":"ui_group","z":"","name":"Ventilator","tab":"ca1eeb2d.b75f88","order":1,"disp":true,"width":"6","collapse":false},{"id":"3902251f.80e562","type":"mqtt-broker","z":"","name":"rpimqtt","broker":"192.168.178.30","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""},{"id":"ca1eeb2d.b75f88","type":"ui_tab","z":"","name":"Ventilator","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Thank you for your help!
It is my first time exporting code. next time I will export in compact mode!

1 Like

Thank you for your code.
I got it "working". But the ventilator is turning of after 6 sec.

in the function edit the line

msg.delay = 6000;
to
msg.delay = 600000;

that wil give 10 minutes

I would like to keep the ventilator running when wc is turn-on. When it is turn-off give it a delay off 10 min.
The same for the bathroom switch.

But I would like to create a function to checks the status of the wc and the bathroom.
If one or the other is still status on keep on running the ventilator.
When the status changes to both off let the ventilator run for 10 min extra.

Hopefully you can help out?

if (msg.topic === 'stat/wc/POWER' && msg.payload === 'ON' || msg.topic === 'stat/badkamer/POWER' && msg.payload === 'ON') {
    //node.send({"reset": 1});
    msg.payload = 'on';
    msg.topic = 'cmnd/ventilator/power';
    //msg.delay = 6000;
    return msg;
} else if (msg.topic === 'stat/wc/POWER' && msg.payload === 'OFF' && msg.topic === 'stat/badkamer/POWER' && msg.payload === 'OFF') {
    //node.send({"reset": 1});
    msg.payload = 'off';
    msg.topic = 'cmnd/ventilator/power';
    msg.delay = 6000;
    return msg;
}else{
    return [null,msg]
}

Can you help me correct the following code?

Thank you for your help!

[{"id":"93b983eb.6c5d","type":"inject","z":"ea03ca5a.9fe438","name":"sw1 on","topic":"stat/badkamer/POWER","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":380,"wires":[["d8aa4095.d7c9b"]]},{"id":"e1d8cbab.ad201","type":"inject","z":"ea03ca5a.9fe438","name":"sw1 off","topic":"stat/badkamer/POWER","payload":"off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":420,"wires":[["d8aa4095.d7c9b"]]},{"id":"4a72d695.e99e88","type":"inject","z":"ea03ca5a.9fe438","name":"sw1 on","topic":"stat/wc/POWER","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":480,"wires":[["d8aa4095.d7c9b"]]},{"id":"a2b51166.6e7af8","type":"inject","z":"ea03ca5a.9fe438","name":"sw2 off","topic":"stat/wc/POWER","payload":"off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":520,"wires":[["d8aa4095.d7c9b"]]},{"id":"d8aa4095.d7c9b","type":"function","z":"ea03ca5a.9fe438","name":"","func":"//Get stored state\nfanOn = flow.get('fan') || {};\n\n//check payload if 'off'\nif (msg.payload === 'off') {\n    // check if store has an 'on'\n    if (Object.values(fanOn).includes('on') ) {\n        // if  store is on set to off\n        fanOn[msg.topic] = 'off';\n        flow.set('fan', fanOn);\n    }\n    // check if store has no on's\n    if (!Object.values(fanOn).includes('on') ) {\n        // if so start timer\n        node.send({\"reset\": 1});\n        msg.payload = 'on';\n        msg.topic = 'cmnd/ventilator/power';\n        msg.delay = 6000;\n        return msg;\n    // return reset as store has 'on' value\n    }else{\n        return {'reset': 1};\n    }\n// if payload is on store 'on' value\n}else{\n    fanOn[msg.topic] = 'on';\n    flow.set('fan', fanOn);\n    node.send({'reset': 1});\n    msg.topic = 'cmnd/ventilator/power';\n    return [null,msg]\n}\n","outputs":2,"noerr":0,"x":300,"y":460,"wires":[["a63ea0cf.5a0b68"],["c418a275.963478"]]},{"id":"a63ea0cf.5a0b68","type":"delay","z":"ea03ca5a.9fe438","name":"","pauseType":"delayv","timeout":"6","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":340,"y":520,"wires":[["648e8f27.acc6f8"]]},{"id":"648e8f27.acc6f8","type":"function","z":"ea03ca5a.9fe438","name":"","func":"msg.payload = 'off';\nmsg.topic = 'cmnd/ventilator/power';\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":520,"wires":[["c418a275.963478"]]},{"id":"c418a275.963478","type":"debug","z":"ea03ca5a.9fe438","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":670,"y":360,"wires":[]}]

Added some comments

Thank you for your code input!
But it didn't switch correctly off.
When wc and bathroom are both on, and after this state one of them is switch off the ventilator should keep on running. After both are switch off the timer of 10 min thould step in and switch the ventilator off after the 10 min.

if ((msg.topic === 'stat/wc/POWER' && msg.payload === 'ON') || (msg.topic === 'stat/badkamer/POWER' && msg.payload === 'ON')) {
    //node.send({"reset": 1});
    msg.payload = 'on';
    msg.topic = 'cmnd/ventilator/power';
    return [ null, msg ];
} else if ((msg.topic === 'stat/wc/POWER' && msg.payload === 'OFF') && (msg.topic === 'stat/badkamer/POWER' && msg.payload === 'OFF')) {
    //node.send({"reset": 1});
    msg.payload = 'off';
    msg.topic = 'cmnd/ventilator/power';
    return [ msg, null ]
} else{
    return msg
}

This code is working until one of the WC or Bathroom are switch off. my code switch off the ventilator when one or the other is being switch off.

This else if part is not working correctly
Any idee?

[{"id":"df663f39.8d272","type":"function","z":"3f993164.760abe","name":"","func":"if ((msg.topic === 'stat/wc/POWER' && msg.payload === 'ON') || (msg.topic === 'stat/badkamer/POWER' && msg.payload === 'ON')) {\n    //node.send({\"reset\": 1});\n    msg.payload = 'on';\n    msg.topic = 'cmnd/ventilator/power';\n    return [ null, msg ];\n} else if ((msg.topic === 'stat/wc/POWER' && msg.payload === 'OFF') && (msg.topic === 'stat/badkamer/POWER' && msg.payload === 'OFF')) {\n    //node.send({\"reset\": 1});\n    msg.payload = 'off';\n    msg.topic = 'cmnd/ventilator/power';\n    return [ msg, null ]\n} else{\n    return msg\n}\n","outputs":2,"noerr":0,"initialize":"","finalize":"","x":320,"y":360,"wires":[["269f51fd.f95296","923958c6.4af1b8"],["aedde9bf.4d01f8","3b6e4d94.8838d2"]]}]

updated code 3 mins ago, i have changed the out puts so look at them in my flow. make sure you connected the links correctly

If you play with the inject buttons and look at the debug to see how it works. I have set time to 6 seconds so you will need to increase that when you move it to your flow.

your code is un workable as the msg.payload value needs to be stored to compare it to the other switches state.

I have used the context store to store the state of the 2 switches

https://www.google.com/url?q=https://nodered.org/docs/user-guide/context&sa=U&ved=2ahUKEwi16sak8sDqAhWlThUIHWBSA7sQtwIwAHoECAkQAQ&usg=AOvVaw3dm-xC3B-X6_WYwjMtXZPJ

Thank you E1cid for your great patience and code! It is working now!

I have two things that I would like to understand:

  1. In your first function:
    fanOn = flow.get('fan') || {};
    fanOn is your now variable.
    'fan': I don't get yet. Can you explain me what this is?
    || is an or function
    {} is an empty function

  2. Between your two functions you put a variable "delay each message" with 6 sec. What is the purpose of this delay?

Again thanks for your great help!

Kind regards,
Robert

'fanOn' is the variable in the function
'fan' is the variable in the context store, where fanOn contents is stored.

|| is an or statement
so if context store flow.get('fan') doesn't exsist, then 'fanOn is set to an empty Object. {}

variable is not a variable it is the setting of the delay node, as i am lazy i did not bother to name the node.
The 6 seconds is ignored as the node is set to receive a variable delay in msg.delay.

This line

fanOn = flow.get('fan') || {};

should be

var fanOn = flow.get('fan') || {};