Owntracks setup to work with OH

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

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

my items:

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

my sitemap:

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

Thank you!

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.

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”:

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:

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

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

Thanks!

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’

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)

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

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

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

my test items:

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)" }

I think I’ve sussed the battery part!

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

yes the battery part is working now :heart_eyes:

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 :confused:

1 Like

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!

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

okay thank for the clarification @Confused

i found this “tutorial” on github and got this so far:

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:

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:

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

and i get this in the log:

[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

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

it needs to be this: /iPhone

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

instead of this: /iPhone/

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

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:

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

Can you change it to this and see what happens?

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

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

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
[/EDIT]

2 Likes

yeah no errors :slight_smile: thank you :blush:
i will give this Google Addon a try , later the day :slight_smile:

owntracks looks like this now :slight_smile:

Looks nice.

One small details regarding this:
image

If you define your item like this:

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

Your sitemap like this:

Text item=mqtt_PhoneDries_Region_Thuis

And your transformation like this:

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

You will see this:
image

1 Like

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

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