Help with Recent Presence Change


(Jon) #1

Below is the code I am using to notify myself of a presence change event. However, if multiple presence sensors are selected, i get notifications for both being either away or present. For example, when I left this morning, a notification came through for my phone leaving AND for my smartsensor being away which it was already away, I would like just what changed or triggered the event to occur.

How can I correctly code the notification for what caused the change?

A little background on how it is supposed to function - a presence change event occurs (notification) example to come home - then it goes and sees if anyone else is home - if someone is already home, then do nothing, if not then perform actions.


def presence(evt) {
for (person in people) {
if (evt.value == “present”) {
if (person.currentPresence == “present”) {
/////not perfect would send push telling you who all is home when you get home.
log.debug “${person.label ?: person.name} has arrived at the ${location}“
sendPush(”${person.label ?: person.name} has arrived at the ${location}”)
} else {
if (person.currentPresence == “not present”) {
log.debug “${person.label ?: person.name} is not at ${location}”
}
}
}

   	if (evt.value == "not present") {
		if (person.currentPresence == "not present") {
        	/////not perfect would send push telling you who all is home when you get home.
			log.debug "${person.label ?: person.name} has left the ${location}"
			sendPush("${person.label ?: person.name} has left the ${location}")
		} else {
    		if (person.currentPresence == "present") {
            log.debug "${person.label ?: person.name} is at ${location}"
        	}
		}
    }

}
///sendPush("${evt.name}: ${evt.value} Current mode ${location.mode}")
presencecheck()
}

PS this forum has changed ALOT since I was here last, glad to see the community growing, but damn what a confusing layout to a fourm!


(Matt Nohr) #2

First, here is a slightly better formatted version:

def presence(evt) {
	for(person in people) {
		if(evt.value == "present") {
			if(person.currentPresence == "present") {
				/////not perfect would send push telling you who all is home when you get home.
				log.debug "${person.label ?: person.name} has arrived at the ${location}"
				sendPush("${person.label ?: person.name} has arrived at the ${location}")
			} else {
				if(person.currentPresence == "not present") {
					log.debug "${person.label ?: person.name} is not at ${location}"
				}
			}
		}

		if(evt.value == "not present") {
			if(person.currentPresence == "not present") {
				/////not perfect would send push telling you who all is home when you get home.
				log.debug "${person.label ?: person.name} has left the ${location}"
				sendPush("${person.label ?: person.name} has left the ${location}")
			} else {
				if(person.currentPresence == "present") {
					log.debug "${person.label ?: person.name} is at ${location}"
				}
			}
		}
	}
	///sendPush("${evt.name}: ${evt.value} Current mode ${location.mode}")
	presencecheck()
}

The problem seems to be with the looping you are using. First, you loop all the presence sensors: for(person in people). Inside of that loop you are checking if the person.currentPresence matches the event value. However, you never check if the person (presence sensor) is the device that triggered the event. Because of that, you are sending a notification for every presence sensor that is in the same state as the event.

I think you should be doing something along these lines:

if(evt.deviceId == person.id && person.currentPresence == "not present") {
    // log.debug ...
}

Let me know how that works


(Jon) #3

Thanks! this is great it seems to work, but I will test for a bit just to be sure. I was not aware of the deviceId parameter.

Also going to try to use this to notify me when motion is detected on one of my selected motion sensors while away.