# Help with setTimeout and ClearTimeout

**URL:** https://discourse.nodered.org/t/help-with-settimeout-and-cleartimeout/64651
**Category:** General
**Created:** [3 July 2022 21:17 UTC](https://discourse.nodered.org/t/help-with-settimeout-and-cleartimeout/64651 "2022-07-03T21:17:22Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![amkochaki](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@amkochaki](https://discourse.nodered.org/u/amkochaki)
#### Post date: [3 July 2022 21:17 UTC](https://discourse.nodered.org/t/help-with-settimeout-and-cleartimeout/64651/1 "2022-07-03T21:17:22Z")

</div>

Hey guys.  
I’m learning node red, I’m pretty basic in programming, but I’ve already managed to do some cool things. I’m trying to do something with my occupancy sensors I have them all over the house.  
I created a subflow with two functions, turn the light on and another function turn off the light this worked but now I’m trying to determine a time for the lamps to turn off I’m having a lot of difficulties in the function to turn off the light (turn\_of).  
It has several conditionals in it, my biggest problem is resetting the time to turn off the lamp when the presence sensor is activated. What I need is this to reset the time to turn off the lamp every time the presence sensor goes on. Can someone help me? That is my funcion turn\_off:

```auto
if((msg.payload === "on"))  
        {
        clearTimeout(setar);
        node.send({payload:false}); //{payload:false}
}

if((msg.payload === "off"))  
        {
if((entities[grupo].state === "on") && (entities[sensor].state === "on") ) 
{            
   setar = setTimeout(function(){
        msg.payload = {
                domain: "light",
                service: "turn_off",
                data: {
                    entity_id: [grupo],
                }
        };
        node.send(msg);
        },
        meutempo);
}
}

return null;

```

and this is my subflow:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b24a119f69ea8bf5342f5b3c0334dc49c48cff78.png)

If anyone can help me I would greatly appreciate it.

---

<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: [3 July 2022 21:31 UTC](https://discourse.nodered.org/t/help-with-settimeout-and-cleartimeout/64651/2 "2022-07-03T21:31:01Z")

</div>

> [@amkochaki](#):
>
> `setar`

`setar` does not exist the next time the function is called, try saving it to context to retrieve it next time the function is called and msg.payload === on, then delete it from context.

---

<div class="post-metadata">

### Author: ![amkochaki](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@amkochaki](https://discourse.nodered.org/u/amkochaki)
#### Post date: [3 July 2022 22:12 UTC](https://discourse.nodered.org/t/help-with-settimeout-and-cleartimeout/64651/3 "2022-07-03T22:12:04Z")

</div>

I thought that defining it as a variable would do. Likewise I'm still pretty bad at node red and programming. You could help me as if I were a 6 year old. A lot of things I don't understand, I read a lot about it but I still haven't been able to develop

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [3 July 2022 22:14 UTC](https://discourse.nodered.org/t/help-with-settimeout-and-cleartimeout/64651/4 "2022-07-03T22:14:27Z")

</div>

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<div class="post-metadata">

### Author: ![amkochaki](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@amkochaki](https://discourse.nodered.org/u/amkochaki)
#### Post date: [3 July 2022 22:29 UTC](https://discourse.nodered.org/t/help-with-settimeout-and-cleartimeout/64651/5 "2022-07-03T22:29:21Z")

</div>

Thanks for the tip, I watched the videos, but I still have some difficulties

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [3 July 2022 22:46 UTC](https://discourse.nodered.org/t/help-with-settimeout-and-cleartimeout/64651/6 "2022-07-03T22:46:39Z")

</div>

You might be better off using the trigger node for now and saving the function node approach for when you have a bit more experience with JavaScript/Node.js.

The trigger node lets you trigger a second msg after a set time delay.

---

<div class="post-metadata">

### Author: ![amkochaki](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@amkochaki](https://discourse.nodered.org/u/amkochaki)
#### Post date: [3 July 2022 22:55 UTC](https://discourse.nodered.org/t/help-with-settimeout-and-cleartimeout/64651/7 "2022-07-03T22:55:10Z")

</div>

I have an automation where any user can change the times and senors according to the needs of each one at each moment. The problem with the trigger number is that I don't know how to use an input\_number as the time to trigger or turn off a device.

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [3 July 2022 23:36 UTC](https://discourse.nodered.org/t/help-with-settimeout-and-cleartimeout/64651/8 "2022-07-03T23:36:24Z")

</div>

Can i suggest you watch this video on YT - the guys is all across HA and pretty good on NR and he is doing somethign very similar to what you want to do in one of the advanced options

[![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/a/5aa300796d69492336d98fc92ed2187cf4b75095.jpeg "Node Red + Home Assistant 2022: Beginner, Advanced and EXPERT Motion Detection and Notifications") ](https://www.youtube.com/watch?v=wBUAUdLkYJM)

Craig

---

<div class="post-metadata">

### Author: ![amkochaki](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@amkochaki](https://discourse.nodered.org/u/amkochaki)
#### Post date: [3 July 2022 23:36 UTC](https://discourse.nodered.org/t/help-with-settimeout-and-cleartimeout/64651/9 "2022-07-03T23:36:45Z")

</div>

Thank you for everyone's suggestion, especially the one from @E1cid, it enlightened me a lot, I modified the function and it now seems to be working. The first tests are ok. If anyone has any more tips or finds any errors I would appreciate it. Because I still have many doubts. Below is how the function of the node.

```auto
const entities = global.get('homeassistant').homeAssistant.states;
var grupo = env.get("grupo");
var sensor = env.get("sensor");
var tempo = env.get("tempo");
var mytime = (entities[tempo].state * 1000 *60)

function desligarFunction(){
        msg.payload = {
                domain: "light",
                service: "turn_off",
                data: {
                    entity_id: [grupo],
                }
        };
    node.send(msg);
}

function setarFunction() {
  let setar = setTimeout(desligarFunction, mytime);
  context.set('mem',setar);
}

function ResetFunction() {
  let setar = context.get('mem');    
  clearTimeout(setar);
  node.send({payload:false});  
}

if((msg.payload === "on"))  
        {
        ResetFunction(); //{payload:false}
}

if((msg.payload === "off"))  
        {
        if((entities[grupo].state === "on") && (entities[sensor].state === "on") ) 
            {            
            setarFunction();
            }
        }

return null;

```

---

<div class="post-metadata">

### Author: ![amkochaki](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@amkochaki](https://discourse.nodered.org/u/amkochaki)
#### Post date: [4 July 2022 00:07 UTC](https://discourse.nodered.org/t/help-with-settimeout-and-cleartimeout/64651/10 "2022-07-04T00:07:07Z")

</div>

@craigcurtin thank for this link. Very Nice!!

---

<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: [2 September 2022 00:07 UTC](https://discourse.nodered.org/t/help-with-settimeout-and-cleartimeout/64651/11 "2022-09-02T00:07:12Z")

</div>

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