Check if a switch was on in the passed x minutes / seconds

Hey guys,

I need some help again or at least a little push into the right direction.
I want to improve my alarm system which gets activtated as soon as i lock my front door.
Currently it always gets activated when the door gets locked even if we lock it from the inside. But there is no need to activate it as long as we are in the house.

My idea is to use the motion detector in my hall. It is very close to the door. And if you move away from the door you have to pass it there is no other way. So when the door gets looked and than the motion detector detects a motion alarm system should not go on. I know how to create the rule for that itself what I don’t know is how to check the sensor for a period of time.

The idea is to define that x seconds after the front door gets locked and at least one time the motion detector detects a motion alarm stays off. So rule described with words.

rule "Alarmanlageaktivierung"
when
    Item EG_Flur_Fenster_Haustuer_Schloss changed to CLOSED
then
    if (EG_Flur_BWM_Wand_Schalten did not changed to ON for 30 seconds){
    Alarmanlage_Scharfschaltungsobjekt_Allgemein.sendCommand(ON)    
    }
end

I guess thats an easy task but I need a little push to find the way.

It looks like you are using .rules files, that way you can use a globally defined timer. That timer should be created in the above posted rule and it should activate the alarm after xx seconds.
In another rule which needs to be saved in the same file! trigger on motion detection and in the THEN part check whether the above timer is active and cancel it.

1 Like

Thanks a lot. That was exactly the push I needed. Solution is working fine.

1 Like

Another approach would be to use persistence and call lastUpate on the lock item. Depending on how persistence is configured that will give you a datetime of the last update to the item.

When using persistence you’d also need to take the previousState into account, otherwise when you unlock the lock, go inside and the motion sensor triggers and it arms the alarm.

@opus

Maybe you could help me once again.
I spend some more time in this and wanted to implent that it gets checked if the timer is really running before cancling it. I ususally do this in separate rules and not my productiv ones.

I did following:

var Timer Testtimer

rule "Timer Test"
when
    Item Test_Schalter changed to ON
then
    EG_Buero_Licht_vorne.sendCommand(ON)
    Testtimer = createTimer(now.plusSeconds(20), [|
    logInfo ("rumtesten", "Timer gestartet")
    EG_Buero_Licht_vorne.sendCommand(OFF)
    ])
end

rule "Testerei 2"
when
    Item Test_Schalter2 changed to ON
then
    if (Testtimer !== null){
    Testtimer.cancel()
        logInfo ("rumtesten", "Timer wurde abgeschaltet")
    }
    if (Testtimer === null){
        logInfo ("rumtesten", "Timer ist bereits aus")
    } 
end

It doesn’t matter if the timer got canceld by the rule or the timer run out every time I fire that rule I get “Timer wurde abgeschaltet” in my log so it looks like the timer is always not null. But if timer run out or it got canceld by the same rule before it should not be running so I should get “Timer ist bereits aus” in my log…

In your timer below the line:

EG_Buero_Licht_vorne.sendCommand(OFF)

add:

Testtimer = null

and do the same after you cancel the timer in “Testerei 2”.

2 Likes

Remember the variable Testtimer is just a handle, a pointer, to a Timer object. Cancelling or rescheduling or executing the Timer code does not change the pointer in any way.

If you want to set it null to indicate the Timer is done with, this is a useful technique - but it is up to you to set it null yourself.

3 Likes

@marcel_erkel @rossko57

Thanks for raising this.

I think i created a good example to describe the handling of timers with my testing rules now and switched my logging texts from german to english so maybe this will help someone struggeling with the same issue in the future.

Thank you guys!

var Timer Testtimer

rule "Timer Test"
when
    Item Test_Schalter changed to ON
then
    EG_Buero_Licht_vorne.sendCommand(ON)
    logInfo ("rumtesten", "Timer startet")
    Testtimer = createTimer(now.plusSeconds(20), [|
    EG_Buero_Licht_vorne.sendCommand(OFF)
    logInfo ("rumtesten", "Timer run out")
    Testtimer = null
    ])
end

rule "Testerei 2"
when
    Item Test_Schalter2 changed to ON
then
    if (Testtimer !== null){
    Testtimer.cancel()
        logInfo ("rumtesten", "Timer got cancelled manually")
        Testtimer = null
    }
    else if (Testtimer === null){
        logInfo ("rumtesten", "Timer is not running")
    } 
end
2 Likes