Issue with easy fan control

Hi all,
I am trying to implement a very easy fan control into NodeRed. It should start a fan x secounds after the light of the room get turned on and stop x secounds after the light get switch off again. If the light gets switched off before, the fan starts blowing nothing should happen.

I tried to solve it with two trigger nodes, but unfortunately it does not work like expected. e.g.: After 90s the fan should turn off and 5min later (after turning the light off) go off again. If I turn on the light and stay in the room more than 90s+5min the fan get turned off. Sometimes the fan does not turn off again even the light is off...

Here my sample flow:
flows (5).json (4.9 KB)

Thank you in advance!

BR

This will get you closer...

[{"id":"eb7c7c249d2898a9","type":"inject","z":"53cced74b780fcb4","name":"payload.state true","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"state\":true}","payloadType":"json","x":210,"y":440,"wires":[["543b215811e151fa"]]},{"id":"543b215811e151fa","type":"switch","z":"53cced74b780fcb4","name":"on","property":"payload.state","propertyType":"msg","rules":[{"t":"eq","v":"true","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":410,"y":440,"wires":[["e7be4cf3f7bd52f8"]]},{"id":"e7be4cf3f7bd52f8","type":"trigger","z":"53cced74b780fcb4","name":"","op1":"","op2":"on","op1type":"nul","op2type":"str","duration":"90","extend":false,"overrideDelay":false,"units":"s","reset":"off","bytopic":"all","topic":"topic","outputs":1,"x":590,"y":440,"wires":[["bd2fd95a53a25016","ab9874d19f6059b0"]]},{"id":"bd2fd95a53a25016","type":"trigger","z":"53cced74b780fcb4","name":"","op1":"","op2":"off","op1type":"nul","op2type":"str","duration":"5","extend":false,"overrideDelay":false,"units":"min","reset":"on","bytopic":"all","topic":"topic","outputs":1,"x":790,"y":480,"wires":[["65c1bc330a7e6b11"]]},{"id":"ab9874d19f6059b0","type":"change","z":"53cced74b780fcb4","name":"start fan WC","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t   \"src\": \"shellyplus2pm-a8032ab7151c\",\t   \"method\": \"Switch.Set\",\t   \"params\": {  \"id\": 1,  \"on\": true  }  \t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":790,"y":440,"wires":[["323d94c2345b2127"]]},{"id":"65c1bc330a7e6b11","type":"change","z":"53cced74b780fcb4","name":"stop fan WC","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t   \"src\": \"shellyplus2pm-a8032ab7151c\",\t   \"method\": \"Switch.Set\",\t   \"params\": {  \"id\": 1,  \"on\": false  }  \t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":970,"y":480,"wires":[["7ce957c45751813a"]]},{"id":"323d94c2345b2127","type":"debug","z":"53cced74b780fcb4","name":"debug 12","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":960,"y":440,"wires":[]},{"id":"7ce957c45751813a","type":"debug","z":"53cced74b780fcb4","name":"debug 13","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1140,"y":480,"wires":[]},{"id":"567df74e222420d4","type":"switch","z":"53cced74b780fcb4","name":"off","property":"payload.state","propertyType":"msg","rules":[{"t":"eq","v":"false","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":410,"y":540,"wires":[["7dd2479e9c6c06e5"]]},{"id":"c38d88575e4fb04c","type":"inject","z":"53cced74b780fcb4","name":"payload.state false","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"state\":false}","payloadType":"json","x":210,"y":540,"wires":[["567df74e222420d4"]]},{"id":"7dd2479e9c6c06e5","type":"change","z":"53cced74b780fcb4","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":540,"wires":[["e7be4cf3f7bd52f8"]]}]

PS

Please dont attach small flows - add them as text as i have done here :point_up_2:

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

Thanks for the fast answer! I just tried to check your flow but I think there is an error: If the payload.state false is injeted the first trigger gets stopped which is ok, but the secound trigger for stopping the fan after 5 min. never gets activated...

BR
Franz

lets not forget I said...

:slight_smile:

Try this...

[{"id":"eb7c7c249d2898a9","type":"inject","z":"53cced74b780fcb4","name":"payload.state true","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"state\":true}","payloadType":"json","x":210,"y":440,"wires":[["543b215811e151fa"]]},{"id":"543b215811e151fa","type":"switch","z":"53cced74b780fcb4","name":"on","property":"payload.state","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":370,"y":440,"wires":[["e7be4cf3f7bd52f8"]]},{"id":"e7be4cf3f7bd52f8","type":"trigger","z":"53cced74b780fcb4","name":"","op1":"","op2":"on","op1type":"nul","op2type":"str","duration":"5","extend":false,"overrideDelay":false,"units":"s","reset":"off","bytopic":"all","topic":"topic","outputs":1,"x":600,"y":440,"wires":[["ab9874d19f6059b0"]]},{"id":"bd2fd95a53a25016","type":"trigger","z":"53cced74b780fcb4","name":"","op1":"","op2":"off","op1type":"nul","op2type":"str","duration":"20","extend":false,"overrideDelay":false,"units":"s","reset":"on","bytopic":"all","topic":"topic","outputs":1,"x":790,"y":500,"wires":[["65c1bc330a7e6b11"]]},{"id":"ab9874d19f6059b0","type":"change","z":"53cced74b780fcb4","name":"start fan WC","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t   \"src\": \"shellyplus2pm-a8032ab7151c\",\t   \"method\": \"Switch.Set\",\t   \"params\": {  \"id\": 1,  \"on\": true  }  \t}","tot":"jsonata"},{"t":"delete","p":"reset","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":440,"wires":[["323d94c2345b2127","bd2fd95a53a25016"]]},{"id":"65c1bc330a7e6b11","type":"change","z":"53cced74b780fcb4","name":"stop fan WC","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t   \"src\": \"shellyplus2pm-a8032ab7151c\",\t   \"method\": \"Switch.Set\",\t   \"params\": {  \"id\": 1,  \"on\": false  }  \t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":970,"y":500,"wires":[["7ce957c45751813a"]]},{"id":"323d94c2345b2127","type":"debug","z":"53cced74b780fcb4","name":"debug 12","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":960,"y":440,"wires":[]},{"id":"7ce957c45751813a","type":"debug","z":"53cced74b780fcb4","name":"debug 13","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1140,"y":500,"wires":[]},{"id":"567df74e222420d4","type":"switch","z":"53cced74b780fcb4","name":"off","property":"payload.state","propertyType":"msg","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":370,"y":500,"wires":[["7dd2479e9c6c06e5"]]},{"id":"c38d88575e4fb04c","type":"inject","z":"53cced74b780fcb4","name":"payload.state false","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"state\":false}","payloadType":"json","x":210,"y":500,"wires":[["567df74e222420d4"]]},{"id":"7dd2479e9c6c06e5","type":"change","z":"53cced74b780fcb4","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":500,"wires":[["e7be4cf3f7bd52f8"]]}]

The issue was msg.reset was reaching the 2nd trigger. I have now added delete msg.reset to the change node.

Thank you very much again - now it works almost perfectly! :slight_smile: Just one more case is missing:
1.) the first (5s) trigger activates the second one (20s),
2.) payload.state get set to false while second trigger is running
3.) If the payload.state is then set to true within 15s both triggers are running at the same time and therefore the outcome could be wrong (if the first trigger finishes before the secound one, it will be wrong). Somehow we would have to stop the second trigger if the first one is active.

Thank you in advance!

BR
Franz

I suspect you realise by now should write out all scenarios (otherwise we would just keep iterating)

I will leave this one as a task for you (but with hints)...

  • Set a flow context var whenever a command is sent to the device
    • e.g. if ON is sent, store "on" or true in flow.light_request
    • e.g. if OFF is sent, store "off" or false in flow.light_request
  • When a new on or off MQTT request comes in, use switch nodes to determine whether to send a msg.reset = true to the 1st or 2nd trigger.

Thanks for the hints - I got it :stuck_out_tongue:

[
    {
        "id": "53cced74b780fcb4",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "eb7c7c249d2898a9",
        "type": "inject",
        "z": "53cced74b780fcb4",
        "name": "payload.state true",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"state\":true}",
        "payloadType": "json",
        "x": 110,
        "y": 120,
        "wires": [
            [
                "543b215811e151fa"
            ]
        ]
    },
    {
        "id": "543b215811e151fa",
        "type": "switch",
        "z": "53cced74b780fcb4",
        "name": "on",
        "property": "payload.state",
        "propertyType": "msg",
        "rules": [
            {
                "t": "true"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 270,
        "y": 120,
        "wires": [
            [
                "e7be4cf3f7bd52f8",
                "cdcda5a07694a91b"
            ]
        ]
    },
    {
        "id": "e7be4cf3f7bd52f8",
        "type": "trigger",
        "z": "53cced74b780fcb4",
        "name": "",
        "op1": "",
        "op2": "on",
        "op1type": "nul",
        "op2type": "str",
        "duration": "5",
        "extend": false,
        "overrideDelay": false,
        "units": "s",
        "reset": "off",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 1,
        "x": 600,
        "y": 160,
        "wires": [
            [
                "ab9874d19f6059b0"
            ]
        ]
    },
    {
        "id": "bd2fd95a53a25016",
        "type": "trigger",
        "z": "53cced74b780fcb4",
        "name": "",
        "op1": "",
        "op2": "off",
        "op1type": "nul",
        "op2type": "str",
        "duration": "20",
        "extend": false,
        "overrideDelay": false,
        "units": "s",
        "reset": "on",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 1,
        "x": 790,
        "y": 100,
        "wires": [
            [
                "65c1bc330a7e6b11"
            ]
        ]
    },
    {
        "id": "ab9874d19f6059b0",
        "type": "change",
        "z": "53cced74b780fcb4",
        "name": "start fan WC",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{\t   \"src\": \"shellyplus2pm-a8032ab7151c\",\t   \"method\": \"Switch.Set\",\t   \"params\": {  \"id\": 1,  \"on\": true  }  \t}",
                "tot": "jsonata"
            },
            {
                "t": "delete",
                "p": "reset",
                "pt": "msg"
            },
            {
                "t": "set",
                "p": "fanstate",
                "pt": "flow",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 750,
        "y": 160,
        "wires": [
            [
                "bd2fd95a53a25016",
                "a494d7f9ea6b8776"
            ]
        ]
    },
    {
        "id": "65c1bc330a7e6b11",
        "type": "change",
        "z": "53cced74b780fcb4",
        "name": "stop fan WC",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{\t   \"src\": \"shellyplus2pm-a8032ab7151c\",\t   \"method\": \"Switch.Set\",\t   \"params\": {  \"id\": 1,  \"on\": false  }  \t}",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "fanstate",
                "pt": "flow",
                "to": "false",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 970,
        "y": 100,
        "wires": [
            [
                "a494d7f9ea6b8776"
            ]
        ]
    },
    {
        "id": "567df74e222420d4",
        "type": "switch",
        "z": "53cced74b780fcb4",
        "name": "off",
        "property": "payload.state",
        "propertyType": "msg",
        "rules": [
            {
                "t": "false"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 270,
        "y": 180,
        "wires": [
            [
                "7dd2479e9c6c06e5"
            ]
        ]
    },
    {
        "id": "c38d88575e4fb04c",
        "type": "inject",
        "z": "53cced74b780fcb4",
        "name": "payload.state false",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"state\":false}",
        "payloadType": "json",
        "x": 110,
        "y": 180,
        "wires": [
            [
                "567df74e222420d4"
            ]
        ]
    },
    {
        "id": "7dd2479e9c6c06e5",
        "type": "change",
        "z": "53cced74b780fcb4",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "reset",
                "pt": "msg",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 420,
        "y": 180,
        "wires": [
            [
                "e7be4cf3f7bd52f8"
            ]
        ]
    },
    {
        "id": "723ba6b6f451acbe",
        "type": "change",
        "z": "53cced74b780fcb4",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "reset",
                "pt": "msg",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 600,
        "y": 100,
        "wires": [
            [
                "bd2fd95a53a25016"
            ]
        ]
    },
    {
        "id": "cdcda5a07694a91b",
        "type": "switch",
        "z": "53cced74b780fcb4",
        "name": "Fan_state_true",
        "property": "fanstate",
        "propertyType": "flow",
        "rules": [
            {
                "t": "true"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 420,
        "y": 100,
        "wires": [
            [
                "723ba6b6f451acbe"
            ]
        ]
    },
    {
        "id": "a494d7f9ea6b8776",
        "type": "debug",
        "z": "53cced74b780fcb4",
        "name": "Debug",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1070,
        "y": 160,
        "wires": []
    }
]

THANKS!

2 Likes

I haven't followed this in detail, but often the field in the trigger node Reset the trigger if: msg.payload = can be useful. If one has a payload that is, for example 1 and 0 (or "on" and "off") the trigger node can be configured to start if the payload is 1, but if 0 comes in then it resets the trigger. This can eliminate the change node that sets msg.reset. As I said, I haven't been following this in detail, so it may not apply here.

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