import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
// Timer for door opener
var Timer timer = null
// Timer for alarm mail
var Timer mailTimer = null
// Timer for alarm mail
var Timer sirenTimer = null
// Alarm mail text
var String mailtext = ""
// Last caller ID on FB
var String lastCallNo = ""
// executed when system starts
rule "Startup"
when
System started or
Time cron "0 0/5 * * * ?"
/*
* order for cron expressions
* 1. Seconds
* 2. Minutes
* 3. Hours
* 4. Day-of-Month
* 5. Month
* 6. Day-of-Week
* 7. Year (optional field)
*/
then
// switch main power led - it shows that openHAB is up and running
sendCommand(AL_OUT_Main, OPEN)
sendCommand(AL_OUT_Main, CLOSED)
// poll key switch state to set alarm main switch item state correctly at startup
if(AL_IN_ON.state == ON) {
postUpdate(Main_Switch, 2)
} else if( AL_IN_Monitoring.state == ON ) {
postUpdate(Main_Switch, 1)
} else {
postUpdate(Main_Switch, 0)
}
logInfo("Alarm", "System ok")
// remove if correctly wired
postUpdate(GATE_Carport, CLOSED)
postUpdate(GATE_Front, CLOSED)
postUpdate(AL_OUT_Line1, OPEN)
postUpdate(AL_OUT_Line2, OPEN)
postUpdate(AL_OUT_Intruder, OPEN)
end
// only executed if "shutdown" ist typed at osgi console :-(
rule "Shutdown"
when
System shuts down
then
sendCommand(AL_OUT_Main, OPEN)
sendCommand(AL_OUT_Entry, OPEN)
logInfo("Alarm", "shut down")
end
// capture changes on key switch to/from monitoring position (only notification)
// and set correct Main_Switch item state
rule "Monitoring Switch"
when
Item AL_IN_Monitoring received update
then
if( AL_IN_Monitoring.state == ON ) {
if( AL_IN_ON.state == ON ) {
logInfo("Main Switch", "set to ON")
postUpdate(Main_Switch, 2)
} else {
postUpdate(Main_Switch, 1)
logInfo("Main Switch", "set to Monitoring")
}
} else {
if( AL_IN_ON.state == ON ) {
logInfo("Main Switch", "set to ON")
postUpdate(Main_Switch, 2)
} else {
logInfo("Main Switch", "set to OFF")
postUpdate(Main_Switch, 0)
}
}
end
// capture changes on key switch to/from alerting position
// and set correct Main_Switch item state
rule "On Switch"
when
Item AL_IN_ON received update
then
if(AL_IN_ON.state == ON) {
logInfo("Main Switch", "set to ON")
sendCommand(Main_Switch, 2)
} else {
if( AL_IN_Monitoring.state == ON ) {
logInfo("Main Switch", "set to Monitoring")
postUpdate(Main_Switch, 1)
} else {
logInfo("Main Switch", "set to OFF")
postUpdate(Main_Switch, 0)
}
}
end
// Reset alarm mail text in notification and off state.
// Experiments with NMA actions.
rule "Main Switch actions"
when
Item Main_Switch changed
then
if(Main_Switch.state == 2) {
notifyMyAndroid("Hauptschalter", "AN")
} else if( Main_Switch.state == 1 ) {
notifyMyAndroid("Hauptschalter", "STILL")
mailtext=""
// switch siren off or cancel siren timer
sendCommand(AL_OUT_R2, OFF)
if( sirenTimer != null ) {
sirenTimer.cancel()
sirenTimer = null
}
} else {
mailtext=""
// switch siren off or cancel siren timer
sendCommand(AL_OUT_R2, OFF)
if( sirenTimer != null ) {
sirenTimer.cancel()
sirenTimer = null
}
}
end
// No changing commands on Main_Switch item is allowed
rule "Main Switch GUI"
when
Item Main_Switch received command
then
if(AL_IN_ON.state == ON) {
postUpdate(Main_Switch, 2)
} else if( AL_IN_Monitoring.state == ON ) {
postUpdate(Main_Switch, 1)
} else {
postUpdate(Main_Switch, 0)
}
end
// Collect door states when in alerting mode
rule "Front Doors"
when
Item TG_Frontdoor received update or
Item EG_Frontdoor received update
then
if( TG_Frontdoor.state == OPEN || EG_Frontdoor.state == OPEN ) {
if( Main_Switch.state == 2 ) {
mailtext=mailtext+"Geoeffnet wurde: "
if( EG_Frontdoor.state == OPEN ) {
mailtext= mailtext + "Haustuer\n"
}
if( TG_Frontdoor.state == OPEN ) {
mailtext= mailtext + "Kellertuer\n"
}
}
sendCommand(AL_OUT_Entry, OPEN)
} else if( TG_Frontdoor.state == CLOSED && EG_Frontdoor.state == CLOSED ) {
sendCommand(AL_OUT_Entry, CLOSED)
}
end
// Collect gate states when in alerting mode
rule "Gates"
when
Item GATE_Front received update or
Item GATE_Carport received update
then
if( GATE_Front.state == OPEN || GATE_Carport.state == OPEN ) {
if( Main_Switch.state == 2 ) {
mailtext=mailtext+"Geoeffnet wurde: "
if( GATE_Front.state == OPEN ) {
mailtext= mailtext + "Eingangstor\n"
}
if( GATE_Carport.state == OPEN ) {
mailtext= mailtext + "Rolltor\n"
}
}
// sendCommand(AL_OUT_Entry, OPEN) //currently not wired
} else if( GATE_Front.state == CLOSED && GATE_Carport.state == CLOSED ) {
// sendCommand(AL_OUT_Entry, CLOSED) //currently not wired
}
end
// start alarm timer if doors are opened
rule "Entry Alarm"
when
Item Doors changed
then
if( Doors.state == OPEN ) {
if( mailTimer == null ) {
logInfo("Alarm", "start timer for action")
// create one minute alarm timer
mailTimer = createTimer(now.plusSeconds(60)) [|
mailtext=mailtext+"Geoeffnet sind:\n"
if( EG_Frontdoor.state == OPEN ) {
mailtext= mailtext + "Haustuer\n"
}
if( TG_Frontdoor.state == OPEN ) {
mailtext= mailtext + "Kellertuer\n"
}
// if( GATE_Front.state == OPEN ) {
// mailtext= mailtext + "Gartentor\n"
// }
// if( GATE_Front.state == OPEN ) {
// mailtext= mailtext + "Carport\n"
// }
if( Main_Switch.state == 2 ) {
// only send mail if still in alerting mode
logWarn("Alarm", "send mail: " + mailtext)
mailtext="";
} else if( Main_Switch.state == 1 ) {
// if in notification mode, write mailtext to log
logWarn("Alarm", "monitoring: " + mailtext)
}
else {
// something happened with the doors (just for debugging)
logInfo("Alarm", "off:doors")
}
mailTimer = null
]
// create 10 minutes siren timer
sirenTimer = createTimer(now.plusSeconds(600)) [|
// make noise
sendCommand(AL_OUT_R2, ON)
sirenTimer = null;
]
}
}
end
// Operate dooropener relais from GUI :-)
rule "Dooropener"
when
Item AL_OUT_R1 received command
then
if(receivedCommand==ON) {
if(timer==null) {
// first ON command, so create a timer to turn the opener on
timer = createTimer(now.plusSeconds(3)) [|
// after 3 sec turn the opener off
sendCommand(AL_OUT_R1, OFF)
]
} else {
// subsequent ON command, so reschedule the existing timer
timer.reschedule(now.plusSeconds(3))
}
} else if(receivedCommand==OFF) {
// remove any previously scheduled timer
if(timer!=null) {
timer.cancel
timer = null
}
}
end
*** snap ***