Here is the code. Sorry if its sloppy, I haven’t coded in a long time.
preferences {
section("Turn on when Door Opened:") {
input "thecontact", "capability.contactSensor", required: true, title: "Multi Sensor?"
}
section("Turn off after...") {
input "minutes", "number", required: true, title: "Minutes?"
}
section("Select a light") {
input "switch1", "capability.switchLevel", required: true
input "brightness1", "number", title: "Brightness"
}
section("Select another light") {
input "switch2", "capability.switchLevel", required: false
input "brightness2", "number", title: "Brightness", required: false
}
section("Program active between these hours") {
input "fromTime", "time", title:"From"
input "toTime", "time", title: "To"
}
section("Program active on these days") {
input "days", "enum", title: "Select Days of the Week", required: true, multiple: true, options: ["Monday": "Monday", "Tuesday": "Tuesday", "Wednesday": "Wednesday", "Thursday": "Thursday", "Friday": "Friday", "Saturday": "Saturday", "Sunday": "Sunday"]
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
subscribe(thecontact, "contact.open", doorOpenedHandler)
}
def doorOpenedHandler(evt) {
log.debug "doorOpenedHandler called: $evt"
def switch1check = switch1.currentState("switch") //get switch1 state
def switch2check = switch2.currentState("switch") //get switch2 state
def df = new java.text.SimpleDateFormat("EEEE")
// Ensure the new date object is set to local time zone
df.setTimeZone(location.timeZone)
def day = df.format(new Date())
def dayCheck = days.contains(day)
if (dayCheck){
def between = timeOfDayIsBetween(fromTime, toTime, new Date(), location.timeZone)
if (between){
if(switch1check.value != "on" && switch2check.value != "on"){ //if switch1 and switch2 are not on, continue
switch1.on()
switch1.setLevel(brightness1)
switch2.on()
switch2.setLevel(brightness2)
runIn(60 * minutes, lightsOff)
}
else if(switch1check.value != "on" && switch2check.value == "on"){ //if switch1 is not on and switch2 is on, continue
switch1.on()
switch1.setLevel(brightness1)
runIn(60 * minutes, light1Off)
}
else if(switch1check.value == "on" && switch2check.value != "on"){ //if switch1 is on and switch2 is not on, continue
switch2.on()
switch2.setLevel(brightness2)
runIn(60 * minutes, light2Off)
}else { //switch1 and switch2 are on, do nothing
}
}
}
}
def lightsOff() {
switch1.off()
switch2.off()
}
def light1Off() {
switch1.off()
}
def light2Off() {
switch2.off()
}