Code for events from App or physical switches?

Now that I have written an app to consider motion, temp and wind speed. I would also like to be able to override those conditions by turning the switch on with the app or switch.

So I realize this will be an “else if” statement in my app. But I don’t know the code to retrieve an “on” event from the app or actual switch? Or since the (evt) section is based on motion how can I accomplish this? Do I need another (evt) section for the app or switch command?

Any suggestions? Here is the (evt) part of my app I want to modify:

def motionHandler(evt) {	
    log.debug "$evt.name: $evt.value"
	def data = getWeatherFeature( "conditions", zipcode )
	log.debug( "Current windspeed in ${zipcode} is ${data.current_observation.wind_mph} mph")      
    if (evt.value == "active") {
		def lastTemp = sensor1.currentTemperature
		if (lastTemp >= temperature1){
			def lastWindspeed = data.current_observation.wind_mph
			if (lastWindspeed <= wind1){
				log.debug "turning on lights"
				switches.on()
				state.inactiveAt = null
                }
            }
		} 
    else if (evt.value == "inactive") {
		if (!state.inactiveAt) {
			state.inactiveAt = now()
		}
  	}
}

TIA,
Scott

You would need to subscribe to your physical device if you want to be notified of its status.

If you want to do something when a switch is turn on, then you would need to subscribe to the switch’s on event, something like:

def installed() {
    subscribe(switch1, "switch.on", switchOnHandler)
}

def switchOnHandler(evt) {
    // Do something based on the event
}

If you just want to check the current state of the switch, you can do this:

    if ( switch1.currentValue("switch") == "on" ) {
        // Do something if the switch is on
    }

@garymullen Thanks for the code. But what I really want may not be possible. I have an app that turns on a switch based on motion conditional on temp and wind. But what if I decide…forget all the conditions…I want the fan to just stay on and not turn off after a period of inactivity.
So I would like to use the App to send an On command and have this override the app or the conditions in the app. So I need to be able to see the On signal from the app? Is this possible? Is it possible to see where a command came from and not the status of the switch?
Thanks!
Scott

My thought…

Manually activate the switch and have your app subscribe to that. In that event, do something like: state.onForever = true

When your app tries to turns things on or off, check for state.onForever and only proceed if that variable is false.

Oh, I see what you’re trying to do. Within your switch on/off handles you can use the event method isPhysical() to test if it was a button was pushed, then set an override to true.

def switchOnHandler(evt) {
    if ( evt.isPhysical() ) {
        state.physicalOverride = true
    }
}
def switchOffHandler(evt) {
    if ( evt.isPhysical() ) {
        state.physicalOverride = false
    }
}

Then in your motionHandler just check the state.physicalOverride before turning the switches on/off.

Would that suite your needs?

@GaryMullen @Dianoga Thanks so much for your expertise! I will integrate your suggestions later today and post my findings. But this looks perfect to handle On/Off from the physical switch.

Is it possible to detect the On signal sent from the Android App? So if I turn on the switch with the app the switch stays on like your suggestion.

Thanks again,
Scott <— not a developer (EE) but learning quickly with cut/paste/trial&error. Fortran 77 is not very helpful now.

p.s. Dumb question but all my apps have been based on a single (evt). Can (evt) blocks be nested? Or do they each need to close and process in sequence?

The event object has a source attribute which is supposed to contain one of APP, APP_COMMAND, COMMAND, DEVICE, HUB, or LOCATION, but in my limited testing is only ever contains DEVICE. I’m going to email support about it. However, I think this is what you would use.

I’m not quite sure what you mean about the nesting of event handlers. They’re just code blocks, so one could call another, if that’s what you mean?