I have been using this rule for detecting phones state:
rule "iPhone Home"
when
Item KristofIPhone_Location changed or
Item EdinaIPhone_Location changed or
Item ReKaIPhone_Location changed
then
val PointType home_location = new PointType(new DecimalType(47.5274), new DecimalType(19.1126))
val PointType phone_location = triggeringItem.state as PointType
val nameParts = triggeringItem.name.toString.split("_")
val triggeringiPhoneName = nameParts.get(0) + "_Home"
val int distance = phone_location.distanceFrom(home_location).intValue()
if(distance < 150) {
postUpdate(triggeringiPhoneName, "ON")
logInfo(nameParts.get(0), "iPhone is at home.")
} else {
postUpdate(triggeringiPhoneName, "OFF")
logInfo(nameParts.get(0), "iPhone is away.")
}
end
However it is not working well at all times. Sometimes phones loses precise location thus the rule says that the phone is not at home. (Because the precision is bigger than that constant number) I want to solve this something like including the Location Accuracy to make a dynamic radius for āHomeā. Like:
val DecimalType locationAccuracy = nameParts.get(0) + "_LocationAccuracy"
and in the head of the if:
if(distance < 150 + locationAccuracy)
But I get some errors like this:
2018-04-21 11:47:07.416 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'iPhone Home': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.lib.NumberExtensions.operator_plus(java.lang.Number,java.lang.Number) on instance: null
2018-04-22 13:34:25.444 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'iPhone Home': 'state' is not a member of 'com.google.common.collect.Iterables$6'; line 12, column 25, length 70
Thanks this seems to solve the issue I havenāt found a better solution for this on the forum so it can be also good for anyone else. All phones sometimes loses location so comparing it with a constant number is not the best ideaā¦
This is much better!
Below is how I ended up doing thisā¦ I am using the LastUpdate event to trigger the rule because Location gets updated before LocationAccuracy which can affect calculations of distance ā¦ I havenāt included the items or groups but I think the code is fairly readable without.
rule "Distance from home"
when
Member of giDeviceLastUpdate received update
then
Home_Location.state = new PointType(new DecimalType(44.925994), new DecimalType(-93.310812))
val nameParts = triggeringItem.name.toString.split("_")
val iDeviceName = nameParts.get(0)
val locationAccuracyName = nameParts.get(0) + "_LocationAccuracy"
val locationAccuracyItem = giDeviceLocationAccuracy.members.findFirst[ i | i.name == locationAccuracyName ]
val locationAccuracy = locationAccuracyItem.state as DecimalType
val locationName = iDeviceName + "_Location"
val locationItem = giDeviceLocation.members.findFirst[ i | i.name == locationName ]
val int distance = Home_Location.distanceFrom(locationItem)
val presenceiCloudName = iDeviceName + "_iCloud"
val iCloudItem = gPresent.members.findFirst[ i | i.name == presenceiCloudName ]
if (distance < 200 + locationAccuracy) {
iCloudItem.postUpdate (ON)
logInfo("iCloud Testing : ","Present "+iDeviceName)
}
else {
iCloudItem.postUpdate (OFF)
logInfo("iCloud Testing : ","Not present "+iDeviceName)
}
end
Thanks, thatās good to know. I havenāt had time to dig into it, but I can clearly see that it takes some time to realize that Iām homeā¦
I will try your method, thanks!
Iām thinking about the opposite way. If the LocationAccuracy is big, it shouldnāt update the Home or Away. Because sometimes a 6km Accuracy will end in updating the switch as āHomeā.
Why it shouldnāt update if the location accuracy is smaller than 50m? I usually get around 20m accuracy, which means it will never update the switch