Alarm email if doors remains open for longer time

I do have OpenHab installed on a laptop. It does nothing but I think i could use some help to make it doing something.
I do have a door sensor and my doors are sometimes not shutting properly.
The question is how to create a rule that will send me an email or some kind of alert that doors are open over a minute?
Where to set it up.
so 2 questions how to trigger if a state of zone is OPEN longer than 60 seconds and how to send an email?

Have you tried to look for the design patterns (DP), especially working with timers?
If not, try read it…
It is not difficult and we will help you, but you should make a start…

1 Like

Thank you for your reply, so as I understand there is no way to do that in a graphical interface and it is just archivable by code?
I read an article about DP but I need small step by step how to do that a first time.

Which version of openHAB, 2.5 or 3.0?

You can do this through the UI, but ultimately you will have to actually write some code. And the UIs are very different between the two versions.

Or you can just write a .rules file and put it in /var/lib/openhab2/rules and not use the UI at all.

I got 2.5 shall i upgrade to 3 before i start?

There isn’t a lot of docs for OH 3 yet. I’ve been using it and it’s pretty stable so far but you are largely on your own and there are still bug.

Out of necessity we tend to be a “teach a man to fish” type forum. Given that, I recommend going to the Rules section of the docs and see if you can create a simple rule. From there you can expand and gradually build up the rules to do what you want. Whether you stick to the UI for everything or write rules in text files, you will have to understand the same concepts and you will have to write code. So even if you move to the UI later on you will not have wasted any time.

1 Like

i did create a rule but I see no option how to build it to trigger only if it is longer than some time?

You can’t, at least not that easily. Maybe try this method:

Create a rule which:

  • Triggers when the door is opened
  • Starts a timer for X minutes. Configure the timer so that when it finishes, it sends an email IF the door is still open.

So I’d research two topics:

  • Timers
  • Mail Binding

I do something similar with lights left on when my presence is not detected in the house.

I use the telegram binding to ask question

rule "Send telegram with question"
    Item Presence changed to OFF
    val telegramAction = getActions("telegram","telegram:telegramBot:2b155b22")
    telegramAction.sendTelegramQuery("No one is at home, but some lights are still on. Do you want me to turn off the lights?", "Reply_Lights", "Yes", "No")

Handle incoming mesage

rule "Reply handler for lights"
    Item telegramReplyId received update Reply_Lights
    val telegramAction = getActions("telegram","telegram:telegramBot:2b155b22")

    if (telegramMessage.state.toString == "Yes")
        telegramAction.sendTelegramAnswer(telegramReplyId.state.toString, "Ok, lights are *off* now.") 
        telegramAction.sendTelegramAnswer(telegramReplyId.state.toString, "Ok, I'll leave them *on*.")

These are my rules for sending a notification if the front door is opened for too long:
(Not 100% it will compile since I have removed and simplified it a bit)


sitemap doorexample label="Front Door" {
	Frame {
           Setpoint item=NotifyFrontDoorOpenTime minValue=1 maxValue=120 step=1


DateTime FrontDoorLastOpened            "Last Opened [%1$tY-%1$tm-%1$td %1$tH:%1$tM]" <time>
Number    NotifyFrontDoorOpenTime      "Front Door Open warn [%d min]"    <door>                (gRestore)


import java.text.SimpleDateFormat

val SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss")
val String LOG_FD = "FrontDoorSensor"
var Timer mFronDoorOpenTimer = null

rule "FrontDoorOpenAlarm"
     Item ZwaveFrontDoorSensorStatus changed to OPEN
    FrontDoorLastOpened.postUpdate(new DateTimeType())
    logInfo(LOG_FD, "Updating front door opened: " + FrontDoorLastOpened.state.toString)
    NotifyFrontDoorNotif.sendCommand("Front Door Opened: " + FrontDoorLastOpened.state.toString)
    mFronDoorOpenTimer = createTimer(now.plusMinutes( (NotifyFrontDoorOpenTime.state as DecimalType).intValue), [|
                                         logInfo(LOG_FD, "Front Door Open for too Long send warning! Time open: " + frontDoorWarningTime + " mins")
                                         NotifyFrontDoorWarn.sendCommand("Warning Front Door open!")
                                         mFronDoorOpenTimer = null

rule "CancelFrontDoorTimer"
     Item ZwaveFrontDoorSensorStatus changed to CLOSED
    if (mFronDoorOpenTimer !== null) {
        mFronDoorOpenTimer = null

rule "FrontDoorWarning"
    Item NotifyFrontDoorWarn received command
        var String message = receivedCommand.toString
        // Send notifications using message like myMqtt.sendCommand(message)
        logInfo(LOG_FD, "Notification should have been sent")

All is great I just need to read more to know where to start it all.
I will start looking at YT as i am missing the start
I hope that once i will learn how to do the first correct rule that rest will go easier.
Is there any step by step how to start the rules and scripts in OH?

All you need to do is open a file with a .rules extension in /etc/openhab2/rules and type in the code, using the links and examples above and the docs as a guide for the syntax. When you save the file openHAB will load it and the rule will start running when the defined events occur.

That’s all there is to it. Create the file and watch the logs for errors.

@Seaside, those look like OH 1.x rules. You do not and should not import anything from org.eclipse.smarthome in your rules. In fact, in OH 3 you will get errors on those lines as all of those classes have moved elsewhere. Even in OH 1.x you never really needed those imports.

Also, it is best to avoid the use of primitives except where absolutely necessary and when it is necessary avoid converting to a primitive at the last possible moment. Use of primitives like the above can add minutes to the loading times for your .rules files.

    val frontDoorWarningTime = NotifyFrontDoorOpenTime.state as Number
    mFrontDoorOpenTimer = createTimer(now.plusMinutes(frontDoorWarningTime.intValue), ...

@rlkoshak yes it originates from oh1, I’ve run it in oh2 and just recently rewritten it in jython . I agree with all your comments :+1:
Updated example as well.