Delaying a process question

This should work just fine ?

When the pir is activated you get 1
Switch node passes 1 to trigger node
Trigger node outputs 1 (to switch on light)
After 10 seconds Trigger node outputs 0 (to switch off light)

Each time the PIR passes 1 the 10 second time is restarted (so light stays on while movement detected)

@smcgann99 did you forget to post the flow?

I think this should do it. It switches the LED on when motion starts. When end of motion is detected it waits 10 seconds then switches it off, unless more motion occurs during that 10 seconds, in which case it stays on.

image

[{"id":"e30c88c4f70e862e","type":"inject","z":"1f1050e0d58da653","name":"PIR 1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":110,"y":860,"wires":[["89800ef865672717","f2a8644e29e611e5"]]},{"id":"9e4e1844d0a9f9fd","type":"inject","z":"1f1050e0d58da653","name":"PIR 0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":110,"y":920,"wires":[["89800ef865672717","f2a8644e29e611e5"]]},{"id":"f2a8644e29e611e5","type":"trigger","z":"1f1050e0d58da653","name":"","op1":"","op2":"0","op1type":"nul","op2type":"num","duration":"10","extend":true,"overrideDelay":false,"units":"s","reset":"1","bytopic":"all","topic":"topic","outputs":1,"x":310,"y":900,"wires":[["27b01429a9b16918"]]},{"id":"27b01429a9b16918","type":"debug","z":"1f1050e0d58da653","name":"LED","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":840,"wires":[]},{"id":"89800ef865672717","type":"switch","z":"1f1050e0d58da653","name":"1?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":290,"y":840,"wires":[["27b01429a9b16918"]]}]

@subwayfootlong Can you post a link so we can see what sort of motion sensor you have?

No I replied to @xx_Nexus_xx flow from 2 days ago which will do what @subwayfootlong needs :wink:
Its simply triggering a trigger node from PIR after filtering out the zero states.

In fact as the PIR has no built in timer, then zero state will come soon after 1 state, so even if we let these go to trigger it will only extend the light time by a second or so.

If no messages arrive for 10 seconds trigger sends a zero and light goes off.

Actually @Frida has posted the solution.
I was wondering if @Colin had a better way to do it as im quite inexperienced in Node-RED

I have posted my suggestion, post #23, with just a Trigger and a Switch.

Lol I think you will find with @Frida flow that you end up with disco lights going on and off :wink:

Does this describe what you want to happen ?

The problem with that is what happens if the PIR sends 1 and then 0 and back to 1 all within the 10 seconds. The light will go off after the 10 secnds even though the PIR is still at 1. See my solution above to fix that.

I get what you are saying, and it covers edge cases so, a thorough solution!.

But 10 seconds is not realistic unless people are running around the room waving their hands in the air :wink:

My thoughts were to keep it really simple, so not even worry about PIR state and just keep resetting trigger on each message.

I use this all around my house for room lights, and in reality PIR WILL reset within a few seconds, even while people are still in the room. Given a reasonable delay of a few mins, it should be triggered again before time runs out. Although not if I have dozed off watching TV :wink:

Another way to do it.

[
    {
        "id": "605950b3904ff015",
        "type": "delay",
        "z": "d362bb7ca55abaf1",
        "name": "",
        "pauseType": "delay",
        "timeout": "5",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 500,
        "y": 220,
        "wires": [
            [
                "43fbe55cf81bceeb"
            ]
        ]
    },
    {
        "id": "bdd566267a971fa4",
        "type": "inject",
        "z": "d362bb7ca55abaf1",
        "name": "1",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "1",
        "payloadType": "num",
        "x": 170,
        "y": 180,
        "wires": [
            [
                "f7dcf0d9ada6d747"
            ]
        ]
    },
    {
        "id": "0b875c1ee14b9df5",
        "type": "inject",
        "z": "d362bb7ca55abaf1",
        "name": "0",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "0",
        "payloadType": "num",
        "x": 170,
        "y": 220,
        "wires": [
            [
                "f7dcf0d9ada6d747"
            ]
        ]
    },
    {
        "id": "f7dcf0d9ada6d747",
        "type": "function",
        "z": "d362bb7ca55abaf1",
        "name": "",
        "func": "let msg1 = {}\nlet msg2 = {}\n\nif(msg.payload === 1) {\n    msg1.payload = 1\n    msg2.reset = 1\n    return [msg1, msg2]\n    \n} else {\n    msg2.payload = 0\n    return [null, msg2]\n}\n",
        "outputs": 2,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 340,
        "y": 200,
        "wires": [
            [
                "43fbe55cf81bceeb"
            ],
            [
                "605950b3904ff015"
            ]
        ]
    },
    {
        "id": "43fbe55cf81bceeb",
        "type": "rpi-gpio out",
        "z": "d362bb7ca55abaf1",
        "name": "",
        "pin": "20",
        "set": "",
        "level": "0",
        "freq": "",
        "out": "out",
        "bcm": true,
        "x": 660,
        "y": 180,
        "wires": []
    },
    {
        "id": "3ef47bdf1d466261",
        "type": "rpi-gpio in",
        "z": "d362bb7ca55abaf1",
        "name": "",
        "pin": "4",
        "intype": "tri",
        "debounce": "25",
        "read": true,
        "bcm": true,
        "x": 150,
        "y": 260,
        "wires": [
            [
                "f7dcf0d9ada6d747"
            ]
        ]
    }
]

Retrying.
The wrong flow, corrected.

I agree, but it is easy to adjust that in the Trigger node. I have mine set to three minutes so if you stay still for a short time you don't get plunged into darkness.

You have posted the flow I posted rather than the one shown in the image.

Flow corrected.

I would make a small change to that flow. You should always try to pass on the original message rather than making new ones. Partly for efficiency but in the general case since a common technique is to add data into a message using a property such as msg.myData. This can then be passed through other nodes and one should be able to assume that the additional properties will remain attached to the message.

Edges are sharp. If you don't cover them you will get injured.
If it can happen then eventually it will happen.

Yes the actual implementation will be longer but for testing purposes is 10 seconds because I cant be bothered to wait

You don't seem to have said whether the flow with just the Change and the Trigger node in post #23 works for you.

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