Openhab Contact Sensor Rules

Hi guys!

I’ve got the current rules in my Openhab which basically just sends my phone an alert if I have left the gate open after 10 minutes.

RULES FILE:

//Gate Sensor

rule "Gate Sensor"
when
    Item MGateSensor1 changed
then
    if(Notifications.state == ON) {
       if (triggeringItem.state == OPEN) Gate_Timer.sendCommand(ON) // Start timer
        else Gate_Timer.postUpdate(OFF) // Cancel the timer
    }
end

rule "Gate Timer"
when
    Item Gate_Timer received command OFF
then
    sendBroadcastNotification("ALERT: Gate has been OPEN for 10 minutes!")


end

ITEMS FILE:

Switch Gate_Timer { expire="10m,command=OFF" }//

I basically want the system to send me a notification once the gate has been closed, but only after it has send a message saying it’s been left open.

EG. 0-9 mins = no notification
10 mins = notification that the gate has been left open
more than 10 mins = notification only when the gate get’s closed.

Anyone have any ideas?

Contact door
Contact doorSendOpenMsg {expire="10m,state=CLOSED"}
rule "door"
when
    Item door changed
then
    doorSendOpenMsg.postUpdate(door.state)
end

rule "doorsendOpenMsg CLOSED"
when
    Item doorSendOpenMsg changed to CLOSED
then
    if (door.state == OPEN) {...}
end

first of all: please use Code fences (one of the Icons on the top Right) - makes the Code more readable…

second: you are not sooo much away. If I understand correctly, you only want a notification, if you left the gate open for more than 10mins?

so, basically what you did was:

  1. Setting upt the Gate_Timer Switch with a 10min. expire time (good!)
  2. writing a rule “Gate Sensor” which sets the time to ON, if the Gate is open (good!)
    you won’t Need the OFF though…
  3. writing a rule “Gate Timer” which handles the notification (good!) - but it Always sends it, if the Gate_Timer receives OFF

notice, I moved the “notification” clause to the sendnotification-rule, as it is better to use it just before that.
notice also, that you don’t need to change the Timer to OFF - but you should check, whether the gate is OPEN and the Timer just went OFF.

so - let’s do it:
item:

Switch Gate_Timer { expire=“10m,command=OFF” } // leave that! It tells the timer to switch OFF after 10mins

Gate Sensor rule

rule "Gate Sensor"
when
	Item MGateSensor1 changed
then
	if (triggeringItem.state == OPEN) Gate_Timer.sendCommand(ON) // Start timer
end

Gate Timer rule

rule "Gate Timer"
when
	Item Gate_Timer received command OFF
then
	if (Notifications.state == ON && MGateSensor1.state == OPEN) { 
		// I guess you use the Notificatins-variable to surpess notifications - so better to place it in front of notifications!
		// So, basically, if Notifications are ON *and* the Gate ist OPEN *and* Timer went OFF, then send notifications
		sendBroadcastNotification("ALERT: Gate has been OPEN for 10 minutes!")
	}
end

Hi Thomas,

Thank’s for your reply! My current code / rules works fine with the notifications - I simply want a notification to tell me once the gate has been closed. Let me try clarify. There is basically 2 possible scenarios that could occur. Gate being closed before 10 minutes or gate being closed after 10 mins.

Scenario 1 //
00:00 - Gate Opens / no notification (done)
00:10 - Notification sent out alerting gate been left open (done)
00:15 - Gate gets closed manually / Send out notification alerting that the gate has been closed (what I’m after)

Scenario 2 //
00:00 - Gate Opens
00:05 - Gate gets closed / do not send out any notifications

Hope this helps! My current setup for the rules works just without the closed notification.

Would it be possible to simply add an if statement after my

sendBroadcastNotification(“ALERT: Gate has been OPEN for 10 minutes!”)
var openMsgHasSend = false

rule "door"
when
    Item door changed
then
    if (door.state == CLOSED && openMsgHasSend) {
		sendBroadcastNotification(“INFO: Gate has been CLOSED!”)
		openMsgHasSend = false}
	doorSendOpenMsg.postUpdate(door.state)
end

rule "doorsendOpenMsg CLOSED"
when
    Item doorSendOpenMsg changed to CLOSED
then
    if (door.state == OPEN) {
		sendBroadcastNotification(“ALERT: Gate has been OPEN for 10 minutes!”)
		openMsgHasSend = true}
end

you can do it with a timer.

var Timer fronddoorTimer = null
var  alarm = false
rule "Gate Sensor open"
when
Item MGateSensor1 changed from CLOSED to OPEN

then

      if(fronddoorTimer === null ) {
        fronddoorTimer = createTimer(now.plusMinutes(10), [|
            fronddoorTimer = null
alarm = true
sendBroadcastNotification("ALERT: Gate has been OPEN for 10 minutes!")
        ]) 

    }

end
rule "Gate Sensor close"
when
Item MGateSensor1 changed from OPEN to CLOSED
then

if(fronddoorTimer !==null) fronddoorTimer.cancel() //Terminate timer if door is closed before the timer ends
if (alarm === true) {
sendBroadcastNotification(“ALERT: Gate has been CLOSE!”)  
  alarm = false
}

end