How to delay in a while loop?

Hiho, here I come with another problem:
This will kill the connection because of running in a loop without delay.
What to do now ?

while(utsRelayOff > utsCurrent)
{
    utsCurrent = parseInt(+new Date());
    utsRelayOff = flow.get("utsRelayOff_t");
    //sleep(10);
}

btw: the delay node is useless until I can not break it / or change the delay time,

the delay node is useless until I can not break it / or change the delay time,

The delay node is quite fantastic.

But what about explaining your topic, what are you trying to do ?
What is the plan for the while loop ?

We cannot guess what you are trying to create.

Well its not hard to understand:
utsCurrent is the current time

utsRelayOff is a flow value that will be changed while in the loop by another function.
This will happen if a motion is detected by a PiR Sensor ... so if a motion is detected the time for sending a message to switch off the relay / light should be delayed.

The background is to send a message that will switch off a relay -> light. The flow is big and its not usefull to post I think.

That sounds more complicated than it has to be.

Look into the trigger node. if PIR, send nothing, wait for x time (extend the delay), time passed, send off-message.

Also note that a while loop within a node-red function is not recommended as you potentially block the flow (ie single thread). I wonder why you need loops in your flow. Are you coming from a programming perspective ?

3 Likes

Yes I am a coder and sometime its hard to use node where a solution by code is so easy.
Thanx for reply. Using a trigger node didn´t do it for me. I used a function node with 2 outputs plus a a delay node to emulate the while loop and used another function node to build the code outside of the while loop around. but thats much overhead just because of missing the support of delay in node red.

Why didn't the Trigger node do it for you? It is designed for exactly the use you've described;

  • receive a message (a trigger from your PIR), wait X seconds until sending the 'off' message
  • if another message is received from the PIR, extend the delay

Well, that's JavaScript for you. There are plenty of ways of achieving a delay - just not in the way you want to do it.

Javascript is new for me too... which ways are there to sleep in js in my case ?
I dont like node with much options since debugging is not possible in a classic way I did avoid the trigger node and took what was clear to me to get a fast result. - My wife is on my neck because of the not working lights at my home :slight_smile:
I hope to find the time to try trigger node later on.

Simply speaking, in JS everything is event-driven. Events are processed by the event loop, only one event ("piece of code") can run at any time there. Running CPU intensive tasks or things like your while-loop will block the event loop and your whole runtime will lock, as you have observed.

This is where callbacks come in. E.g., you can use setTimeout() to execute a piece of code after a specific time by specifying a callback function and a timeout. This way, your code will not block the event loop. That's what the delay and trigger nodes are using under the hood.

Here is some light reading on the topic: :grinning:

Perhaps you have a misconception on what node-red does and how to use it.

Yes you can use programming (JS) in functions for more customized control, this should not be needed for your example (unless we are missing some information).

Node-red in a nutshell to me: a message has a lifecycle, comes in at point A and goes to point B or any other direction (to the right), no loops.
You connect nodes together to make this work. Need input from other nodes accross flows ? Use link nodes. Personally, I consider variables a last resort, function nodes as well.

Your PIR example is simple if you use the principle above.

My house is full with PIR's and triggers together with all kinds of conditions in between them. No function nodes needed.

Fair enough - except in this instance the 'fast result' isn't happening because you are trying to do something in a language you are not familiar with. We have pointed out the Trigger node a couple times now as being the ideal fit for what you want. You could have the result working in a couple minutes if you tried it and got your lights working. That doesn't stop you pursuing a code-only approach if you really wanted to in parallel... but I suspect once you see how easy the Trigger node makes it, you'd realise it wasn't worth reinventing its functionality in code you'd have to maintain.

1 Like