Create GPX Tracks with openHAB2 presence detection

You have a GPS presence detection in your openHAB instance configured? So why not using your data and create your privately stored GPX tracks every day?

I wrote the python script dbtracks.py which can be used to create GPX tracks from your database data. The script uses SQL commands and should work with all SQL databases but is currently only tested with my MariaDB database. You can find this script in GitHub here: openHABdbTools.

You can copy this script to /etc/openhab2/scripts and give him execution rights with „chmod +x *.py“.

This description assumes that you have an GPS tracking running on your openHAB and smartphone like described here.

First you need to know that openHAB2 does not store items of type location in the persistence. We need to implement a little work around for this we use a rule which is called on every location change. Than we convert the location to a string item. This string need to be stored now on every change.

Let us assume you have the following items configured for your GTSTracker binding:

// Olivers Location:
Location	locationOliver	"Olivers Location [%2$s°N %3$s°E %1$sm]" {channel="gpstracker:tracker:ol:lastLocation"}

// Olivers Location as string for persitence storage:
String	locationOliverString "Location Oliver as String "

// Accuracy of the current position:
Number:Length accuracyOliver	"GPS Accuracy Oliver [%d m]"		{channel="gpstracker:tracker:ol:gpsAccuracy"}

Than we can implement the following rule, which stores the location as the string item locationOliverString in your persistence.

var String lastLocationOliver = "0,0"
val Number AccuracyThreshold = 100

/* store location in string for later processing */
rule "Store location"
when
Item locationOliver received update
then
	var Number accuracy = (accuracyOliver.state as QuantityType<Number>).doubleValue

	// Store only different values with a good accuracy!
	if( (lastLocationOliver != locationOliver.state.toString) && (accuracy < AccuracyThreshold) ) {
		locationOliverString.postUpdate(locationOliver.state.toString)
		lastLocationOliver = locationOliver.state.toString
	}
end

This rule checks also the accuracy of the position and stores only positions under a given AccuracyThreshold in meters and if there is a location change compared to the lastLocationOliver.

Now is time to take a break and walk around the block. Take your smart phone with the OwnTracks App running to store some locations! Set the OwnTracks App to a mode where it tracks you with high precision to get better results.

Check your tracking mode!

Now you need to setup your tracks configuration in the dbtools.ini file:

[dbconfig]
#host address of the db server:
host=localhost      	
#openHAB table:
table=openhab   
#openHAB data base user:
user=openhab	
#openHAB data base password
password=openhab		

# You can add more persons to track with sections [dailyTrack1] - [dailyTrack9]
[dailyTrack1]
#path where the tracks are stored
path=/etc/openhab2/data/Tracks/
#item to create daily track (must be of type string)
item=locationOliverString
#name for this tracks. Used in GPX filenames.
name=Oliver

configure your host, table name, user and password for your persistence. Then you can configure up to 9 different location items to tracks. You need to configure them in the section [dailyTrack1]…[dailyTrack9].

Here we have on track configured which uses the string item locationOliverString. name= is used for the created file name only. The tracks will be stored in the configured path=. The script will create a directory structure there like this:

directory structure with year and month

If you call the script dbtracks.py now you will notice that no file is created! It will only create Tracks of the last days. That means you need to wait until the next day to see something.

You can use the cron job rule which comes this the openHABdbTools to get every day at 6am your tracks created:

rule "dbtools every day job"
when 
  	Time cron "0 0 6 * * ?"   // Every day 6:00 hours
then
    var String result = executeCommandLine("/etc/openhab2/scripts/dbsize.py",10000)
    logInfo( "DbTools daily", result)

    result = executeCommandLine("/etc/openhab2/scripts/dbtracks.py",10000)
    if(result != "") {
	    logInfo( "DbTools daily", result)
    }
    result = executeCommandLine("/etc/openhab2/scripts/dbtimescheet.py",10000)
    if (result != "") {
	    logInfo( "DbTools daily", result)
    }
end

Load your tracks to for example to google earth:

GPX track in google earth professional

Would be nice to get feedback if the script is also working for your installation and which persistence configuration you use.