# Condition loop only exits during testing

**URL:** https://discourse.nodered.org/t/condition-loop-only-exits-during-testing/50506
**Category:** General
**Created:** [1 September 2021 17:20 UTC](https://discourse.nodered.org/t/condition-loop-only-exits-during-testing/50506 "2021-09-01T17:20:26Z")
**Posts on this page:** 4
**Page:** 2

<div class="post-metadata">

### Author: ![MarkWalsh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/markwalsh/32/46664_2.png) [@MarkWalsh](https://discourse.nodered.org/u/MarkWalsh)
#### Post date: [3 September 2021 12:16 UTC](https://discourse.nodered.org/t/condition-loop-only-exits-during-testing/50506/21 "2021-09-03T12:16:55Z")

</div>

> [@MarkWalsh](#):
>
> > [@Colin](#):
> >
> > OK. I think once you can get the current intensity the best way to do it might change completely anyway.
> 
> And that's a good ways away, I'm still in the crawling phase.

I realized there is a way to use the current intensity in my flow

Of the 2 nodes available for my lights, one has the capability to only send a value to the lamp, the other has the capability to only receive events from the lamps (a single lamp or all lamps). I can't poll the lamp for it's current value, but I can use the second node to update a variable each time the lamp changes (either through a physical switch, this flow, or any other method), and I can use that variable in the flow to set the initial value, and in the increment function.

Unfortunately, this will never be 100% reliable; if the Node Red server goes out due to a power outage or a shutdown due to an update or maintenance, the value would be out of sync until the next time the light changes. But for my needs, this should be sufficient enough for now.

---

<div class="post-metadata">

### Author: ![MarkWalsh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/markwalsh/32/46664_2.png) [@MarkWalsh](https://discourse.nodered.org/u/MarkWalsh)
#### Post date: [3 September 2021 12:20 UTC](https://discourse.nodered.org/t/condition-loop-only-exits-during-testing/50506/22 "2021-09-03T12:20:52Z")

</div>

> [@Colin](#):
>
> Yes. The delay node holds them in a queue till it is time to release them.
> 
> Alternatively this might be useful to you. I found it by searching for `slew` in the flows site and then realised that I wrote it myself.
> 
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/c/7cfd4db811736419ec50559f8f4ff81791b596ae.png)
> 
> ```auto
> [{"id":"95daacd1.0e09f8","type":"debug","z":"84405ff5.25fa6","name":"","active":true,"console":"false","complete":"false","x":610,"y":2820,"wires":[]},{"id":"c136d3fd.21da78","type":"inject","z":"84405ff5.25fa6","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":150,"y":2780,"wires":[["fc58524d.34f02"]]},{"id":"fc58524d.34f02","type":"function","z":"84405ff5.25fa6","name":"Slew rate limit 10 unit/second","func":"// Limits the slew rate incoming payload values\n// optionally sending intermediate values at specified rate\nlet maxRate = 10; // max slew rate units/second\nlet sendIntermediates = true; // whether to send intermediate values\nlet period = 100; // period in millisecs to send new values (if sendIntermediates)\nlet jumpThreshold = 100; // if the step asked for is more that this then goes immediately to that value\n\nvar newValue = Number(msg.payload);\nvar timer = context.get('timer') || 0;\n// check the value is a number\nif (!isNaN(newValue) && isFinite(newValue)) {\n var target = msg.payload;\n context.set('target', target);\n // set last value to new one if first time through\n var lastValue = context.get('lastValue');\n if (typeof lastValue == \"undefined\" || lastValue === null) {\n lastValue = newValue;\n context.set('lastValue', newValue);\n }\n // calc new value\n msg.payload = calcOutput();\n // stop the timer\n if (timer) {\n clearTimeout(timer);\n context.set('timer', null);\n }\n // restart it if required to send intermediate values\n if (sendIntermediates) {\n timer = setInterval(function(){\n // the timer has run down calculate next value and send it\n var newValue = calcOutput();\n if (newValue != context.get('lastValueSent')) {\n context.set('lastValueSent', newValue);\n node.send({payload: newValue});\n }\n },period);\n context.set('timer', timer);\n }\n context.set('lastValueSent', msg.payload);\n} else {\n // payload is not a number so ignore it\n // also stop the timer as we don't know what to send any more\n if (timer) {\n clearTimeout(timer);\n context.set('timer', null);\n }\n msg = null;\n}\nreturn msg;\n\n// determines the required output value\nfunction calcOutput() {\n var lastValue = context.get('lastValue');\n var target = context.get('target');\n // set to current value if first time through or step > threshold\n if (typeof lastValue == \"undefined\" || lastValue === null) lastValue = target;\n var now = new Date();\n var lastTime = context.get('lastTime') || now;\n // limit value to last value +- rate * time\n var maxDelta = (now.getTime() - lastTime.getTime()) * maxRate/1000;\n if (Math.abs(target - lastValue) > jumpThreshold) {\n // step > threshold so go there imediately\n newValue = target;\n } else if (target > lastValue) {\n newValue = Math.min( lastValue + maxDelta, target);\n } else {\n newValue = Math.max( lastValue - maxDelta, target);\n }\n context.set('lastValue', newValue);\n context.set('lastTime', now); \n return newValue;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":391,"y":2820,"wires":[["95daacd1.0e09f8"]]},{"id":"1501600b.8cd8d","type":"inject","z":"84405ff5.25fa6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"100","payloadType":"num","x":150,"y":2900,"wires":[["fc58524d.34f02"]]},{"id":"82a4bba6819a9b5e","type":"inject","z":"84405ff5.25fa6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"50","payloadType":"num","x":150,"y":2840,"wires":[["fc58524d.34f02"]]}]
> 
> ```

Now that I have found that i have a way to use the current value of the light, I understand that your first suggested flow using the delay wouldn't work, as the messages would have all been sent before the light changes value.

But this flow should be adaptable to use a variable set from the light changing event, so I'll have to look into this at some time in the future to see if it would be usable. For now, I'm going to use my current flow, as it is easily understandable, but this does give me something to think about.

---

<div class="post-metadata">

### Author: ![MarkWalsh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/markwalsh/32/46664_2.png) [@MarkWalsh](https://discourse.nodered.org/u/MarkWalsh)
#### Post date: [3 September 2021 12:24 UTC](https://discourse.nodered.org/t/condition-loop-only-exits-during-testing/50506/23 "2021-09-03T12:24:23Z")

</div>

> [@Colin](#):
>
> > [@MarkWalsh](#):
> >
> > The issue happened once or twice while I was using the isolated Test flow as well.
> 
> In that case perhaps it is a bug in the node. I went to look for the node's github page to see if there were any issues submitted against it, but it's repository is on gitlab and there doesn't seem to be anywhere to submit issues, though I am not familiar with gitlab so that may be my ignorance showing.
> 
> Did you realise that if you re-trigger your test flow before it has completed that it will start running two sequences nested together? The same applies to your modified version.

No extra messages in the debugger this morning, so it looks like the loop exited properly this time. Your assumption that there may be a bug in the node seems possible (although, it's more likely that i may have been using it incorrectly).

Thanks again for your help. Onward to my next Node Red challenge!

---

<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 November 2021 12:24 UTC](https://discourse.nodered.org/t/condition-loop-only-exits-during-testing/50506/24 "2021-11-02T12:24:38Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/condition-loop-only-exits-during-testing/50506.md?page=1)
