Presence detection with location accuracy

Which one? :slight_smile:

Here is my latest version, which tracks the phones location and updates the Presence Item:

val String filename = "Icloud.rules"

rule "Phones Home or Not"
when
	Member of gPhoneLastLocationUpdate changed
then
	// Get items
	val nameParts = triggeringItem.name.toString.split("_")
	val triggeringiPhoneName = nameParts.get(0) + "_Home"
	val locationAccuracyString = nameParts.get(0) + "_LocationAccuracy"
	val locationName = nameParts.get(0) + "_Location"

	val locationItem = gPhoneLocation.members.findFirst[ i | i.name == locationName ]

	// Calculate home address and phone address
	val PointType home_location = new PointType(new DecimalType(47.5274), new DecimalType(19.1126))
	val PointType phone_location = locationItem.state as PointType

	// Filter the Location Accuracy item corresponding to the phone	
	val locationAccuracyItem = gAccuracy.members.filter[ i | i.name == locationAccuracyString ].head

	// Convert it
	val locationAccuracy = locationAccuracyItem.state as DecimalType
	
	// If the location accuracy is not accurate, skip updating the switch
	if(locationAccuracy as Number > 450) {
		logInfo(filename, nameParts.get(0) + " -> " + "Location inaccurate ({} m), skipping update", locationAccuracy)
		return
	}

	// Calculate the distance from home to phone
	val int distance = phone_location.distanceFrom(home_location).intValue()
	
	// Update the triggering phone's Home item
	if(distance < 150 + locationAccuracy) {
		postUpdate(triggeringiPhoneName, "ON")
		logInfo(filename, nameParts.get(0) + " is at home." + " ->" + " Distance: " + distance + "m  " + "Location Accuracy: " + locationAccuracy + " m")
	}

	else {
		postUpdate(triggeringiPhoneName, "OFF")
		logInfo(filename, nameParts.get(0) +" is away." + " ->" + " Distance: " + distance + "m  " + "Location Accuracy: " + locationAccuracy + " m")
	}
end

1 Like

Thanks, appreciated!!