OpenHAB and OwnTracks (formerly MQTTitude) Info

2,191 views
Skip to first unread message

nde...@gmail.com

unread,
May 22, 2014, 10:28:11 AM5/22/14
to ope...@googlegroups.com
So I'm just in the process of tying together OwnTracks, Mosquitto, and OpenHAB on a Raspberry Pi, and have full plans to help update the documentation on the OwnTracks Wiki and the OpenHAB wiki to help future users too.

I have OwnTracks set up properly with regions, tracking my location and updating properly to the Mosquitto server.

One issue I'm having is the actual integration of OwnTracks info into OpenHAB. Once I have the MQTT binding set up, and I have put my Regions as a switch item, how do I use this information?
I plan on using it as presence detection, but how do I tell OpenHAB to check that, and how can I show the presence data on the sitemap?
I don't need the latitude and longitude information, just a simple Home = Yes/No, Work/Away, etc = Yes/No.

This is the last stumbling block I have before I can finish up my instructions/guide.

Thanks for any input!

Message has been deleted

Scott K

unread,
May 22, 2014, 1:04:43 PM5/22/14
to ope...@googlegroups.com, nde...@gmail.com
Maybe Jan-Piet Mens will chime in on this one.  Jan-Piet has an example of some python script to bring up a webpage (google maps) with the URL encoded GPS location.  See his Owntracks page here.  I don't know how to implement what he writes about, but I'd be interested in it if anyone else makes this work and could give a detailed account for newbies.


Maybe there's a way to put a button on OpenHAB that will open a webpage showing the location on a map?  It won't be as nice as being part of OpenHAB, but next close thing.  Or maybe one of the widgets can allow you to bring in a map image from OSM showing the location...somehow.  When I asked a question about how to make custom graphics, I was told "use the REST API w/ Javascript"...I have no idea what that means.  

Sorry, no solutions from me.  But I just wanted to express my interest in the same thing.

Scott K

unread,
May 22, 2014, 1:41:28 PM5/22/14
to ope...@googlegroups.com, nde...@gmail.com
This might help. Check out what Dan did on his Jan 29th post:


He bound a string to an MQTT topic containing the google maps URL location with the Long/Lat.  I know you weren't interested in bringing up a google map of the GPS location inside OpenHAB, but maybe you can use the same method.

Use a "transform" in the form of a Javascript file, put it in the configuration/Transform folder.  The transform takes in the GPS coordinate, and computes an output string "Yes" or "No", based on the GPS location.  Something like 45<long<50 AND 120<lat<125...

I don't really understand how Dan did this, to be honest, but it looks kind of close to what you want.  If you figure it out, I'd appreciate more details.  I wish there was a tutorial on using javascript to create transforms.

dan cunningham

unread,
May 22, 2014, 2:45:06 PM5/22/14
to ope...@googlegroups.com
I'm assuming you have openhab successfully connecting to the same mqtt broker as owntracks.   Once this is done you create items (say a switch item) that bind to the mqtt path that owntracks is publishing to.  So for example i have an item that switches on if my presence is detected at all like this :



--
You received this message because you are subscribed to the Google Groups "openhab" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openhab+u...@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at http://groups.google.com/group/openhab.
For more options, visit https://groups.google.com/d/optout.

dan cunningham

unread,
May 22, 2014, 2:59:26 PM5/22/14
to ope...@googlegroups.com
I'm assuming you have openhab talking with the same mqtt broker as owntracks.  If this is the case you can bind items to specific owntracks locations like so: 

Switch  PresenceDan_PhoneMqttHome   "Dan @ Home"   { mqttitude="home:owntracks/daniel/iphone5s:home" }
Switch  PresenceDan_PhoneMqttWork   "Dan @ Work"   { mqttitude="home:owntracks/daniel/iphone5s:work" }

 home is my mqtt broker, between the two colons is the topic id in the owntracks settings (which is made from the client id and  device id). The word after the last colon is the description label.  This is set by clicking on a dropped pin and filling in the description field in owntracks.  I have two, home and work.  If i enter one of these areas then that item turns on (or off when I leave).   Omitting the last colon and description label will give you a switch that is on whenever there presence detected,  which will be almost always so its not that useful except for tracking/coordinates purposes.  This trick can generate a map url for you https://github.com/openhab/openhab/wiki/Samples-Tricks#how-to-format-a-google-maps-url-from-a-mqttitude-mqtt-message .  I also have a little web page I embed in a webview which uses the openhab REST api to get the coordinates and display a google map.  I will try and post that this weekend. 


On Thu, May 22, 2014 at 11:41 AM, Scott K <doh...@gmail.com> wrote:

--

Ben Jones

unread,
May 22, 2014, 4:07:32 PM5/22/14
to ope...@googlegroups.com
Dan is correct. His example is what you want/need. This is the best way to use OwnTracks/Mqttitude. The alternative is to leave out the region description (last token in the item binding) and enter your home coordinates in your openhab.cfg file in the Mqttitude binding config. Then the binding will manually do calculations each time a location update is sent from your OwnTracks app (regardless of regions/waypoints) and determine you location relative to your home coordinates. If you are within the geofence specified the presence switch will be flicked on.

This was how the binding originally worked, before the region/waypoint features were added to the OwnTracks apps. Now those features are present in both iOS and Android apps it is a better option to use them.

There is quite a bit of documentation about the two different modes in the WIKI;


Feel free to improve this if you think bits are missing or it is confusing in any way.

Anthony Lavado

unread,
May 23, 2014, 2:08:45 AM5/23/14
to ope...@googlegroups.com
I do have OwnTracks and OpenHAB speaking to the same MQTT broker, and I do have the switch items set up exactly the way Dan described it there.

There are two questions then:
1) How does my presence in a region get displayed in OpenHAB? I know it doesn't need to be "shown" in order to work in a rule, but I would like to show it. Not as a map, or co-ordinates, just like a regular binary switch.

For example, in the Wi-Fi presence detection, they create an extra switch that is just called "Presence", and use a rule to verify if a device is present (in the "on" state), and then flip the presence switch to "on".

Since we already have a switch created for the Region (such as PresenceDan_PhoneMqttHome), are we not able to show it's status (event enter = on, event leave = off) graphically in the site map?

2) Is the information returned from the region check a True/False? Is it an On/Off? Once I know what type of value it returns, how do I call this value in a rule? That is to say, once I have a rule to perform a set of actions for me arriving home (entering the home region), what do I put for the rule's trigger?

I hope this provides a little more clarity into what my questions were. I really love the idea of OwnTracks, OpenHAB, and MQTT in general. My goal is to take the documentation for these pieces, and turn it into something very easily actionable - with an snippets of example rules/items/sitemap - so if someone else wanted to set this up, they can do so quickly. I know it won't be turn key and consumer friendly just yet, but it would be a step in that direction.

Thanks in advance!

dan cunningham

unread,
May 23, 2014, 9:27:36 AM5/23/14
to ope...@googlegroups.com
Hi Anthony, If  I'm understanding you correctly you want a switch in the sitemap (gui) to be on when you are in a region.  The item line

Switch  PresenceDan_PhoneMqttHome   "Dan @ Home"   { mqttitude="home:owntracks/daniel/iphone5s:home" }

is exactly that.  In my sitemap file I have 

Switch item=PresenceDan_PhoneMqttHome

which displays this as an on off switch, and turns on when in that region, off when I leave.  I also have rules that fire off when the item changes. 


Ben Jones

unread,
May 25, 2014, 2:22:56 AM5/25/14
to ope...@googlegroups.com
Works great Dan - thanks very much for sharing that.

One question - where is the image shown? The Google Maps marker has a title and an image defined. When I hover over the pin in my map I see the title shown in a tooltip, but there is no sign of the image anywhere. Not too worried about it, as the Google Maps pin is just fine, but just wondering if I have missed something.

Anthony Lavado

unread,
Jun 3, 2014, 11:47:28 AM6/3/14
to ope...@googlegroups.com
Thanks for the Map display tip, that works wonderfully. It looks like the marker is set up right, but indeed it doesn't show.

As well, the map is getting my location info correctly, but somehow the switches still don't seem to turn on or off on entering or leaving a region in OwnTracks.

Any idea how I can go further or enable logging to track down the issue?

Message has been deleted

Jan-Piet Mens

unread,
Jun 4, 2014, 1:49:19 AM6/4/14
to ope...@googlegroups.com
> Can you explain how the transform (mqttitude-coordinates.js) works?

The payload passed to the transformation is JSON (maybe read up on what
that is at json.org). That object looks like this:

{
"lat" : xxxx,
"lon" : yyyy
}

What the Javascript transform file (mqttitude-coordinates.js) does is to
load that JSON as a Javascript object (eval(...)), which results in a JS
variable called location with two elements `lat' and `lon'.

(openHAB knows nothing about that, and it doesn't care.)

The second line in the .js builds a string which finally looks like

"xxxx,yyyy"

Hope that helps a bit.

-JP

dan cunningham

unread,
Jun 4, 2014, 7:57:48 AM6/4/14
to ope...@googlegroups.com
I have also noticed that openhab does not always get the enter/leave events from owntracks, have not had time to hunt down the issue.  The best thing to do would be to run openhab under debug or trace logging and see what the mqtt binding spits out.  

Jan-Piet Mens

unread,
Jun 4, 2014, 9:41:50 AM6/4/14
to ope...@googlegroups.com
> I have also noticed that openhab does not always get the enter/leave events
> from owntracks, have not had time to hunt down the issue.

The OwnTracks events are very reliable, *iff* the apps publish them. I
know that, because I pushover enter/leave for 10 people connected to my
broker. :)

(There are edge cases in which an event can be issued later, e.g. if the
iOS output queue is full; this should be done with since today with 7.2
because we wake up periodically and 'ping' the broker with a PUB.)

You can monitor what your broker gets with a mosquitto_sub of course,
but you know that; I just wanted to clarify.

As to when openHAB gets the events, that's part of the MQTT/MQTTitude
bindings. Ben has been working on those, IIRC, because he found some
cases in which openHAB would 'hang' (is that the correct word).

Just sayin' ;-)

-JP

Anthony Lavado

unread,
Jun 4, 2014, 10:00:45 AM6/4/14
to ope...@googlegroups.com
Yeah, I've noticed that they get published just fine under Significant Change mode, but I'll run OpenHAB with trace on to see what it's finding.

I know the code freeze is coming up soon for the 1.5 release... Hopefully the new versions won't get stuck :-)

Ben Jones

unread,
Jun 4, 2014, 4:08:59 PM6/4/14
to ope...@googlegroups.com
I can also testify that I am not aware of ever missing a update from OwnTracks in openHAB.

dan cunningham

unread,
Jun 6, 2014, 9:40:38 AM6/6/14
to ope...@googlegroups.com
The issue was in owntracks, the last time the app was updated my settings dictionary somehow got corrupted, opening it it up popped up a bunch of NSDictionary errors ( i should have taken a screenshot).  Going through and setting up my labeled pins again seems to have everything working. 


On Wed, Jun 4, 2014 at 2:08 PM, Ben Jones <ben.j...@gmail.com> wrote:
I can also testify that I am not aware of ever missing a update from OwnTracks in openHAB.

--

Anthony Lavado

unread,
Jun 7, 2014, 3:20:46 PM6/7/14
to ope...@googlegroups.com
So this is what I end up with when I ran OpenHAB with trace debugging (sanitized):

22:42:26.724 TRACE o.o.i.t.m.i.MqttBrokerConnection[:534]- Received message on topic 'owntracks/anthonylavado/iphone5' : {"desc":"home","t":"c","tst":"1401936112","acc":"5","lon":"-xx.xxxx","_type":"location","event":"enter","lat":"xx.xxxx","batt":"40"}
22:42:26.729 TRACE o.o.b.m.i.MqttitudeConsumer[:109]- Message received on topic owntracks/anthonylavado/iphone5: {"desc":"home","t":"c","tst":"1401936112","acc":"5","lon":"-xx.xxxx","_type":"location","event":"enter","lat":"xx.xxxx","batt":"40"}
22:42:28.148 TRACE o.o.b.m.i.MqttitudeConsumer[:123]- Checking item PresenceAnthony_PhoneMqttWork...
22:42:28.154 TRACE o.o.b.m.i.MqttitudeConsumer[:165]- Location enter event is for region 'home', ignoring
22:42:28.157 TRACE o.o.b.m.i.MqttitudeConsumer[:123]- Checking item PresenceAnthony_PhoneMqttHome...
22:42:28.160 DEBUG o.o.b.m.i.MqttitudeConsumer[:173]- PresenceAnthony_PhoneMqttHome has entered region home

So it would seem that OpenHAB is able to process the event, however the switch I have never turns on.

This is my site map:

sitemap home label="Main Menu"
{
Frame label="Location" {
Switch item=PresenceAnthony_PhoneMqttHome
Switch item=PresenceAnthony_PhoneMqtt
Webview  url="xxxx/static/map.html"  height=12
}
}


And my item configuration:
 
/* Location and Presence */
Switch PresenceAnthony_PhoneMqttHome "Anthony @ Home" {mqttitude="home:owntracks/anthonylavado/iphone5:home"}
Switch PresenceAnthony_PhoneMqtt "Anthony's Phone Connected" {mqttitude="home:owntracks/anthonylavado/iphone5"}
String Location_Anthony_Phone {mqtt="<[home:owntracks/anthonylavado/iphone5:state:JS(mqttitude-coordinates.js)]"

Any possible idea as to why the switch isn't turning on when I enter the home region?

dan cunningham

unread,
Jun 7, 2014, 4:39:48 PM6/7/14
to ope...@googlegroups.com
That looks ok to me, I'm not sure why the switch is not on, is this the case for both switches?


--

Anthony Lavado

unread,
Jun 7, 2014, 6:25:37 PM6/7/14
to ope...@googlegroups.com
You know what, it started working. I have no idea why. I'll keep an eye on it.

--
Anthony Lavado
You received this message because you are subscribed to a topic in the Google Groups "openhab" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openhab/DdXgjjAbCbQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openhab+u...@googlegroups.com.

gluf...@gmail.com

unread,
Nov 8, 2014, 12:41:59 PM11/8/14
to ope...@googlegroups.com
How can I convert the tst field from the JSON package? It is a timestamp of the moment when the GPS data are sent from Owntracks app in UNIX format (seconds since 1970). How to get normal date/time string? 

Rajat Sud

unread,
Nov 11, 2014, 12:13:50 AM11/11/14
to ope...@googlegroups.com
I am using this in the items file

String Location_Rajat_Phone {mqtt="<[mymosquitto:owntracks/rajat/note2android:state:JS(mqttitude-coordinates.js)]"}

and this in the config file
mqttitude:home.lat=12.0
mqttitude:home.lon=77.0

# Optional. The geofence radius.
mqttitude:geofence=200

Dan

unread,
May 24, 2014, 10:12:44 AM5/24/14
to ope...@googlegroups.com
To unsubscribe from this group and stop receiving emails from it, send an email to openhab+unsubscribe@googlegroups.com.

Nicholas Waterton

unread,
Jul 10, 2015, 8:43:11 AM7/10/15
to ope...@googlegroups.com
To get the timestamp I am using this:

var location = eval('(' + input + ')');
var cal = new java.util.GregorianCalendar();
cal.setTimeInMillis(location.tst*1000);
var t = new org.openhab.core.library.types.DateTimeType(cal);
result = t;

as the transform mqttitude-timestamp.js

Here is the item:

DateTime NickLocLastUpdate "Nick's Phone Last location Update [%1$tm/%1$td/%1$tY %1$tH:%1$tM:%1$tS]" <clock>  (gPeople) { mqtt="<[proliant:owntracks/Nick/iPhone:state:JS(mqttitude-timestamp.js)]" }

Regards,
Reply all
Reply to author
Forward
0 new messages