# Simple settimeout function

**URL:** <https://discourse.nodered.org/t/simple-settimeout-function/38721>\
**Category:** General\
**Created:** [7 January 2021 15:11 UTC](https://discourse.nodered.org/t/simple-settimeout-function/38721 "2021-01-07T15:11:28Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [7 January 2021 21:26 UTC](https://discourse.nodered.org/t/simple-settimeout-function/38721/21 "2021-01-07T21:26:51Z")

</div>

Two things:

- just to make sure you are using the second updated version of the function i posted?
- im pretty sure there is a race condition happening where the closing messages are happening so close together that the timer var is not yet set to false in context when already the other 2 have triggered their own instances of the function as this would explain that only ever the false messages are duplicated

Maybe try adding a delay node in rate limit mode set to something like one message every 100ms before the function node.  
Edit:  
Do as @dceejay says below

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [7 January 2021 21:28 UTC](https://discourse.nodered.org/t/simple-settimeout-function/38721/22 "2021-01-07T21:28:31Z")

</div>

so yes - that lastactivation property prevents the rbe from working (as it is always different). - so - use a change node to move `msg.payload.ContactSensorSate` to `msg.payload` (so we just have the useful part - ie the state) - then an rbe node (to deduplicate that) - then @JGKK original flow.

---

<div class="post-metadata">

**Author:** ![Winni](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/winni/32/34648_2.png) [@Winni](https://discourse.nodered.org/u/Winni)\
**Post date:** [8 January 2021 11:51 UTC](https://discourse.nodered.org/t/simple-settimeout-function/38721/23 "2021-01-08T11:51:38Z")

</div>

Thanks alot! I re-implemented the state calling by not using homebridge node but directly from deconz where its originally implemented (Xiaomi Sensor read by Rasbee modul). That prevents from getting multiple different messages from the beginning. Could´ve thought about that eralier sorry 😕  
Now i´m getting one simple true if its opened and one false if its closed after rbe node and the original flow works fine. The function returns 2x true and 1x false when opened for +3sec and 3x false when closed again +1x false after another 3sec after closing.

```auto
[{"id":"a6a49b9f.703138","type":"deconz-input","z":"44c6aba9.4558b4","name":"","server":"b66ed896.a3d74","device":"00:15:8d:00:01:f3:6a:76-01-0006","device_name":"Haustür : ZHAOpenClose","topic":"","state":"open","output":"onchange","outputAtStartup":true,"x":280,"y":580,"wires":[[],["da1ab79c.c6761"]]},{"id":"da1ab79c.c6761","type":"rbe","z":"44c6aba9.4558b4","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":510,"y":580,"wires":[["24da88b9.53bc68"]]},{"id":"24da88b9.53bc68","type":"function","z":"44c6aba9.4558b4","name":"","func":"let timer = context.get(\"timer\") || false;\nswitch(msg.payload.ContactSensorState){\n case 1:\n if (!timer) {\n timer = setTimeout(()=>{\n context.set(\"timer\", false);\n node.send({payload:{on:true}});\n },3000);\n context.set(\"timer\",timer);\n }\n break;\n case 0:\n if (timer) {\n clearTimeout(timer);\n context.set(\"timer\", false);\n node.send({payload:{on:false}});\n }\n break;\n}\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":700,"y":580,"wires":[["21ecd98e.1d4e16"]]},{"id":"21ecd98e.1d4e16","type":"debug","z":"44c6aba9.4558b4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":580,"wires":[]},{"id":"b66ed896.a3d74","type":"deconz-server","name":"Rasbee","ip":"192.168.178.17","port":"80","apikey":"B84F7EA25A","ws_port":"443","secure":false,"polling":"15"}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/9/994aef231788dfc96053cbd34ead40042ac7680f.png)

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [8 January 2021 14:56 UTC](https://discourse.nodered.org/t/simple-settimeout-function/38721/24 "2021-01-08T14:56:28Z")

</div>

Have a play with this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/f/ef28ed76dc36ade2d5c835cb4f0f7cee57533076.jpeg)

```auto
[{"id":"5c34e9c0.c762","type":"inject","z":"c38d0f4.885dff","name":"Open","props":[{"p":"payload.ContactSensorState","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":470,"y":460,"wires":[["efac3fa8.e076d","24da88b9.53bc68"]]},{"id":"c1c9de36.978c98","type":"inject","z":"c38d0f4.885dff","name":"Closed","props":[{"p":"payload.ContactSensorState","v":"0","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":470,"y":580,"wires":[["24da88b9.53bc68","26fa87b2.464718"]]},{"id":"24da88b9.53bc68","type":"function","z":"c38d0f4.885dff","name":"","func":"const last = context.get(\"last\");\nif (last !== msg.payload.ContactSensorState) {\n context.set(\"last\", msg.payload.ContactSensorState);\n} else {\n return null;\n}\nlet timer = context.get(\"timer\") || false;\nswitch(msg.payload.ContactSensorState){\n case 1:\n if (!timer) {\n timer = setTimeout(()=>{\n context.set(\"timer\", false);\n node.send({payload:{on:true}});\n },3000);\n context.set(\"timer\",timer);\n }\n break;\n case 0:\n if (timer) {\n clearTimeout(timer);\n context.set(\"timer\", false);\n node.send({payload:{on:false}});\n } else {\n node.send({payload:{on:false}});\n }\n break;\n}\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":800,"y":560,"wires":[["21ecd98e.1d4e16"]]},{"id":"21ecd98e.1d4e16","type":"debug","z":"c38d0f4.885dff","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":950,"y":560,"wires":[]},{"id":"d224ca9c.39cb7","type":"function","z":"c38d0f4.885dff","name":"open x3","func":"for(i=0;i<3;i++){\n node.send({payload:{ContactSensorState:1}});\n}\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":520,"wires":[["24da88b9.53bc68","efac3fa8.e076d"]]},{"id":"c545aee3.6cc78","type":"function","z":"c38d0f4.885dff","name":"close x3","func":"for(i=0;i<3;i++){\n node.send({payload:{ContactSensorState:0}});\n}\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":640,"wires":[["24da88b9.53bc68","26fa87b2.464718"]]},{"id":"efac3fa8.e076d","type":"debug","z":"c38d0f4.885dff","name":"Open","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":460,"wires":[]},{"id":"1feff751.2512c9","type":"inject","z":"c38d0f4.885dff","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":480,"y":640,"wires":[["c545aee3.6cc78"]]},{"id":"ebfce448.1ef628","type":"inject","z":"c38d0f4.885dff","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":480,"y":520,"wires":[["d224ca9c.39cb7"]]},{"id":"26fa87b2.464718","type":"debug","z":"c38d0f4.885dff","name":"Closed","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":640,"wires":[]}]

```

I don’t know what your sensor is sending but I can’t get the function node to behave in any way I wouldn’t expect even when throwing multiple things at it at the same time.  
I added rbe functionality directly in the function node.

```auto
const last = context.get("last");
if (last !== msg.payload.ContactSensorState) {
    context.set("last", msg.payload.ContactSensorState);
} else {
    return null;
}
let timer = context.get("timer") || false;
switch(msg.payload.ContactSensorState){
    case 1:
        if (!timer) {
            timer = setTimeout(()=>{
                context.set("timer", false);
                node.send({payload:{on:true}});
            },3000);
            context.set("timer",timer);
        }
        break;
    case 0:
        if (timer) {
            clearTimeout(timer);
            context.set("timer", false);
            node.send({payload:{on:false}});
        } else {
            node.send({payload:{on:false}});
        }
        break;
}
return null;

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/e/2e192e0941282527dd888ddd294916b97802c124.jpeg)

---

<div class="post-metadata">

**Author:** ![Winni](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/winni/32/34648_2.png) [@Winni](https://discourse.nodered.org/u/Winni)\
**Post date:** [8 January 2021 15:22 UTC](https://discourse.nodered.org/t/simple-settimeout-function/38721/25 "2021-01-08T15:22:15Z")

</div>

The function just works perfectly fine now!  
One "true" msg after 3 seconds of opening, one "false" msg after closing, before or after the timeframe.  
Don´t know why the rbe didn´t work in the first place but the function is perfection now without any rbe involved!  
Thanks a lot again for your ongoing help!!!

---

<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:** [22 January 2021 15:22 UTC](https://discourse.nodered.org/t/simple-settimeout-function/38721/26 "2021-01-22T15:22:24Z")

</div>

Closed on 22 January 2021 15:22 UTC

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

[Previous page](https://discourse.nodered.org/t/simple-settimeout-function/38721.md?page=1)
