Ok I have been searching through some code and was able to copy and past some stuff into @baldeagle072 to get a bit closer. But still not working correctly. I have renamed it âWater Cooler Testâ. Not sure if this is how we are supposed to do it in he community if not someone let me know and i will remove the following code.
/**
*/
definition(
name: âWater Cooler Testâ,
namespace: âbaldeagle072â,
author: âEric Robertsâ,
description: âThe cooler will turn on if everybody arrives home and the water cooler virtual switch is on. If the water cooler virtual switch turns off or everyone leaves, it will turn off the cooler.â,
category: âConvenienceâ,
iconUrl: âhttps://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.pngâ,
iconX2Url: âhttps://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.pngâ,
iconX3Url: âhttps://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.pngâ)
preferences {
section(âWater Coolerâ) {
input âcoolerâ, âcapability.switchâ, title: âWater Coolerâ, multiple: false, required: true
input âvCoolerâ, âcapability.switchâ, title: âVirtual Switch for coolerâ, multiple: false, required: true
}
section("People") {
input "presenceSensors", "capability.presenceSensor", title: "Presence Sensor(s)", multiple: true, required: true
}
section(title: "More options", hidden: hideOptionsSection(), hideable: true) {
def timeLabel = timeIntervalLabel()
href "timeIntervalInput", title: "Only during a certain time", description: timeLabel ?: "Tap to set", state: timeLabel ? "complete" : null
input "days", "enum", title: "Only on certain days of the week", multiple: true, required: false,
options: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
input "modes", "mode", title: "Only when mode is", multiple: true, required: false
}
}
def installed() {
log.debug âInstalled with settings: ${settings}â
initialize()
}
def updated() {
log.debug âUpdated with settings: ${settings}â
unsubscribe()
initialize()
}
def initialize() {
subscribe(presenceSensors, âpresenceâ, âpresenceHandlerâ)
subscribe(vCooler, âswitch.onâ, âonHandlerâ)
subscribe(vCooler, âswitch.offâ, âoffHandlerâ)
}
def presenceHandler(evt) {
if (vCooler.currentSwitch == âonâ) {
if (getPresence()) {
cooler.on()
} else {
cooler.off()
}
} else {
cooler.off()
}
}
def onHandler(evt) {
if (getPresence()) {
cooler.on()
} else {
cooler.off()
}
}
def offHandler(evt) {
cooler.off()
}
def getPresence(){
def presentCounter = 0
presenceSensors.each {
if (it.currentValue("presence") == "present") {
presentCounter++
}
}
if (presentCounter > 0) {
return true
} else {
return false
}
}
private getAllOk() {
modeOk && daysOk && timeOk
}
private getModeOk() {
def result = !modes || modes.contains(location.mode)
log.trace "modeOk = $result"
result
}
private getDaysOk() {
def result = true
if (days) {
def df = new java.text.SimpleDateFormat(âEEEEâ)
if (location.timeZone) {
df.setTimeZone(location.timeZone)
}
else {
df.setTimeZone(TimeZone.getTimeZone(âAmerica/New_Yorkâ))
}
def day = df.format(new Date())
result = days.contains(day)
}
log.trace "daysOk = $result"
result
}
private getTimeOk() {
def result = true
if (starting && ending) {
def currTime = now()
def start = timeToday(starting).time
def stop = timeToday(ending).time
result = start < stop ? currTime >= start && currTime <= stop : currTime <= stop || currTime >= start
}
log.trace "timeOk = $result"
result
}
private hhmm(time, fmt = âh:mm aâ)
{
def t = timeToday(time, location.timeZone)
def f = new java.text.SimpleDateFormat(fmt)
f.setTimeZone(location.timeZone ?: timeZone(time))
f.format(t)
}
private hideOptionsSection() {
(starting || ending || days || modes) ? false : true
}
private timeIntervalLabel() {
(starting && ending) ? hhmm(starting) + â-â + hhmm(ending, âh:mm a zâ) : ââ
}
If there are any coders out there that could have a look at this and fix it i would really appreciate it!! @MichaelS @slagle Or anyone else please feel free to join in.
Edit: Oh boy i really have this screwed up!!