# Counter + Loop to check the door sensor

**URL:** <https://discourse.nodered.org/t/counter-loop-to-check-the-door-sensor/81011>\
**Category:** General\
**Created:** [3 September 2023 19:19 UTC](https://discourse.nodered.org/t/counter-loop-to-check-the-door-sensor/81011 "2023-09-03T19:19:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [3 September 2023 19:19 UTC](https://discourse.nodered.org/t/counter-loop-to-check-the-door-sensor/81011/1 "2023-09-03T19:19:11Z")

</div>

Hello everyone,  
Can anyone give me a good solution to the following problem?  
: I attached a sensor to a door. When opening the door, I get the payload: "open" or when closing comes "closed"  
That works quite well so far.  
My problem is that I would like to have a timer to which I can react if the door is open for longer than 5 minutes, for example.  
I have already tried to solve this with a counter node. Unfortunately, I only manage to trigger the counter with both messages. It should of course be the case that the counter switches off within the time when the door is closed. And when the counter is reached, it should be checked again whether the door is still open. If so, I would like to send a message to alexa or if the whole thing has been sent to alexa 3 times and the door is still open, send an email.  
It is not about the output of Alexa or sending an email, but only about the function that something should be checked again after a certain time and only then should a reaction be made. So actually a kind of "loop" that is executed until a condition is met. So either the door is closed or just a message.  
Perhaps one of you has already realized something like this?  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)\
**Post date:** [3 September 2023 19:53 UTC](https://discourse.nodered.org/t/counter-loop-to-check-the-door-sensor/81011/2 "2023-09-03T19:53:42Z")

</div>

You need to show some code but You can send an 'on' to turn on a timer (mytimer - my module 🙂 ) and an off or cancel can turn it off otherwise it triggers. It sends and receives via MQTT.

---

<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:** [3 September 2023 20:17 UTC](https://discourse.nodered.org/t/counter-loop-to-check-the-door-sensor/81011/3 "2023-09-03T20:17:02Z")

</div>

Assuming that "I have not yet received 'Close'" is equivalent to "The door is still open" you can do it like this

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4fa78710e22543f15c52df3bb6780ffe80879228.jpeg)

```auto
[{"id":"5c35f879e30c4e86","type":"inject","z":"142f935612b6e391","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Open","payloadType":"str","x":90,"y":40,"wires":[["61a49049877c4a50"]]},{"id":"85f93cd0cc48fabb","type":"inject","z":"142f935612b6e391","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Close","payloadType":"str","x":90,"y":80,"wires":[["61a49049877c4a50"]]},{"id":"931a4abf9e94499b","type":"trigger","z":"142f935612b6e391","name":"","op1":"","op2":"Door has been open for 5 minutes","op1type":"nul","op2type":"str","duration":"5","extend":false,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":670,"y":60,"wires":[["77bb6810962c2ab1"]]},{"id":"c8ffa58679d37cda","type":"change","z":"142f935612b6e391","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":120,"wires":[["931a4abf9e94499b"]]},{"id":"77bb6810962c2ab1","type":"debug","z":"142f935612b6e391","name":"debug 395","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":120,"wires":[]},{"id":"eaef9761993d57fc","type":"switch","z":"142f935612b6e391","name":"Open or Close?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Open","vt":"str"},{"t":"eq","v":"Close","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":320,"y":60,"wires":[["931a4abf9e94499b"],["c8ffa58679d37cda"]]},{"id":"61a49049877c4a50","type":"junction","z":"142f935612b6e391","x":200,"y":60,"wires":[["eaef9761993d57fc"]]}]

```

---

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [4 September 2023 07:18 UTC](https://discourse.nodered.org/t/counter-loop-to-check-the-door-sensor/81011/4 "2023-09-04T07:18:35Z")

</div>

> [@jbudd](#):
>
> ```auto
> [{"id":"5c35f879e30c4e86","type":"inject","z":"142f935612b6e391","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Open","payloadType":"str","x":90,"y":40,"wires":[["61a49049877c4a50"]]},{"id":"85f93cd0cc48fabb","type":"inject","z":"142f935612b6e391","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Close","payloadType":"str","x":90,"y":80,"wires":[["61a49049877c4a50"]]},{"id":"931a4abf9e94499b","type":"trigger","z":"142f935612b6e391","name":"","op1":"","op2":"Door has been open for 5 minutes","op1type":"nul","op2type":"str","duration":"5","extend":false,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":670,"y":60,"wires":[["77bb6810962c2ab1"]]},{"id":"c8ffa58679d37cda","type":"change","z":"142f935612b6e391","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":120,"wires":[["931a4abf9e94499b"]]},{"id":"77bb6810962c2ab1","type":"debug","z":"142f935612b6e391","name":"debug 395","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":120,"wires":[]},{"id":"eaef9761993d57fc","type":"switch","z":"142f935612b6e391","name":"Open or Close?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Open","vt":"str"},{"t":"eq","v":"Close","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":320,"y":60,"wires":[["931a4abf9e94499b"],["c8ffa58679d37cda"]]},{"id":"61a49049877c4a50","type":"junction","z":"142f935612b6e391","x":200,"y":60,"wires":[["eaef9761993d57fc"]]}]
> 
> ```

Hi! thank you! this one works fine ! i set 3 trigger. 1. for 2 minutes, 2. 3 minutes, and the last one to 5 minutes. so I can react differently in each case.

THX

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [4 September 2023 13:11 UTC](https://discourse.nodered.org/t/counter-loop-to-check-the-door-sensor/81011/5 "2023-09-04T13:11:24Z")

</div>

Just for info, you can do this with one trigger and different topics and delay times  
e.g.

```auto
[{"id":"5c35f879e30c4e86","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"open","payloadType":"str","x":570,"y":100,"wires":[["1a38a487735d5c28"]]},{"id":"1a38a487735d5c28","type":"junction","z":"b9860b4b9de8c8da","x":440,"y":240,"wires":[["07cb9683b646344c","92ec7fde4781279f","b97b7eafd2c29e36"]]},{"id":"85f93cd0cc48fabb","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"closed","payloadType":"str","x":570,"y":140,"wires":[["1a38a487735d5c28"]]},{"id":"07cb9683b646344c","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"two","tot":"str"},{"t":"set","p":"delay","pt":"msg","to":"120000","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":200,"wires":[["931a4abf9e94499b"]]},{"id":"92ec7fde4781279f","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"three","tot":"str"},{"t":"set","p":"delay","pt":"msg","to":"180000","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":240,"wires":[["931a4abf9e94499b"]]},{"id":"b97b7eafd2c29e36","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"five","tot":"str"},{"t":"set","p":"delay","pt":"msg","to":"300000","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":280,"wires":[["931a4abf9e94499b"]]},{"id":"931a4abf9e94499b","type":"trigger","z":"b9860b4b9de8c8da","name":"","op1":"","op2":"open fo 5 mins","op1type":"nul","op2type":"str","duration":"5","extend":false,"overrideDelay":true,"units":"min","reset":"closed","bytopic":"topic","topic":"topic","outputs":1,"x":770,"y":120,"wires":[["63c8b4ab0d573b01"]]},{"id":"63c8b4ab0d573b01","type":"switch","z":"b9860b4b9de8c8da","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"two","vt":"str"},{"t":"eq","v":"three","vt":"str"},{"t":"eq","v":"five","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":910,"y":120,"wires":[["dba9ccb33364b1e3"],["dba9ccb33364b1e3"],["dba9ccb33364b1e3"]]},{"id":"dba9ccb33364b1e3","type":"template","z":"b9860b4b9de8c8da","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Open for {{topic}} minutes","output":"str","x":820,"y":260,"wires":[["77bb6810962c2ab1"]]},{"id":"77bb6810962c2ab1","type":"debug","z":"b9860b4b9de8c8da","name":"debug 395","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":990,"y":260,"wires":[]}]

```

 ![chrome_screenshot_1693832874132](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/4/44692af9d59cc42fe09a80828d9bb89e3afddbb3.png)

---

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [12 September 2023 05:54 UTC](https://discourse.nodered.org/t/counter-loop-to-check-the-door-sensor/81011/6 "2023-09-12T05:54:02Z")

</div>

thank 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:** [26 September 2023 05:54 UTC](https://discourse.nodered.org/t/counter-loop-to-check-the-door-sensor/81011/7 "2023-09-26T05:54:27Z")

</div>

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