How to count up variable while Input is high

Hello,
I need to count up a variable every 400ms as long (while) the Input of a KNX-Switch is “ON”.
I tried several things. Last one was:

[Code]
rule "Dimmertaster"
when
Item KNX_HWR_TestTaste received command
then
var Number percent = 0

while(receivedCommand==ON) {
    Thread::sleep(400)
    percent = percent + 5
    postUpdate(Dimmer_Ingo, percent);
    logInfo("ONN",now + ": StartZeit= " + StartTime_Ingo + "; Offf= " + StopTime_Ingo)
    }

[/Code]

This is not working as the variable gets countet up, even if the input is not “ON” anymore.
What I want, only count up as long as the Input is “ON” and stop counting on when Input gets “OFF” again.

Thanks for reading (and helping) in advance,
Ingo

1 Like

I haven’t tried this so no guarantees that it works…

  • Create a rule that trigger on KNX-Switch receiving command ON
  • In the rule, create a timer (createTimer) with a body that increases your variable and then reschedule the timer.
  • Create another rule that trigger on KNX-Switch receiving command OFF
  • In the rule, cancel the timer.

In general I think this should work. I am not so sure about millisecond precision on timers, though.

receivedCommand only gets set when the rule first triggers. It doesn’t get updated after that. You want:

while(KNX_HWR_TestTaste.state ==ON)

Thanks, that’s it.
Ingo