# Owntracks setup to work with OH

**URL:** <https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730>\
**Category:** Bindings\
**Tags:** owntracks, mqtt, setup\
**Created:** [November 28, 2017, 2:15pm UTC](https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730 "2017-11-28T14:15:42Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![RiotMode](https://community.openhab.org/user_avatar/community.openhab.org/riotmode/32/80291_2.png) [@RiotMode](https://community.openhab.org/u/RiotMode)\
**Post date:** [November 28, 2017, 2:15pm UTC](https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730/1 "2017-11-28T14:15:42Z")

</div>

Hi Guys,

i tried to find an answer but wasn’t successful. never used owntracks before, just for info…  
my **mqtt server is working** , because is can use my sonoff switch via mqtt without any problem  
(my mqtt broker is on the raspb. with openhabian)

my owntracks settings on the iphone:  
Status: **connected**  
Parameters: **mqtt://xx.xx.xx.xx:1883 c0 k60 as riotmodeiPhone**  
Authentication: OFF  
Websockets & TLS: OFF

i subscribe with MQTT.fx to  
**owntracks/riotmode/iPhone**  
**owntracks/#**

both subs get infos

 ![36](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/a/1/a1195125236e6d50f11dc1c9659afef99f99743c.png)

also when i hit “publish settings” / “publish waypoints” on the phone app i get some infos in mqtt.fx

my items:

```auto
Switch PresencePhilipp "Philipp @ Home" { mqttitude="broker:owntracks/riotmode/iPhone/" }
Switch PresencePhilipp_OwnTracks "Philipp @ Home" { mqttitude="broker:owntracks/riotmode/iPhone/event:home" }

```

my sitemap:

```auto
Switch item=PresencePhilipp
Switch item=PresencePhilipp_OwnTracks

```

why is the switch not working ? do i really need to go for a walk ?

i have seen that there is also a BATTERY STATUS to grab, how could i get this info into OH ?

 ![02](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/d/a/da820e2a37d07727ffcd4537552d7cc3ea5bf71c.png)

Thank you!

---

<div class="post-metadata">

**Author:** ![Confused](https://community.openhab.org/user_avatar/community.openhab.org/confused/32/72636_2.png) [@Confused](https://community.openhab.org/u/Confused)\
**Post date:** [November 28, 2017, 2:25pm UTC](https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730/2 "2017-11-28T14:25:28Z")

</div>

I remember reading somewhere that you shouldn’t use “home” as a region name (annoying, I know!)

You must also Share the Region so that the name is passed through in the message to MQTT so that the OwnTracks binding will work correctly.

The ‘Event’ occurs when you leave or enter a region only - so yes, you’ll probably need to go for a walk!

You can pull the battery status using the JSONPath Transformation.

---

<div class="post-metadata">

**Author:** ![RiotMode](https://community.openhab.org/user_avatar/community.openhab.org/riotmode/32/80291_2.png) [@RiotMode](https://community.openhab.org/u/RiotMode)\
**Post date:** [November 28, 2017, 2:30pm UTC](https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730/3 "2017-11-28T14:30:03Z")

</div>

i only have **one region defined** and the name is the name of the village eg. “newyork” - so **no “home”** in regions on the iphone app.

my mqttitude.cfg for “ **home** ”:

```auto
home.lat=XX.XXXX
home.lon=XX.XXXX
geofence=100

```

the **region is shared** on the app!

after i publish the waypoint, it looks like this:

```auto
{
  "_type" : "waypoints",
  "waypoints" : [ {
    "_type" : "waypoint",
    "tst" : 1511879063,
    "lat" : XX.XXXXX,
    "lon" : XX.XXXXX,
    "rad" : 0,
    "desc" : "XXXXXX"
  } ]
}

```

> [@Confused](#):
>
> You can pull the battery status using the JSONPath Transformation.

i have never done that - could you help me with some code here ?

Thanks!

---

<div class="post-metadata">

**Author:** ![Confused](https://community.openhab.org/user_avatar/community.openhab.org/confused/32/72636_2.png) [@Confused](https://community.openhab.org/u/Confused)\
**Post date:** [November 28, 2017, 2:42pm UTC](https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730/4 "2017-11-28T14:42:57Z")

</div>

I haven’t defined a geofence within the mqttitude.cfg file, I am relying only on the app publishing the Event - in this case, you should use the Region name at the end of the Item, where you have ‘home’

```auto
Switch PresenceGarry "Garry"	<present> (gPresent) { mqttitude="mosquitto:owntracks/garry/p9/event:Beech" }

```

JSONPath could work something like this (my OwnTracks isn’t wanting to send a message to my MQTT server, so I can’t confirm it)

```auto
Number Battery "Philipp @ Home" { mqttitude="broker:owntracks/riotmode/iPhone:state:JSONPATH($.batt)" }
```

---

<div class="post-metadata">

**Author:** ![RiotMode](https://community.openhab.org/user_avatar/community.openhab.org/riotmode/32/80291_2.png) [@RiotMode](https://community.openhab.org/u/RiotMode)\
**Post date:** [November 28, 2017, 2:48pm UTC](https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730/5 "2017-11-28T14:48:00Z")

</div>

> [@Confused](#):
>
> you should use the Region name at the end of the Item

i understand and tried that, without luck - but i didn’t go out for a walk so far 😃

> [@Confused](#):
>
> JSONPath could work something like this

i didn’t work ☹  
( i have “JSONPATH Transformation” installed with PaperUI )

my test items:

```auto
Switch PresencePhilipp "Philipp @ Home" { mqttitude="broker:owntracks/riotmode/iPhone/" }
Switch PresencePhilipp_OwnTracks "Philipp @ Home" { mqttitude="broker:owntracks/riotmode/iPhone/event:home" }
Switch PresencePhilipp2_OwnTracks "Philipp @ Home" { mqttitude="broker:owntracks/riotmode/iPhone/event:XXXXXXXX" }
Number PresencePhilipp2 "Philipp @ Home" { mqttitude="broker:owntracks/riotmode/iPhone:state:default:" }
Number OwnTracks_Battery "Philipp @ Battery" { mqttitude="broker:owntracks/riotmode/iPhone:state:JSONPATH($.batt)" }

```

---

<div class="post-metadata">

**Author:** ![Confused](https://community.openhab.org/user_avatar/community.openhab.org/confused/32/72636_2.png) [@Confused](https://community.openhab.org/u/Confused)\
**Post date:** [November 28, 2017, 2:57pm UTC](https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730/6 "2017-11-28T14:57:19Z")

</div>

I think I’ve sussed the battery part!

```auto
Number OwnTracks_Battery "Philipp @ Battery [%.0f%%]" <battery> { mqtt="<[broker:owntracks/riotmode/iPhone:state:JSONPATH($.batt)]" }

```

---

<div class="post-metadata">

**Author:** ![RiotMode](https://community.openhab.org/user_avatar/community.openhab.org/riotmode/32/80291_2.png) [@RiotMode](https://community.openhab.org/u/RiotMode)\
**Post date:** [November 28, 2017, 2:58pm UTC](https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730/7 "2017-11-28T14:58:57Z")

</div>

> [@Confused](#):
>
> the battery part!

yes the battery part is working now 😍

but what to do with the Presence Switch - i was out for a walk and the switch is still OFF

can it be done in local network, or do i need CLOUD MQTT for that ?  
my mqtt broker is on the raspb. with openhabian 😕

---

<div class="post-metadata">

**Author:** ![Confused](https://community.openhab.org/user_avatar/community.openhab.org/confused/32/72636_2.png) [@Confused](https://community.openhab.org/u/Confused)\
**Post date:** [November 28, 2017, 5:49pm UTC](https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730/8 "2017-11-28T17:49:56Z")

</div>

OwnTracks will need access to the MQTT server in order to publish the events/location so that OH knows what’s going on!

Whether this is via a 3rd party such as CloudMQTT, or whether you open up your local MQTT broker to the internet, is your choice.

If you chose the second option, please be wary of the security implications of doing so!

---

<div class="post-metadata">

**Author:** ![ben\_jones12](https://community.openhab.org/user_avatar/community.openhab.org/ben_jones12/32/52_2.png) [@ben\_jones12](https://community.openhab.org/u/ben_jones12)\
**Post date:** [November 28, 2017, 8:15pm UTC](https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730/9 "2017-11-28T20:15:36Z")

</div>

Nothing wrong with using `home` as a region name - that is what I use.

---

<div class="post-metadata">

**Author:** ![RiotMode](https://community.openhab.org/user_avatar/community.openhab.org/riotmode/32/80291_2.png) [@RiotMode](https://community.openhab.org/u/RiotMode)\
**Post date:** [November 28, 2017, 8:26pm UTC](https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730/10 "2017-11-28T20:26:35Z")

</div>

okay thank for the clarification @Confused

i found this “tutorial” on [github](https://github.com/openhab/openhab1-addons/wiki/GoogleMap) and got this so far:

 ![08](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/6/6/66789a3d6cf401fa3e1b23e1b5cb6c5f1d95282e.png)

but i needed to make some little adjustments in the rule - i changed:  
(new DecimalType(acc)) to (acc)  
(new PercentType(batt)) to (batt)

my rule now:

```javascript
import org.openhab.core.library.types.*

rule "Mqtt Postion Parse Philipp"
  when
    Item mqttPositionPhilippRaw received update
  then
    val String json = (mqttPositionPhilippRaw.state as StringType).toString

    val String type = transform("JSONPATH", "$._type", json)
    if (type == "location") {
      val String lat = transform("JSONPATH", "$.lat", json)
      val String lon = transform("JSONPATH", "$.lon", json)
      val String acc = transform("JSONPATH", "$.acc", json)
      val String batt = transform("JSONPATH", "$.batt", json)

      mqttPhilippLatitude.postUpdate(lat)
      mqttPhilippLongitude.postUpdate(lon)
      locationPhilipp.postUpdate(new PointType(lat + "," + lon))
      mqttPhilippAccuracy.postUpdate(acc)
      mqttPhilippBattery.postUpdate(batt)
      logInfo("ownTRACKS=", +mqttPositionPhilippRaw.state)
    }
  end

```

on github patrik had this changes:

```javascript
mqttPatikAccuracy.postUpdate(new DecimalType(acc))
mqttBattery.postUpdate(new PercentType(batt))

```

and i get this in the log:

```php
[INFO] [el.core.internal.ModelRepositoryImpl] - Loading model 'owntracks.rules'
==> /var/log/openhab2/events.log <==
2017-11-28 21:15:30.027 [ItemStateChangedEvent] - mqttPositionPhilippRaw changed from {"batt":58,"lon":XX.XXXXXXXXXX,"acc":165,"vac":10,"lat":XX.XXXXXXXXXX,"t":"u","_cp":1,"conn":"w","tst":1511900024,"alt":366,"_type":"location","tid":"PL"} to 
2017-11-28 21:15:30.870 [ItemStateChangedEvent] - mqttPhilippLatitude changed from XX.XXXXXXXXXX to XX.XXXXXXXXXX
2017-11-28 21:15:30.874 [ItemStateChangedEvent] - mqttPhilippLongitude changed from XX.XXXXXXXXXX to XX.XXXXXXXXXX
2017-11-28 21:15:30.892 [ItemStateChangedEvent] - locationPhilipp changed from XX.XXXXXXXXXX,XX.XXXXXXXXXX to XX.XXXXXXXXXX,XX.XXXXXXXXXX
2017-11-28 21:15:30.895 [ItemStateChangedEvent] - mqttPhilippAccuracy changed from 165 to 10
==> /var/log/openhab2/openhab.log <==
2017-11-28 21:15:30.900 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Mqtt Postion Parse Philipp': An error occurred during the script execution: index=1, size=1

```

---

<div class="post-metadata">

**Author:** ![RiotMode](https://community.openhab.org/user_avatar/community.openhab.org/riotmode/32/80291_2.png) [@RiotMode](https://community.openhab.org/u/RiotMode)\
**Post date:** [November 29, 2017, 2:01am UTC](https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730/11 "2017-11-29T02:01:38Z")

</div>

i got it, a simple “/” made the difference - omg hours of searching 😃

it needs to be this: /iPhone

```auto
{ mqttitude="broker:owntracks/riotmode/iPhone" }

```

instead of this: /iPhone/

```auto
{ mqttitude="broker:owntracks/riotmode/iPhone/" }

```

but still having troubles with the rule - can someone help here ? - thanks

---

<div class="post-metadata">

**Author:** ![Dries](https://community.openhab.org/user_avatar/community.openhab.org/dries/32/8797_2.png) [@Dries](https://community.openhab.org/u/Dries)\
**Post date:** [November 29, 2017, 10:00am UTC](https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730/12 "2017-11-29T10:00:50Z")

</div>

> [@RiotMode](#):
>
> but still having troubles with the rule - can someone help here ? - thanks

I’m not sure. Your rule looks exactly the same than mine. Except for this part:

```auto
logInfo("ownTRACKS=", +mqttPositionPhilippRaw.state)

```

Can you change it to this and see what happens?

```auto
logInfo("ownTRACKS", mqttPositionPhilippRaw.state.toString)

```

A nice addon is translating GPS coordinates to a formatted address. I’ve done it like this:

```auto
val apiKey = "AIzaSykjgkjgkjgkjgkjgkjgkjg2u8"		
val geocodeURL = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + mqtt_PhoneDries_Location.state.toString + "&language=dutch&sensor=true&key=" + apiKey
val String geocodeJson = sendHttpGetRequest(geocodeURL)
var String formattedAddress = transform("JSONPATH", "$.results[0].formatted_address", geocodeJson)
formattedAddress = formattedAddress.replace(", Belgium", "")
mqtt_PhoneDries_Location_Address.postUpdate(formattedAddress)

```

(not my credit - I’ve found this on this forum as well).

Obviously, you will need to get your apiKey from Google.

[EDIT]  
Credit where credit is due:  
[https://community.openhab.org/t/use-google-geocode-api-to-get-location-string-from-coordinates](https://community.openhab.org/t/use-google-geocode-api-to-get-location-string-from-coordinates)  
[/EDIT]

---

<div class="post-metadata">

**Author:** ![RiotMode](https://community.openhab.org/user_avatar/community.openhab.org/riotmode/32/80291_2.png) [@RiotMode](https://community.openhab.org/u/RiotMode)\
**Post date:** [November 29, 2017, 12:06pm UTC](https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730/13 "2017-11-29T12:06:32Z")

</div>

> [@Dries](#):
>
> Can you change it to this and see what happens?

yeah no errors 🙂 thank you 😊  
i will give this Google Addon a try , later the day 🙂

owntracks looks like this now 🙂

 ![23](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/8/1/816ab7585448d14ce2180b9cb065d0ce41f99a83.jpeg)

---

<div class="post-metadata">

**Author:** ![Dries](https://community.openhab.org/user_avatar/community.openhab.org/dries/32/8797_2.png) [@Dries](https://community.openhab.org/u/Dries)\
**Post date:** [November 29, 2017, 2:23pm UTC](https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730/14 "2017-11-29T14:23:03Z")

</div>

Looks nice.

One small details regarding this:  
 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/c/8/c8ca3ad71cc5994f4ce8a7a3499f9e896cd71399.jpeg)

If you define your item like this:

```auto
Switch mqtt_PhoneDries_Region_Thuis "Waar is Dries? [MAP(thuis.map):%s]" 

```

Your sitemap like this:

```auto
Text item=mqtt_PhoneDries_Region_Thuis

```

And your transformation like this:

```auto
ON=Thuis
OFF=Weg van huis
-=-
undefined=-
uninitialized=-
NULL=-

```

You will see this:  
 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/9/6/96e9df8cb9ff6effd56a1377096fa8079a9c9c9f.png)

---

<div class="post-metadata">

**Author:** ![RiotMode](https://community.openhab.org/user_avatar/community.openhab.org/riotmode/32/80291_2.png) [@RiotMode](https://community.openhab.org/u/RiotMode)\
**Post date:** [November 29, 2017, 2:30pm UTC](https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730/15 "2017-11-29T14:30:55Z")

</div>

you are right, thats a much nicer way to display the presence 🙂  
thanks !!

---

<div class="post-metadata">

**Author:** ![Edwin\_de\_Boer](https://community.openhab.org/user_avatar/community.openhab.org/edwin_de_boer/32/36016_2.png) [@Edwin\_de\_Boer](https://community.openhab.org/u/Edwin_de_Boer)\
**Post date:** [February 18, 2018, 4:36pm UTC](https://community.openhab.org/t/owntracks-setup-to-work-with-oh/36730/16 "2018-02-18T16:36:36Z")

</div>

Hello Phillipp,

That map look really cool.  
I have found the code and adjusted it to my variables.  
I have saved the html file into the folder html of the configurations.

But how can I request this file to show on the sitemap?

Thanks,

Edwin
