Functionnode: If loop with time

I have written this function that if the value x for a certain time
(Time1) is higher than the value y. All values ​​are numbers. After I'm not familiar with javascript I do not know why the if loop with the counter does not work

x = msg.payload.down
y = msg.topic.topic1
time = msg.payload.Time1
z = 0

while (x >= y) {
    if (z >= time){
        msg.payload.Was = "Jetzt"
    } 
    else {
        z += 1
    }
}
while (x < y) {
    z = 0
{
return msg;

So you are comparing x against y but don't change either x or y so the condition you are testing will never change.

while (x < y) {
z = 0
{
same thing and you need to close your while statement. (the function node should have flagged this with a red cross indicating a problem...)

See https://www.w3schools.com/js/js_loop_while.asp

I find if you talk yourself through your code you will spot problems...

x = msg.payload.down //didnt declare x. Should be  var x  or   let x
y = msg.topic.topic1 //didnt declare y. Should be  var y  or   let y
time = msg.payload.Time1 //again no declare
z = 0 //you get the idea

while (x >= y) { //so loop round and round and round until x is > or = to y
    if (z >= time){
        msg.payload.Was = "Jetzt"
    } 
    else {
        z += 1
    }
} //end of loop - but hang on,  x  never changed - this will loop for ever!

while (x < y) {//again, loop round until x < y.  where do you change   x  ?
    z = 0
{
return msg; 
  // <<-- missing a close brace here!

I suspect you think a new payload will update x and y.

You are not thinking in terms of node-red flow programming me thinks.

Perhaps you should describe what you are trying to achieve?
Perhaps we can guide you?

1 Like

Thanks, yes I could use help and I hope it is understandable what I explain below.

I have a photodiode which gives me a number value of the voltage of the diode. And also I have a dashboard on which one set the desired voltage at which commanded (shutters down) to be executed. But the command he should be executed when the value of the photodiode for a certain time (Time1) falls below the value of a Dashboardslider

I will send you the exportet file later, if you need it.

it will be helpful to post screen shots of what you have done so far & the flow.

Remember to paste your exported flow between three backticks e.g.

```
your flow here
```

However, some pointers first...

  • Checkout the trigger & delay node (read the info on the side-bar for clues)
  • Get rid of the loop in the function
    • Not least because you will cause an infinite loop and cause other issues

The problem is just that I can not connect to the Node red editor, which means I can not make screenshots or similar.

Probably because you flow/function is in an infinite loop!
So stop node-red and restart Node-RED with the --safe setting which starts node-red, loads your flow but doesn't deploy.