definition( //parent: 'Mark-C-UK:**tbc**', name: 'alarm Mgr', namespace: 'Mark-C-UK', author: 'Mark C', description: 'do alarm stuff', category: 'Convenience', iconUrl: '', iconX2Url: '', pausable: true ) preferences { page(name: 'mainPage') } Map mainPage() { dynamicPage(name: 'mainPage', title: 'Setup', uninstall: true, install: true) { section { input 'alarm', 'capability.alarm', title: 'Switches to monitor and control', multiple: false, required: true input 'time', 'number', title: 'time in min to re-arm when the alarm was in armed home and someone comes home (blank to disable)', required: false input 'PreSens', 'capability.presenceSensor', title: 'Presense sensors', multiple: true, required: false input 'PDiss', 'bool', title: 'Use presence disarm', defaultValue: false, submitOnChange: true if(PDiss== true){ app.updateSetting('Day',[value:'false',type:'bool']) app.updateSetting('Eve',[value:'false',type:'bool']) } input 'PArm', 'bool', title: 'Use presence Arm away', defaultValue: false, submitOnChange: true if(PArm== true){ app.updateSetting('Away',[value:'false',type:'bool']) } input 'Day', 'bool', title: 'Use mode to day to disarm', defaultValue: false, submitOnChange: true if(Day== true){ app.updateSetting('PDiss',[value:'false',type:'bool']) } input 'Eve', 'bool', title: 'Use mode to Evening to disarm', defaultValue: false, submitOnChange: true if(Eve== true){ app.updateSetting('PDiss',[value:'false',type:'bool']) } input 'Night', 'bool', title: 'Use mode to Night to arm home', defaultValue: false, submitOnChange: true input 'Away', 'bool', title: 'Use mode to awary to arm away', defaultValue: false, submitOnChange: true if(Away== true){ app.updateSetting('PArm',[value:'false',type:'bool']) } input 'logEnable', 'bool', title: 'Enable logging', defaultValue: false, submitOnChange: true } } } void updated() { log.info "updated ${app.label}" unsubscribe() unschedule() initialize() } void installed() { log.info "Installed ${app.label}" initialize() } void uninstalled(){ unschedule() unsubscribe() log.info "uninstalled ${app.label}" } void initialize() { String currentstate = alarm.currentmodes log.info "${app.label} ${alarm} is $currentstate" if (logEnable==true) { runIn (86400, logsOff) log.info "${app.label} loging will be disabled in 24 hours" } if (time != null && PreSens != null) log.info "${app.label} - when in ARM HOME auto dis arm and re arm on pressance event after $time" if (PDiss == true || PreSens != null) subscribe(PreSens, 'presence.present', PreHandler) if (PArm == true && PreSens != null) subscribe(PreSens, 'presence.not present', PreHandler) if (Day == true || Eve == true || Night == true || Away == true) subscribe(location, 'mode', modeCh) log.info "${app.label} initializing and subscribing to ${PreSens}" } void logsOff(){ app?.updateSetting('logEnable',[value:'false',type:'bool']) log.warn "${app.label} loging disabled" } void PreHandler(evt){ String logTxt = "Presence event - ${evt.device.name} is ${evt.value}, " if (evt.value == 'present' && PDiss == true){ String currentstate = alarm.currentmodes logTxt += "current mode is $currentstate, " if (currentstate != 'disarmed'){ logTxt += 'DISARMED, ' AlamContol('disarm') if (time != null && currentstate == 'armed home'){ logTxt += "REARMING to ArmHome in ${time}min" runIn (5, AlamContol, [data:'armHome']) } } logTxt += "no action, " } else if (evt.value == 'not present' && PArm == true) { state.pre = false for (com.hubitat.app.DeviceWrapper dev in PreSens) { if (dev.currentValue('presence') == 'present') { logTxt += "$dev is present, " state.pre = true } } if (state.pre != true){ logTxt += "ARMING, " runIn (5, AlamContol, [data:'armAway']) //runIn (5, ArmAway) } else { logTxt += "no action, " //return } } else { logTxt += "no action, " } if (logEnable==true) log.info "${app.label} $logTxt" } void modeCh(evt){ String logTxt = "Mode event - ${evt.value}, " String currentstate = alarm.currentmodes if ((evt.value == 'Day' && Day == true && currentstate != 'disarmed') || (evt.value == 'Evening' && Eve == true && currentstate != 'disarmed')) { logTxt += 'DISARMED, ' AlamContol('disarm') } else if (evt.value == 'Night' && Night == true && currentstate != 'armed home') { logTxt += 'ARM-HOME, ' runIn (5, AlamContol, [data:'armHome']) } else if (evt.value == 'Away' && Away == true && currentstate != 'armed away'){ logTxt += 'ARM-AWAY, ' runIn (5, AlamContol, [data:'armAway']) } else { logTxt += 'no action, ' } if (logEnable==true) log.info "${app.label} $logTxt" } void AlamContol(String command){ // this is contol point for the alarm if (logEnable==true) log.trace "${app.label} Alarm Contol command $command" runIn (2*60, refresh) alarm."$command"() } void refresh(){ if (logEnable==true) log.trace "${app.label} refreshing" alarm.refresh() }