Repeat the same notification every 60 minutes if door sensor is open

Hi I have small problem with this rule
If my gate (door sensor) is open for 61 minutes OH sent me notification in 60 minutes. If my gate is open 4 hours I got only one notification
I want to get information form OH that when my gate is open for 5 hours that every 60 minutes I got the same notification

rule “[OUT] Garaz Maly - kontaktron info mail otwarta brama"
when
Item OUT_GarazMaly_Kontaktron changed
then
if(OUT_GarazMaly_Kontaktron.state==OPEN) {
timer_kontaktron_out_garazmaly = createTimer(now.plusMinutes(60)) [|
sendNotification("hxcdoh@gmail.com”, “[GARAŻ MAŁY] Brama otwarta od 60 min.”)
sendMail("matcin@my.com", “[GARAŻ MAŁY] BRAMA OTWARTA”, “Brama w Grażu Małym jest otwara od 60min.”)
sendMail("i.strojna@outlook.com", “[GARAŻ MAŁY] BRAMA OTWARTA”, “Brama w Grażu Małym jest otwara od 60min.”)
sendMail("haiced@gmail.com", “[GARAŻ MAŁY] BRAMA OTWARTA”, “Brama w Grażu Małym jest otwara od 60min.”)
]
}
else {
if(timer_kontaktron_out_garazmaly!=null) {
timer_kontaktron_out_garazmaly.cancel
timer_kontaktron_out_garazmaly = null
}
}
end

It is a lot easier to review code if you use code boundaries so the indentation is preserved. Use three backtics before and after the code:

    Your code goes here
        it preserves indentation

The above code only sends one alert because you only create one timer.

The flow of events is:

  1. OUT_GarazMaly_Kontaktron changes state
  2. If it changed to OPEN create a timer to go off in 60 minutes, if CLOSED cancel any running timers
  3. After 60 minutes send an email that the door is still open

The door doesn’t change state so there is nothing to cause the rule to run again and create a new Timer.

To get the behavior you are after you need to create or reschedule the Timer after sending the email. The easiest would probably be to just add a timer_kontaktron_out_garazmaly.reschedule to the body of your existing timer body.

If that doesn’t work, you might need a different approach like using a cron triggered rule to poll the door sensor state every 60 minutes and send an alert if it has been open over an hour

rule "[OUT] Garaz Maly - kontaktron info mail otwarta brama"
when
Item OUT_GarazMaly_Kontaktron changed
then
if(OUT_GarazMaly_Kontaktron.state==OPEN) {
timer_kontaktron_out_garazmaly.reschedule = createTimer(now.plusMinutes(60)) [|
sendNotification("hxcdoh@gmail.com", "The gate is open last 60 minutes.")
]
}
else {
if(timer_kontaktron_out_garazmaly!=null) {
timer_kontaktron_out_garazmaly.cancel
timer_kontaktron_out_garazmaly = null
}
}
end

I added it but without any effects. Stil I have had only one notification
Somebody can help me with it

If you want a message every hour, you need to recreate the Timer after it goes off.

You are using reschedule incorrectly. You call that to change the time that the timer will go off. What is here makes no sense.

What you are after is hard to implement using Timers. It is hard because once a timer goes off to must create a new one which is really hard to do inside the timer itself. You end up with a similar problem to pointing a camera at its monitor. You end up with an infinite number if copies of the code.

There are ways around this but they are not with the effort in this case.

An easier, though perhaps less precise approach is to:

-set up persistence on the door item
-create a rule that runs periodically, say once every five minutes

  • in the rule check to see if the door is open. If it is see when it was opened. If it has been open for an hour or more and it has been more than an hour since the last Notification
  • send the notification
  • save a timestamp so we know when the last Notification was sent

Hi rich, I want to set a rules that if no one at home is true and door was opened within 10 minutes, turn off all light in house, I don’t know how to write the rule relate to timestamp, can you light me up?

Write a rule that:

  • put your door in persistence so we can see when it was last opened
  • triggers when your Presence state goes to OFF (i.e. no one is home)
  • create a Timer for 10 minutes in the Timer:
  • if(Door.state == CLOSED && Door.previousState(true).timestamp.after(now.minusMinutes(10).toDate)) { // turn off lights }

The if statement gets the previous state that is different from the current one and if it occurred within ten minuted turn off the lights.

I could never really settle on an openHAB rule for what I wanted accomplish with repeating notifications. I’ve shared a Node Red solution here Monitor sensor data/status with Node Red I hope it’s useful.