Timer that can be interrupted, now using expire

In the future I want to use 2 KNX relay ports to control my shutters (up and down) and I want to do this using only one push button.

I started experimenting and I have the following rule that works quite okay, the only thing i want to add in the rule is a timer function.
In both switches, rol_UP and rol_down, i set up an expire of 5 seconds (the time it takes the shutter to move up/down). But it is this timing I want to add in my rule and not in my switch, it seems a more elegant solution for me.

Now the idea is:
push button (wip1) is changed;
if the shutter is up it will move down (until the 5s expire is over) and vice versa.
when the button (wip1) is changed and the shutters are moving they are stopped, paused and rol_UP is activated for 5s, reopening the shutters.

I tried using "Thread::sleep " and “createTimer” but both keep on running and can’t be stopped once started. Apparently the expire attribute can be interrupted. Or I am doing something wrong with sleep or timer?.

I think I’m looking for a function: turn on for X seconds or until turned off.

Any tips on this?


 //rol_loc: 0=down  100=up  150=inWork   work in progress, to check location
rule "rolluiken"
when
        Item knxschakelaar_wip1 changed
then
        if(rol_loc.state==0 && rol_down.state == OFF && rol_UP.state== OFF ) {   // going up/open
        rol_UP.sendCommand(ON)  //thing is a 5 second expire switch
        rol_loc.sendCommand(150)
        rol_loc.sendCommand(100)
        }

        if(rol_loc.state==100 && rol_down.state == OFF && rol_UP.state== OFF ) {  // going down/close

        rol_down.sendCommand(ON)        // thing is a 5 second expire switch
        rol_loc.sendCommand(150)
        rol_loc.sendCommand(0)
         }


        if(rol_down.state == ON || rol_UP.state == ON) {  // stop while moving
        rol_down.sendCommand(OFF)
        rol_UP.sendCommand(OFF)
        Thread::sleep(500)      // pause to protect shutter motor from direct switching
        rol_UP.sendCommand(ON)
        rol_loc.sendCommand(150)
        rol_loc.sendCommand(100)
          }
end

I would recommend using blocklies as described here but of course the same can be achieved with DSL rules. The reference (which is currently in the writing) shows that you should have everything you need for the timers (of course also creating timers)

image

and you can use the following blocks to persist a rule-global-variable that could keep track of you current movement state.

I can’t help you exactly because it is not clear to me what the 5 seconds are for. Do you want to stop the motor after 5 seconds? In that case use a “after 5 second do with timer” block. If you need to cancel a timer, use the cancel block and you can also use the checks to know if a timer is still running and then cancel it (e.g. you probably want to do that if you want to stop the rollershutter).

To sum up, there is most likely everything there that you need only that do not exactly know what you want.

Yes, you cannot interrupt or cancel a sleep.

Probably. Using createTimer in a files based rule gives you great flexibility to cancel, reschedule, whatever, as you require. But you haven’t shown us your code tat didn’t work.

Look for examples that use reschedule and/or cancel, and note the usage of declaring a ‘global’ handle for the timer.