Turn a light on when I enter a room, but don't turn it back on if I stay and decide I want it off

Not strange, you have required: false for those inputs. Nothing says you have to require inputs, unless your app absolutely must have one in order to function. Same with number input. If you don’t enter a number, the corresponding variable will be null. Same with any omitted input. You can find out if you have an optional input this way:

if(switches) {  [some code]  }

because switches will be null and the if fail if you haven’t specified that input. Obviously, any code that wanted to work on switches cannot do so if it isn’t there.

As to wanting to require one OR the other input: You could test for it using a variant of the above check on one input. But what do you do if it fails? You are the one installing the app, so it shouldn’t matter, you know what to do. No value in making it idiot proof unless you are the idiot. :grinning:

I said strange… because you get an error installing the app, and I don’t. I’ve left them empty to idiot test the app… and I get no error.

I am using if(switches) & if(dimSwitches) to make sure that the code that would fire an error doesn’t run in the event it is left blank. This is needed for the current layout that am using as I might add a dimmer, and not a switch or vice versa.

App has been running fine… I have a few instances running and they seem flawless.

I have noticed some errors in the logs though, and I am not sure what they are from. Any ideas what would be triggering these:

error: com.netflix.hystrix.exception.HystrixRuntimeException: C*-IsaState-Update short-circuited and fallback disabled.

error: physicalgraph.exception.UncheckedException: com.netflix.astyanax.connectionpool.exceptions.NoAvailableHostsException: NoAvailableHostsException: [host=None(0.0.0.0):0, latency=0(0), attempts=0]No hosts to borrow from

I have two instances of this code running in once room on different switches, and the errors only appear in one of the instances… if that lends to figuring out why this is happening.

Its definitely happening directly after motionActiveHandler runs else… so I don’t know what could be causing an error since all I do is log.debug in that section.

Here is the current code I am running:

preferences {
section(“When there is movement here:”) {
input “motion1”, “capability.motionSensor”, title: “Where?”, multiple: true
}
section(“Turn ON These Dimmers”){
input “Dimswitches”, “capability.switchLevel”, multiple: true, required: false
input “BrightLevel”, “number”, title: “Dimmer Level %1-99 (OPTIONAL) Zero for no dimming”, required: false, defaultValue: “0”
}
section(“Turn ON Switches”){
input “switches”, “capability.switch”, multiple: true, required: false
}
section(“Unless there has been movement within the past”){
input “minutes1”, “number”, title: “Minutes?”
}
section(“And it is dark (OPTIONAL)”) {
input “LightMeter”, “capability.illuminanceMeasurement”, title: “Where?”, required: false
input “luminanceLevel”, “number”, title: “1-1000”, defaultValue: “250”, required: false
}
}

def installed() {
subscribe(motion1, “motion.active”, motionActiveHandler)
subscribe(motion1, “motion.inactive”, motionInactiveHandler)
subscribe(LightMeter, “illuminance”, handleLuxChange)
initialize()

}

def updated() {
unsubscribe()
subscribe(motion1, “motion.active”, motionActiveHandler)
subscribe(motion1, “motion.inactive”, motionInactiveHandler)
subscribe(LightMeter, “illuminance”, handleLuxChange)
initialize()

}

def initialize() {
ResetClock()
ActivityClock()
state.BrightLevel = BrightLevel as Integer
if (state.BrightLevel >= 100) {
state.BrightLevel = 99
}
if (state.BrightLevel <= 0) {
state.BrightLevel = 0
}
state.luminanceLevel = luminanceLevel as Integer
if (state.luminanceLevel <= 0) {
state.luminanceLevel = 0
}
if (state.luminanceLevel >= 1000) {
state.luminanceLevel = 1000
}
log.info “Brightness: $state.BrightLevel Luminance: $state.luminanceLevel”
}

def ActivityClock() {
//log.debug “ActivityClock”
state.threshold = 1000 * 60 * minutes1
state.elapsed = now() - state.motionEvent
//state.threshold = minutes1
//state.elapsed = (now() - state.motionEvent) / 60000
state.elapsedMinutes = (now() - state.motionEvent) / 60000
state.roundElapsed = Math.round(state.elapsedMinutes)
//log.debug “ActivityClock: $state.threshold ms Threshold is $minutes1 Minute(s)”
//log.debug “ActivityClock: $state.elapsed ms Elapsed”
log.info “ActivityClock: $state.roundElapsed Minutes Elapsed: Threshold is $minutes1 Minute(s)”
}
def ResetClock() {
state.motionEvent = now()
log.trace “ResetClock”
}

def motionActiveHandler(evt) {
log.info “$evt.name: $evt.value”
ActivityClock()
if (state.elapsed >= state.threshold) {
log.debug “motionActiveHandler: $state.elapsedMinutes Minutes >= $minutes1 Minute(s) Check Light Sensor”
checkLuminance()
//ResetClock()
} else {
//ResetClock()
log.debug “motionActiveHandler: Not enough time has elapsed, do nothing”
}
}

def motionInactiveHandler(evt) {
log.info “$evt.name: $evt.value”
ResetClock()
//ActivityClock()
}

def handleLuxChange(evt){
if (LightMeter){
def lightSensorState = LightMeter.currentIlluminance
log.info “handleLuxChange: SENSOR = $lightSensorState”
} else {
log.debug “handleLuxChange: SENSOR = No Light Meter”
}
}

def checkLuminance() {
log.debug “checkLuminance”
if (LightMeter){
def lightSensorState = LightMeter.currentIlluminance
log.debug “checkLuminance: SENSOR = $lightSensorState”
if (lightSensorState != null && lightSensorState <= state.luminanceLevel) {
log.debug “checkLuminance: SENSOR = $lightSensorState is <= $state.luminanceLevel: Turn On Lights”
TurnOnLights()
} else {
log.debug “checkLuminance: SENSOR = $lightSensorState is >= $state.luminanceLevel: Do Not Turn On Lights”
}
} else {
log.debug “checkLuminance: SENSOR = No Light Meter: TURN ON LIGHTS”
TurnOnLights()
}
}

def TurnOnLights() {
log.debug"TurnOnLights"
if (switches) {
log.trace “TurnOnLights: Switches ON”
switches.on()
}
if (Dimswitches) {
if (BrightLevel) {
log.trace “TurnOnLights: User set Dim Level $BrightLevel set to Light ON at $state.BrightLevel %”
Dimswitches?.setLevel(state.BrightLevel)
} else {
log.trace “TurnOnLights: Not set to Dimmer Level: Dimmer Light ON”
Dimswitches.on()
}
}
}