How do I get my smartApp to act differently based on presence?

Hi all! This is my first SmartApp and I hope I haven’t missed an answer to this question somewhere else in the forum.

Basically, I’m trying to get an app to switch on different lights on arrival depending on wether someone is home or not (When no one is home, switch home all main hallway lights, but when someone is home and someone else arrives, only the entrance lights should switch one.)

The lights should all close when I leave.

Also, this should only happen in certain modes.

I’ve looked at How to handle multiple presence(s) and although I found some good ideas, I wasn’t able to make it work. The problem I have is that once presence event gets detected. It’s to late to simply check wether someone is home or not.

I’m thinking I could run a people.findAll(it.currentPresence == “present”) and check wether that returns more than one result… is that possible? how would I go about doing that?

Here’s my code so far:

definition(
    name: "Smart Welcome Light",
    namespace: "gustavnadeau",
    author: "Gustav Nadeau",
    description: "Turns lights on when you arrive but selects lights depending wether someone is already present or not.",
    category: "Convenience",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_presence-outlet.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_presence-outlet@2x.png"
)

preferences {
	section("When I arrive and leave..."){
		input "people", "capability.presenceSensor", title: "Who?", multiple: true
	}
	section("Turn on/off lights when no one is home..."){
		input "switchAlone", "capability.switch", multiple: true
	}
    section("Turn on/off lights when someone is home..."){
        input "switchNotAlone", "capability.switch", multiple: true
    }
}

def installed()
{
	subscribe(people, "presence", presenceHandler)
    state.isHome = false
}

def updated()
{
	unsubscribe()
	subscribe(people, "presence", presenceHandler)
}

def presenceHandler(evt)
{
	log.debug "presenceHandler $evt.name: $evt.value"
	def current = people.currentValue("presence")
	log.debug current
	def presenceValue = people.find{it.currentPresence == "present"}
	log.debug presenceValue
	if(presenceValue && "$evt.value" == "present"){
		if(state.isHome){
            switchNotAlone.on()
            log.debug "Someone else has arrived!"
        }
        else{
            switchAlone.on()
            state.isHome = true
		    log.debug "Someone's home!"
        }
        
	}
	else if(!presenceValue) {
		switchAlone.off()
		log.debug "Everyone's away."
        state.isHome = false
	}
}
1 Like

Well, this did the trick

private someoneHome(){

        def whoIsHome = people.findAll{it.currentPresence == "present"}
        if(whoIsHome.size() > 1){
        	return true
        }
        
        return false
    }
3 Likes

you get bazillion internet points for actually posting the answer!

1 Like