Group wise distance calculation for variable locations in a group (.distancefrom on String)

Raspi 4 running openHAB 2.5.10-1 (Release Build)

All,

I am using succesfully distance from for geofencing:

rule "Geo Distance calculation"
when
    Member of G_Loc changed
then
	val itemName = triggeringItem.name.toString
	val itemState = triggeringItem.state.toString
	val String who = itemName.replaceAll("_Loc", "")

 // measure Car distances
	if(itemName.contains("Car")) {
		val dist_Home_tmp = (Car_Loc).distanceFrom(Loc_Home)
		val dist_Gar_tmp = (Car_Loc).distanceFrom(Loc_Car_Garage)
		val dist_NCO_tmp = (Car_Loc).distanceFrom(NCO_Loc)
		val dist_DNA_tmp = (Car_Loc).distanceFrom(DNA_Loc)
		CarDist.postUpdate(dist_Home_tmp.intValue)
		CarDistGar.postUpdate(dist_Gar_tmp.intValue)
		CarDistNCO.postUpdate(dist_NCO_tmp.intValue)
		CarDistDNA.postUpdate(dist_DNA_tmp.intValue)
	}
...

This kind of calculation is done for each Family Member (and the car above), but I would like to simplify this by using auxiliary variables, by adding:

val String who = itemName.replaceAll("_Loc", "")
val String whoDH = who + "_Dist" // distance (item name) home for who
val String distH = (itemState).distanceFrom(Loc_Home) // this does not work
whoDH.postUpdate(distH.intValue)

The problem is this part.
val String distH = (itemState).distanceFrom(Loc_Home)
returning:
Rule 'Geo Distance calculation': 'distanceFrom' is not a member of 'java.lang.String'; line 63, column 21, length 34

So is there an option to use the placeholder "itemState " (which is a string representation of the geo location)?

By the way:
Using this does not work either:
val Location whoLoc = triggeringItem.state.toString as Location

leads to
Rule 'Geo Distance calculation': Could not cast 38.897403745917856, -77.03739319460877,0.0 to void; line 12, column 24, length 41

Finally found something - I will try…

The other case does not help, because I don’t have the real Location item but its representation as a string.

So how to proceed with this?

EDIT:
This does not work either:

val loc = new PointType(lat + "," + lon)
val distH = (loc).distanceFrom(Loc_Home)

Where Loc_Home is a Location item and loc is created of lat / lon - both Strings
I get:
An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.core.library.types.PointType.distanceFrom(org.eclipse.smarthome.core.library.types.PointType) on instance: 38.897403745917856,-77.03739319460877

So I am wondering, why distanceFrom does not work with PointType?
What’s the difference of PointType and Location?

I checked a lot of threads and this one supposed to be working:
_distance = _position.distanceFrom(ItemPatrikLocation.state).intValue

(got it from here for instance:)

However, I get also:
Could not invoke method: org.eclipse.smarthome.core.library.types.PointType.distanceFrom(org.eclipse.smarthome.core.library.types.PointType) on instance:

I finally found a working solution:

val PointType loc = triggeringItem.state as PointType // for .distanceFrom either both need to be PointType or Location - I guess and don't know, what's the difference between them.
val PointType home = Loc_Home.state as PointType
val String whoDH = who + "_Dist" // distance (item name) from home for who
val whoDHitem = ScriptServiceUtil.getItemRegistry.getItem(whoDH) // distance (real item itself) from home for who
// who's distance from home
val distH = (loc.distanceFrom(home))
postUpdate(whoDHitem, distH)

Obviously for distanceFrom both must be either Location type items or PointType items.
If there is someone (maybe @rlkoshak) :wink: , who knows why and what’s the difference, this would be a good addon to this thread for future documentation.

I don’t do much with location so I don’t have any ideas what’s different between them.

Surprising, that I found something you don’t know about :wink:
Thanks for your quick response.

thanks a lot it work fine for me too !:slight_smile: