# Timer with queuing?

**URL:** <https://discourse.nodered.org/t/timer-with-queuing/4086>\
**Category:** General\
**Created:** [18 October 2018 11:29 UTC](https://discourse.nodered.org/t/timer-with-queuing/4086 "2018-10-18T11:29:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [18 October 2018 11:29 UTC](https://discourse.nodered.org/t/timer-with-queuing/4086/1 "2018-10-18T11:29:49Z")

</div>

I could not think of a better title.

What am I trying to accomplish:

- Start recording motion detection from webcamera,
- keep it running at least 30 seconds and
- as long as no "motion detection:off" has been received.

The problem with motion detection is that it goes: on...off, on..off, on.....off if you pass by the camera and I want to keep it running during that time.

My flow (the "Thuis" part is an occupancy check):

 ![17](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/18856e5ec483291aecb17b1feabecd4b1a5f064b.jpeg)

I tried to play with a delay node, the delay node will drop the messages in between including the last "off" message, doesn't seem to work.

How can I:

- send the initial "on", run at least 30 seconds, ignore the "off" during that time,
- if another "on" has been received in between, reset the 30 second timer,
- if no "on" was received in that time, turn it off ?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [18 October 2018 11:38 UTC](https://discourse.nodered.org/t/timer-with-queuing/4086/2 "2018-10-18T11:38:29Z")

</div>

Hi @bakman2

You can use the `Trigger` node to achieve this.

From the sounds of it, you only really care about the 'on' messages. So your flow can use a Switch node to only pass those ones on.

You can then use a `Trigger` node configured to pass on the initial message, then wait for 30 seconds, extending the delay if new messages arrive. Then send an 'off' message.

 ![Local_Node-RED](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/88a3540817b3cbe0b3c7285ff39b01cdb32465d8.png)

 ![Local_Node-RED](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/91160e93f62b3a1aad02e5b8c4c197a94d5af3ca.png)

```auto
[{"id":"4dcbea6.cd50414","type":"inject","z":"24d928f3.3c58f8","name":"","topic":"","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":175,"y":719,"wires":[["9a288da0.280c5"]]},{"id":"3e41d776.3cb308","type":"inject","z":"24d928f3.3c58f8","name":"","topic":"","payload":"off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":760,"wires":[["9a288da0.280c5"]]},{"id":"fa4c377a.4284a8","type":"trigger","z":"24d928f3.3c58f8","op1":"","op2":"off","op1type":"pay","op2type":"str","duration":"30","extend":true,"units":"s","reset":"","bytopic":"all","name":"","x":530,"y":720,"wires":[["496c3c1b.ad6d54"]]},{"id":"9a288da0.280c5","type":"switch","z":"24d928f3.3c58f8","name":"ignore off","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":360,"y":720,"wires":[["fa4c377a.4284a8"]]},{"id":"496c3c1b.ad6d54","type":"debug","z":"24d928f3.3c58f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":750,"y":720,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [18 October 2018 13:48 UTC](https://discourse.nodered.org/t/timer-with-queuing/4086/3 "2018-10-18T13:48:09Z")

</div>

I see what you are saying and it seems to be working indeed.

As I am using an exec node that I want to kill once the trigger has finished, I try to "then send" `{"kill":"SIGHUP"}` as json but this gets encapsulated by a key/value of payload, is this correct ? eg:

`{"payload":{"kill":"SIGINT"},"_msgid":"fc4c145e.fb36b8"}`

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [18 October 2018 13:50 UTC](https://discourse.nodered.org/t/timer-with-queuing/4086/4 "2018-10-18T13:50:51Z")

</div>

> [@bakman2](#):
>
> is this correct

Yes, the 'then send' field is specifying the payload the node sends. If you need other properties set on the message, add a Change node afterwards to set what you need besides the payload.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [18 October 2018 14:37 UTC](https://discourse.nodered.org/t/timer-with-queuing/4086/5 "2018-10-18T14:37:24Z")

</div>

Got it! Change node didn't work for me, but function to the rescue.  
Thanks for the quick response/help.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [19 October 2018 05:47 UTC](https://discourse.nodered.org/t/timer-with-queuing/4086/6 "2018-10-19T05:47:11Z")

</div>

I don't want to butt in, but you last post is confusing:

> [@bakman2](#):
>
> Change node didn't work for me

It should.

Nick isn't the type to write faulty code.

For the sake of curiosity, could you elaborate on what is happening?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [19 October 2018 06:39 UTC](https://discourse.nodered.org/t/timer-with-queuing/4086/7 "2018-10-19T06:39:44Z")

</div>

You are right and triggered me to check it again.  
I have not used the change node before tbh.

I was using Set the other way around as:

```auto
set: msg.payload.kill
to: msg.kill

```

I read this as source \> dest, but I should read it as `set new prop` from `source`, switched it and it works.

Thanks for the trigger.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [19 October 2018 06:41 UTC](https://discourse.nodered.org/t/timer-with-queuing/4086/8 "2018-10-19T06:41:48Z")

</div>

No worries.

I have fallen for that trap myself a few times too. So don't fret on it too much.

🙂
