# Delaying a process question

**URL:** <https://discourse.nodered.org/t/delaying-a-process-question/57530>\
**Category:** General\
**Created:** [31 January 2022 02:58 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530 "2022-01-31T02:58:16Z")\
**Posts on this page:** 19\
**Page:** 2

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [2 February 2022 12:43 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/21 "2022-02-02T12:43:04Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 February 2022 13:47 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/22 "2022-02-02T13:47:48Z")

</div>

@Sean-McG did you forget to post the flow?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 February 2022 14:22 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/23 "2022-02-02T14:22:20Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b58dae99375b98dc2a618cc49083695ef5da8709.png)

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [2 February 2022 15:05 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/24 "2022-02-02T15:05:27Z")

</div>

> [@subwayfootlong](#):
>
> My bad. It sends 1 when movement detected and 0 when there is no movement. There is no delay unlike yours

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

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [2 February 2022 15:48 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/25 "2022-02-02T15:48:15Z")

</div>

> [@Colin](#):
>
> did you forget to post the flow?

No I replied to @xx_Nexus_xx flow from 2 days ago which will do what @subwayfootlong needs 😉  
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.

---

<div class="post-metadata">

**Author:** ![subwayfootlong](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/subwayfootlong/32/42037_2.png) [@subwayfootlong](https://discourse.nodered.org/u/subwayfootlong)\
**Post date:** [2 February 2022 15:58 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/26 "2022-02-02T15:58:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 February 2022 16:02 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/27 "2022-02-02T16:02:27Z")

</div>

> [@subwayfootlong](#):
>
> I was wondering if @Colin had a better way to do it

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

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [2 February 2022 16:09 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/28 "2022-02-02T16:09:27Z")

</div>

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

Does this describe what you want to happen ?

> [@Sean-McG](#):
>
> 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)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 February 2022 17:32 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/29 "2022-02-02T17:32:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [2 February 2022 17:57 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/30 "2022-02-02T17:57:31Z")

</div>

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 😉

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 😉

---

<div class="post-metadata">

**Author:** ![Frida](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/frida/32/68250_2.png) [@Frida](https://discourse.nodered.org/u/Frida)\
**Post date:** [2 February 2022 20:13 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/31 "2022-02-02T20:13:05Z")

</div>

Another way to do it.

 ![2022-02-02 211103](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/9141fcd07dbc92dab24284b510b387cc79b47053.png)

```auto
[
    {
        "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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 February 2022 20:13 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/32 "2022-02-02T20:13:34Z")

</div>

> [@Sean-McG](#):
>
> But 10 seconds is not realistic

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 February 2022 20:15 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/33 "2022-02-02T20:15:53Z")

</div>

> [@Frida](#):
>
> Another way to do it.

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

---

<div class="post-metadata">

**Author:** ![Frida](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/frida/32/68250_2.png) [@Frida](https://discourse.nodered.org/u/Frida)\
**Post date:** [2 February 2022 20:26 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/34 "2022-02-02T20:26:00Z")

</div>

Flow corrected.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 February 2022 20:49 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/35 "2022-02-02T20:49:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 February 2022 20:53 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/36 "2022-02-02T20:53:01Z")

</div>

> [@Sean-McG](#):
>
> it covers edge cases

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

---

<div class="post-metadata">

**Author:** ![subwayfootlong](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/subwayfootlong/32/42037_2.png) [@subwayfootlong](https://discourse.nodered.org/u/subwayfootlong)\
**Post date:** [3 February 2022 07:39 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/37 "2022-02-03T07:39:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 February 2022 11:25 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/38 "2022-02-03T11:25:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [17 February 2022 11:26 UTC](https://discourse.nodered.org/t/delaying-a-process-question/57530/39 "2022-02-17T11:26:24Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/delaying-a-process-question/57530.md?page=1)
