Location Item and persistence

Hi @sipvoip,

I just recognized the same issue.
Havee you been able to find a solution (I am using mysql persistence).
Thanks in advance.

Nope :frowning:

I’m using MariaDB and jdbc-mariadb for persistence, and I ran into the same issue. I eventually just went back to using Strings for our location items. When I saw this post it reminded me of the last thing I wanted to try but haven’t yet… changing the data type in location items tables to GEOMETRY for the Value column. I doubt this will do it and that there is a problem with the binding (it creates location item tables with VARCHAR), but without digging into the code to look, it should be an easy thing to test. If anyone else tries this before I get to it, I’d recommend a fresh backup in case the VARCHAR <–> GEOMETRY conversion does not go well! Or has someone already tried this?

A Location item consists of two or optionally three float numbers. Did you ever consider simply persisting these parts? You have to remember, that persistence is only halve the job. What are you going to do with that data later? If you want to use it in another application, chances are high you will benefit from Decimal numbers in clearly addressable fields.

Did I miss anything?

The specific issue is restoring persistence data to a Location item.

Ah I see.

Not quite the same but maybe this goes into the right direction: Switch value changing to Null. Why!?

Nah… completely different issue. In my experience, the Location item data is persisted properly, but the restore on startup throws an error. (which I do not have handy).

Edit: found it in an old backup:

2017-07-24 03:09:58.913 [ERROR] [pse.smarthome.core.items.GenericItem] - Tried to set invalid state 41.226653,-81.722503 (StringType) on item Home_Hinckley of type LocationItem, ignoring it
2017-07-24 03:09:58.943 [ERROR] [pse.smarthome.core.items.GenericItem] - Tried to set invalid state 41.2266068,-81.7225042 (StringType) on item Lisa_Location of type LocationItem, ignoring it
2017-07-24 03:09:59.042 [ERROR] [pse.smarthome.core.items.GenericItem] - Tried to set invalid state 41.2267487,-81.7224206 (StringType) on item Scott_Location of type LocationItem, ignoring it

Depends. My opinion is that restoreOnStartup is hugely overrated and it’s more important to make sure the system reinitialized correctly.

Anyhow! Back to the issue, that is indeed awkward and you should open an Issue on GitHub for that. A LocationItem can be set by a StringType, as far as I remember.

I agree… it appears to be affecting multiple people. I opened this…

1 Like

Like @ThomDietrich suggested, I tried the way of separating the Location coordinates and it was actually much simpler than I expected:
Items:

Location 	Location_Home
Number		Location_Home_Lat		(G_Numbers)
Number		Location_Home_Lon		(G_Numbers) 

Initialization (startup.rules)

// set home location coordinates
	Location_Home_Lat.postUpdate(38.897676)
	Location_Home_Lon.postUpdate(-77.036626)

and calculation in geofencing.rules:

rule "Calc Distance of from Home"
when
	Item mqttLatitude changed or
	Item mqttLongitude changed
then
	val Loc_Home_tmp = new PointType(Location_Home_Lat.state as DecimalType, Location_Home_Lon.state as DecimalType)
	Location_Home.postUpdate(Loc_Home_tmp)
	val dist_Home_tmp = (Location_).distanceFrom(Location_Home)
	mqttDist.postUpdate(dist_Home_tmp.intValue)
end

Hi there,

I have the same issue now that I can’t store location items to my mySQL DB.
As Thom suggested I would like to separate the values to different items. Did somebody do that already so I might use the script? I use the iCloud binding for that so I get the values directly to the one item:

Location	VSiPhone_Location				"Viktor Koordinaten"									<apple>		(giCloud, giCloudLocation)	{channel="icloud:device:viktor:VSiPhone:location"}

Thanks,
Viktor

Look up one post… :wink:

I’ve recreated the issue in the correct repo…

Hi Scott,

Thanks for your reply. I’m not getting it :slight_smile:
The issue is open. Fine so far. But what I want is to extract the latitude, altitude and longtitude from the location item in other items so that I have them. Do I miss anything?

Apologies… I thought there was more to @NCO’s example! This may not help you, since I don’t split the lat and lon, but just put the location into a String Item (which does persist, and can be converted to a Location in rules), but this is my rule, using openhab2-jython modules and MQTT2 binding w/Owntracks…

import json
from core.rules import rule
from core.triggers import when

@rule("Light: Location changed")
@when("Item Scott_Location_Raw changed")
@when("Item Lisa_Location_Raw changed")
def locationChanged(event):
    if json.loads(str(event.itemState))['_type'] == "location":
        person = event.itemName.replace("_Location_Raw","")
        events.postUpdate("{}_Location".format(person),"{},{}".format(str(json.loads(str(event.itemState))['lat']),str(json.loads(str(event.itemState))['lon'])))
        events.postUpdate("{}_Accuracy".format(person),str(json.loads(str(event.itemState))['acc']))
        events.postUpdate("{}_Battery".format(person),str(json.loads(str(event.itemState))['batt']))
        #locationChanged.log.debug("Location changed: {}: {}".format(person,event.itemState)) 

And here is the same rule using the Rules DSL (requires the JSONPATH transform service)…

rule "Location: Parse location events"
when
    Item Scott_Location_Raw changed
    or
    Item Lisa_Location_Raw changed
then
    if (transform("JSONPATH","$._type",triggeringItem.state.toString) == "location") {
        val String person = triggeringItem.name.replace("_Location_Raw","")
        postUpdate(person + "_Location",(new PointType(transform("JSONPATH", "$.lat", triggeringItem.state.toString) + "," + transform("JSONPATH", "$.lon", triggeringItem.state.toString))).toString)
        postUpdate(person + "_Accuracy",transform("JSONPATH", "$.acc", triggeringItem.state.toString))
        postUpdate(person + "_Battery",transform("JSONPATH", "$.batt", triggeringItem.state.toString))
        //logDebug( "Rules","Presence: Location: {}: triggeringItem.state=[{}]",triggeringItem.name,triggeringItem.state)
    }
end

And some Items…

String      Scott_Location_Raw	            "Location: Raw (Scott) [%s]"		        <error>	    (gPresenceAndMode)	{ channel="mqtt:topic:owntracks:scottLocation" }
String      Scott_Location	                "Location (Scott) [%s]"		                <error>	    (gPresenceAndMode)
Number      Scott_Accuracy	                "Location: Accuracy (Scott) [%s]"		    <error>	    (gPresenceAndMode)
Number      Scott_Battery	                "Location: Battery (Scott) [%s]"		    <error>	    (gPresenceAndMode)
1 Like

Right, thanks.
I now created a new item and simply set it with a rule:

rule "Viktor iCloud Location String Item"
when
	Item VSiPhone_Location changed
then
	VSiPhone_Location_String.postUpdate(VSiPhone_Location.state)
end

It seems to work. Now I have all three values in one text item. I still don’t know if I need them separately. My plan is to create a kind of map where I see all the locations I was at…

Here is a good example/tutorial. I built something similar, but it gives you a trail of your locations for the last 24 hours (configurable), but haven’t made the time to post it.

Nice, thanks a lot!

1 Like

Any update on this i try to get rid of all workarounds in my system and want to persist my Location objects.

/mike

Hav someone been successful to persist LOCATION items?

/Mike

Until now only with rules:

Items:

Location GPSLocation “Location” <location> {channel=“gpstracker:tracker:xx:lastLocation”}
Number Latitude “Latitude”
Number Longitude “Longitude”

Rules:

rule “Split Coordinates”
when
Item GPSLocation received update
then
Latitude.postUpdate(GPSLocation.state.toString.split(’,’).get(0))
Longitude.postUpdate(GPSLocation.state.toString.split(’,’).get(1))
end

rule “System Start”
when
System started
then
val loctmp = new PointType(Latitude.state as DecimalType, Longitude.state as DecimalType)
GPSLocation.postUpdate(loctmp)
end

…get rid of all workarounds…
But most probably you have this already :slight_smile:

3 Likes