Help with setTimeout and ClearTimeout

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:

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:

If anyone can help me I would greatly appreciate it.

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.

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

I recommend watching this playlist: Node-RED Essentials. 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.

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

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.

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.

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

Craig

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.

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;

1 Like

@craigcurtin thank for this link. Very Nice!!

1 Like

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