The counter has to start somewhere. Presumably it is a global variable? And it somehow maps to the current position of the shutter I’m assuming (e.g. if the shutter is 50% open the counter is 6).
With those assumptions:
This is a really dangerous implementation. It is a really bad idea to have long sleeps inside a Rule. You can only run 5 Rules at the same time. If something goes wrong or for some reason Arbeitszimmer_Rolladen changes a too much it will cause ALL your Rules to stop running until one exits. And if you have an error like Vincent pointed out, the Rule will never exit.
It is much better to use Timers to push the delay outside of the Rule. And you really don’t need a counter. You know how long to wait so just wait for that amount of time.
Also, a counter is a really poor choice for this. You can just calculate how long to wait based on the desired % and sleep for that amount of time.
val totalUp = 2000 // total time to go from fully closed to fully open in millis
cal totalDown = 3000 // total time to from fully open to fully closed in millis
rule "Arbeitszimmer_Rollo"
when
Item Arbeitszimmer_Rolladen_Proxy received command // send a command to tell it what percent you want the shutter opened to
then
val percent = Math::abs((Arbeitszimmer_Rolladen.state - Arbeitszimmer_Rolladen_Proxy.state).intValue) // calculate the difference
val direction = if(Arbeitszimmer_Rolladen.state < Arbeitszimmer_Rolladen_Proxy.state) DOWN else UP
val time = if(direction == UP) (totalUp * percent) / 100 else (totalDown * percent) / 100
Arbeitszimmer_Rollo.sendCommand(direction)
createTimer(now.plusMillis(time), [ |
Arbeitszimmer_Rollo.sendCommand(STOP)
Arbeitszimmer_Rollo_Proxy.postUpdate(Arbeitszimmer_Rollo.state)
]
end
I may have the directions mixed up above and there could be other typos but the overall concept is:
- you have a proxy Item that you use to send the command to the rollershutter telling it what percent you want the shutter to open to.
- you calculate the difference between the current percentage and the desired percentage
- you calculate the direction the shutter needs to go to reach the desired percentage
- you calculate how long it will take to get there based on how far it needs to go and how long it takes to get there based on the direction going. Put in words, if it takes 2 seconds to go up and you need to go up by 25%, you need to go up for 500 milliseconds (i.e. 2000 * .25).
- Now send the command to start going in the right direction and create a timer to sleep for the needed amount of time and when the timer goes off stop the rollershutter.
- Make sure to synchronize the proxy with the actual rollershutter once it stops.