openHAB Owntracks: Presence Detection Using GPS Location

 

Presence Detection is among the most challenging aspects of Home Automation and getting it right is often a combination of different methods. Today, I bring you a tutorial on the openHAB Owntrancks implementation.

Owntracks is yet another tool that you can use to detect human presence. On this tutorial, I will show you how to use it and how to combine it with other methods.

 

What is Owntracks?

Let’s start with the basics as usual…

Owntracks is a software that collects your location and reports it to a server. There are other applications that do that but Owntracks has three big selling points:

  • It is fully Open Source. You know exactly what the software is doing with your data.
  • The position can be reported to a private server, no cloud or third party company storing your information.
  • It uses standards protocols like MQTT to send the location.

These three features make Owntracks a great candidate to implement openHAB Presence Detection.

 

So…How Does it Work?

For Android, Owntracks runs in the background and relies on your phone to tell the app that you moved somewhere else.

It is important to notice that it doesn’t actively report your location, it relies on your phone to do that.

This approach benefits your battery life but it also means that Owntracks performs better in some devices than others.

I will only cover Android, for now, primarily becuase I don’t have an iPhone 🙂

Let’s get started detecting presence!

 

Pre-Requisites for Presence Detection with OwnTracks

There are a few pre-requisites to get openHAB and Owntracks working together but I am sure you already have most of them.

  • You need an MQTT Broker like Mosquitto on your Home Automation controller.
  • A Dynamic DNS is not required but it will make your life easier.
  • You need to open the 1883 port on your router and point it to the MQTT Server.

 

Configuring OwnTracks on your SmartPhone

The biggest advantage about Owntracks is its biggest disadvantage as well, it has way too many options…

I will walk you through the main settings and will suggest you an initial setup to start working things out.

 

Owntracks: Initial Setup

When you open the app for the first time, it will walk you through a series of steps. The configuration of the app can get pretty confusing but I will try to be as detailed as possible.

 

Owntracks Openhab Tutorial 1

 

The first life-changing decision that you have to make is where you want your data to be sent.

You can use a public MQTT server but this is strongly not recommended.

In the private mode, there are two options; MQTT and HTTP. We will be using MQTT here becuase it is the easiest one to integrate with openHAB.

 

Owntracks Openhab Tutorial 2

 

The last step of the setup is granting permissions for the application to grab your GPS location. This is a requirement for the app to work correctly so you are gonna have to say yes here.

 

Owntracks Openhab Tutorial 4

 

Owntracks Openhab Tutorial 5

 

Configuring the App

The message Done, Enjoy Owntracks is a bit too optimistic…there is still quite a few things to configure.

I will go over the different options and give you some recommendations but you might need to do some additional adjustments for your very own needs.

Once you enter into the preferences of the app, you will see 6 different options, we will focus primarily on the first 3 of them.

  • Connection: Details to connect to your MQTT server.
  • Reporting: How and what to report to the MQTT server.
  • Notifications: What do you want to be notified about?
  • Advanced: Some fancy parameters…I will tell you about them later.
  • Configuration Management: Ugly json file with the configuration that you can import and export. This is neat in case you want to export the same config into different phones.
  • Information: This is the about section of the app. Nothing interesting here.

Most of the sections are already configured with default values but I want you to know a bit about them so you can play around with the settings.

 

Owntracks Openhab Tutorial 5

 

MQTT Connectivity

This is kind of the only mandatory part, Owntracks needs the details of you MQTT broker to be able to publish messages.

Let’s get down to business.

  • Mode:  You already told owntracks that you wanted to use a Private MQTT broker. If you ever want to change that, you can always come back here and do it.
  • Host: This will be our Dynamic DNS, DuckDNS if you are using the recommended one. You will also have to specify the port, which, if you didn’t change it, should be 1883.
  • Identification: This section has 4 parameters:
    • Username: The username for the MQTT Server.
    • Password: Password for the MQTT server.
    • Device ID: Here you should specify the name you want your cell phone to be associated with. Normally the owner of the phone. This is relevant becuase it will be part of the MQTT topic name.
    • Tracker ID: Similar to the Device ID but in the form of a number. You can make this up, just make it unique.
  • Security: If you configured the MQTT Broker with my MQTT tutorial, you don’t need to worry about this.

 

Owntracks Openhab Tutorial 6

 

After filling out all the MQTT  details, open the left-hand side menu and check the status, there is no point in moving forward if the app can’t connect to the server.

Owntracks Openhab Tutorial 7

All good? Cool Let’et move forward then.

 

Reporting Options

This section allows you to chose how to report and what to report.

  • Automatic Reporting: The App will report your position automatically based on some configurations that I will explain later.
  • Extended Data: Every message will carry some additional information like the battery left. This info can be useful if you want to set some smart alerts.

 

Owntracks Openhab Tutorial 8

Notifications

Notifications aren’t very useful from an information point of view, however, Android devices generally clean apps running in the background to improve your battery life.

If you see that Android is killing the  Owntracks App often, keeping the notifications active might help.

 

Owntracks Openhab Tutorial 9

 

Advanced Preferences

This section gets a bit confusing but you can start with the default options and improve from there.

Remote Commands

This setting allows you to send MQTT messages so Owntracks can execute remote commands on your device.

This is not a requirement for presence detection and my preference is to have it deactivated. Let me know if you find a good use case for it though.

Accuracy

This parameter defines how much battery you want to drain in exchange for awesome precision in your location data.

You are going to have to do some trial and error here but I can tell you that I have it configured as High Powered and I haven’t had any issues with the battery duration.

Let me know how does this work out on your side.

Inaccurate Locations

Depends on how many satellites your phone is using to locate you, the position reported will have different accuracy.

Sending inaccurate locations to your home automation system will confuse it so it is best to specify a threshold.

I have mine set up to 20 meters and is giving me very good results.

Displacement and Interval

As I said at the beginning of the tutorial, Owntracks doesn’t send your location on a timely basis, it is a bit more complex than that.

In order to determine when to send a new location, it uses two parameters, displacement, and interval.

Let’s take the following setup as an example.

Interval: 10 Minutes.

Displacement: 100 Meters (300 Feet)

In this case, Owntracks will report a new location only if after 10 minutes you have moved more than 100 meters.

For example, if my Owntracks app reported a position and after 10 minutes I haven’t moved more than a 100 meters no new position will be reported.

On the other hand, if you moved more than 100 meters but only five minutes have passed, your position will be reported eventually but not before the next 5 minutes.

Start with the default setting and play around as you learn more about the app.

 

Owntracks Openhab Tutorial 10

Configuring Regions

Regions are a cool functionality within the Owntracks universe. They allow you to set up different locations like HOME, WORK, SUPERMARKET…Essentially any location you go to on a regular basis.

Once you have them, you can configure a geofence. Once you get into the area of one of your regions, Owntracks will generate an event.

Owntracks Openhab Tutorial 11

 

In order to add a region, just open the main menu on the left-hand side and hit Regions. Then click on the plus sign in the top right of the screen.

Owntracks Openhab Tutorial 12

 

Name your region and add the location, no worries, you don’t need to know your latitude and longitude by heart.

If you hit the scope (or whatever that symbol is) on the top-right side of the screen it will open Google Maps and will let you chose a location. By default, it uses your current position.

The next thing to specify is Radius which is the area you want to geofence.

Don’t worry about the next fields, they are used to configure Bluetooth beacons which are out of the scope for this tutorial.

The last parameter, Share,  is a bit counterintuitive, even if you don’t have activated, it will still share the position but it won’t include the region that you are getting in or out.

I don’t get why it was designed this way but that is what it is.

 

Owntracks Openhab Tutorial 13

 

 

Testing the Owntracks Setup

Before we get into the details of how to configure openHAB, it is good to do some debugging to make sure it works.

I like to use an app called MQTT.FX bur really any MQTT client will do just fine.

When you open MQTT.FX for the first time you will find something like this:

 

Owntracks Openhab Tutorial 14

 

Click on the gear to add a new connection and then on the + sign.

Owntracks Openhab Tutorial 15

Fill out the relevant details of your MQTT server, including username and password.

Owntracks Openhab Tutorial 16

 

Now that everything is configured, you should be able to subscribe to topics and start getting messages.

Owntracks Openhab Tutorial 17

 

By default, Owntracks publishes messages into two different topics.

Location Messages: GPS coordinates and some additional details about the device.

 

Owntracks Openhab Tutorial 18
Event Messages: Sent when a given user goes in or out a particular region that you have configured on the Owntracks app.
Owntracks Openhab Tutorial 19
If you are not sure how owntracks has named your topics, you can subscribe to “#” and it will sniff all the traffic on the MQTT broker.

openHAB Configuration

Install the OwnTracks Binding

Owntracks is fully supported by openHAB through the use of a binding. Let’s go ahead and install it.

  1. Open PaperUI and install the Owntracks binding

 

Owntracks Openhab Tutorial 18

 

This part was simple…Let’s get into the good stuff.

The first thing that we need to do is to configure the basic settings on the Owntracks configuration file.

The configuration file that you are after is called mqttitude.cfg and you will find it under:

 

Edit the following parameters on the file:

Needless to say that you have to replace {LATITUD} and {LONGITUD} by the coordinates of your home location. You should be able to find them using google maps on your phone or even just taking the info from the region you have configured in Owntracks.

You should also specify {RADIUS}, which is the area around your house that you want to geofence.

It is specified in meters by default so you may want to keep that in mind when picking a value.

 

openHAB Items

You have configured the binding but openHAB isn’t gathering the values yet.

The openHAB Owntracks configuration comes in two different flavors and you can use either of them or both.

 

Manual Mode

Manual mode only uses location messages and calculates the distance between the coordinates defined in your config file and the current coordinates sent by the Owntracks app.

The openHAB item using manual config would look like this:

 

Don’t forget to replace MQTT_USER and USERNAME by the ones you defined in the owntracks app.

This definition will create a switch item that will be ON when the {USERNAME} is within the radius specified on the mqttitude.cfg file.

 

Region Mode

The other mode is based on regions defined in the Owntracks App. During the tutorial, I defined a region called HOME which is the one I am going to be using now.

 

This definition will create a switch item that will be ON when the {USERNAME} is within the range of the region specified on the Owntracks app.

Using Groups to Implement Presence Detection

In order to make Presence detection more generic, it is good to use groups as opposed to addressing the individual items.  This will make your rules simpler and will help with the maintenance of the system.

Here you have an example of combining the two Owntracks modes into one switch called gPresence.

When either of the methods detects presence, the switch will turn ON.  In this way, you don’t need to address each item individually.

Groups

 

Items

 

Sitemap

 

Rules

Using a group to track presence will make your rules a lot simpler.

 

Conclusion

After using Owntracks extensively, the conclusion is that is not precise enough as the only presence detection method, however, in combination with other methods like the network binding, it can provide additional stability.

It doesn’t take long to set up and it is free so I think you should at least give it a try.

The integration with openHAB is quick and simple but in my case, the manual mode has provided much more accurate results.

If you want to get deeper into the Presence detection topic, you should check this talk from the smart home day back in 2017. It is really worth it.

So…What presence detection methods do you use?

This Post Has 2 Comments

  1. Wolfgang

    Can you please explain or make a tutorial how we can connect owntracks to an openHAB-system (raspi 3), who is accessible with VPN (as you explained us in your excellent How-to VPN Setup Guide Using PiVPN).
    Since openHAV v2.4.0 there is a new GPSTracker binding, which nicely integrates OwnTracks with openHAB.

    Many thanks

    Wolfgang

  2. David C.

    Hi Wolfgang,

    I am playing a bit of catch up with the latest release of openhab. There are quite a few changes in the way it integrates with mqtt and also the GPSTracker binding.

    I will see if I can put some time this weekend to update the posts that have been rendered obsolete by the latest update.

    Thanks for the suggestion!

Leave a Reply