Sorry, for bringing this topic up again.
But the code always returns the whole JSON response instead of the transformed string.
Seems that transform("JSONPATH", "$.XXXXX", result)
does not work correctly
Perfect it work but only for one iPhone,
I copy the code for an 2nd Device, but for both devices google send my the same location name.
What have to be different in the code for the second device?
I am struggling with the OpenStreetMap reverse address lookup. When the rule is executed I get the following error:
2021-10-04 21:10:52.013 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'icloudChristian-1' failed: cannot invoke method public abstract java.lang.String org.openhab.core.items.Item.getName() on null in icloudChristian
javax.script.ScriptException: cannot invoke method public abstract java.lang.String org.openhab.core.items.Item.getName() on null in icloudChristian
Any help to solve this would be highly appreciated.
I use this rule (thanks Bredmich):
val Functions$Function1<GenericItem, String> lookupAddress= [ ChristianiPhone_Location |
// get lat lon from item
val PointType location = ChristianiPhone_Location.state as PointType
val String lat = location.latitude.toString
val String lon = location.longitude.toString
// build request
val String endpoint = 'https://nominatim.openstreetmap.org/reverse'
val String lang = 'da-DK'
val String query = 'lat=' + lat + '&lon=' + lon + '&accept-language=' + lang + '&format=json&addressdetails=1'
val String request = endpoint + '?' + query
// get result
val String result = sendHttpGetRequest(request)
// logInfo("lookupAddress", result)
var String road = transform("JSONPATH", "$.address.road", result)
var String house = transform("JSONPATH", "$.address.house_number", result)
var String city = transform("JSONPATH", "$.address.city", result)
var String town = transform("JSONPATH", "$.address.town", result)
var String country = transform("JSONPATH", "$.address.country", result)
var String hide = 'Danmark' // in result language; hide your home country
// Empty the Strings if they´re not part of the result and the full result was put into the Strings
if(road.startsWith("{"))
{
road = ''
}
if(house.startsWith("{"))
{
house = ''
}
if(town.startsWith("{"))
{
town = ''
}
if(city.startsWith("{"))
{
city = ''
}
if(country.startsWith("{"))
{
country = ''
}
// Check if city is empty and fill in town
if(city == '')
{
city = town
}
// Check if city is still empty and fill Unknown
if(city == '')
{
city = 'Unknown'
}
var String address = road + ' ' + house + ', ' + city
// Check if the address starts with comma and only use the city
if(address.startsWith(",") || address.startsWith(" ,"))
{
address = '' + city
}
if (country != hide) {
address = address + ', ' + country
}
return address
]
rule "ChristianHjemme"
when
Item ChristianiPhone_Location changed
then
// Definition von Zuhause und aktueller Standort
val PointType homeLocation = new PointType(new DecimalType(55.XXXXX), new DecimalType(12.XXXXX))
val PointType phoneLocation = ChristianiPhone_Location.state as PointType
val int distance = phoneLocation.distanceFrom(homeLocation).intValue()
// Remove _Location from the triggering item and add _Address
val String updateItem = triggeringItem.name.toString.split("_Location").get(0) + '_Address'
// Fill in the address into the item
postUpdate(updateItem, lookupAddress.apply(triggeringItem))
if(distance < 150)
{
iPChristian_Home.postUpdate(ON)
logDebug("ChristianHome", "Christian er hjemme")
}
else
{
iPChristian_Home.postUpdate(OFF)
logDebug("ChristianHome", "Christian er ude")
}
end