Coordinates and distance calculation in OH2?

Anyone please show me how to get my coordinates for my current location and display them on sitemap?
Also, how can I calculate the distance from my current location to my home/workplace?
I’m using OH2 on raspberry pi 3
Please help me, I’d be very appreciated!

Oh thanks for replying @watou
But the first line in your rule file:

import org.openhab.core.library.types.PointType  // remove under OH2

Does it mean that this library doesn’t work with OH2?

No, it means to remove just that line of the file if on OH2. The line is needed under OH1.

Well anyway can I update my current location to items “Location MyCarLoc” ?
I mean is there any binding available which enable us to track GPS our phones (realtime tracking would be awesome) in newest OH2?

The MQTT binding will directly update Location items, and the source of updates would be the OwnTracks app. Is that not what you want?

1 Like

Oh I get it now
sorry if I’m asking for too much

I’ve a few more questions, I hope you can answer:

  1. I installed Owntracks app but I don’t know how to set up the connection between this app with Location items. Because I don’t see any option in this app allow me to do that
  2. Can I have many item locations and each of them represent separately specific places. I just want to make sure that this app doesn’t consider all Location items as one
  3. After searching in this forum I saw another post relating to my problems:
https://community.openhab.org/t/mqtt-mqttitude-owntracks/3744/44

(I hope the admin will merge my post into this post so that it’s easier for other people who want to dig into this matter)
I just want to confirm that I get your idea. You said if I put all three items you gave above into a group like this:

Group Locations

and the sitemap:

Group item=Locations

I will get the map indicating my current position in the classic UI, right? Unfortunately, at this point, I receive nothing.
Furthermore, I see many guys here define mqtt and mqttitude inside item definition so that the string items can be updated. Thus,I think in your configuration:

String MyCarLocString
Location MyCarLoc
Number DistanceFromHome

should be added these so-called “mqtt” or “mqttitude” parts, am I right? I think it must have some kind of trigger to activate the event :“Item MyCarLocString changed” in your rule file to happen.Please correct if I’m wrong
At last, so sad that the android app cannot display the map, you said. I hope the maintainers of openhab can fix this.

@watou Umm, please give me some hint, is that OK?

Example (untested) for an OwnTracks user named “John”. If you navigate into the sitemap

items/john.items

Location LocJohn "John GPS [%s]"  { mqtt="<[mosquitto:owntracks/john/phone:state:JS(owntracks-coordinates.js)]" }

transform/owntracks-coordinates.js

var location = JSON.parse(input);
result = location.lat + "," + location.lon;

sitemap/john.sitemap.fragment

sitemap john label="Where Is John"
{
    MapView item=LocJohn
}

Thanks to you now I’ve made a progress, I can see that OH2 is receiving something from owntracks on my android phone whenever I pressed the arrow-like button on the top-right corner.
But the receive message is an error like this:

==> /var/log/openhab2/openhab.log <==
2017-01-30 10:44:54.335 [ERROR] [.mqtt.internal.MqttMessageSubscriber] - Error processing MQTT message.
org.openhab.core.transform.TransformationException: An error occured while loading script.
        at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:63)[176:org.openhab.core.compat1x:2.0.0]
        at org.openhab.binding.mqtt.internal.MqttMessageSubscriber.processMessage(MqttMessageSubscriber.java:138)[233:org.openhab.binding.mqtt:1.9.0]
        at org.openhab.io.transport.mqtt.internal.MqttBrokerConnection.messageArrived(MqttBrokerConnection.java:552)[185:org.openhab.io.transport.mqtt:1.9.0]
        at org.eclipse.paho.client.mqttv3.internal.CommsCallback.handleMessage(CommsCallback.java:336)[185:org.openhab.io.transport.mqtt:1.9.0]
        at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:148)[185:org.openhab.io.transport.mqtt:1.9.0]
        at java.lang.Thread.run(Thread.java:745)[:1.8.0_111]

Please help me fix this <3 @watou
Item:

Location LocJohn "John GPS [%s]"  { mqtt="<[mosquitto:owntracks/john/phone:state:JS(owntracks-coordinates.js)]" }

On Owntracks app:
host: 192.168.1.11 (my raspberry pi IP in wifi network)
port:1883 (this is the default port for mqtt, just like in service/mqtt.cfg"
identification: Authentication:OFF, username:john, DeviceId:phone, trackerID:jo
No security, No parameters

  1. Did you install JavaScript transformation?
  2. Did you put .js file in transform directory with correct file ownership and permissions?

I did install javascript transformation


Here is the information about mqtt-coordinates.js:

Might be you will be confused because I put the permission of this file as “root” user. But I’m sure that’s fine to me because I’ve other .map files also belong to ‘root’ but they work normally whenever I use them, for example:

Switch Garden_Rain "Rain [MAP(rain.map):%s]" <water> (Garden,Rain){mqtt="<[mymosquitto:gardenrain:state:default]"} 

It works fine
p.s: when I tried executing the mqtt-coordinate.js from terminal screen:

pi@raspberrypi:/etc/openhab2/transform $ sudo ./mqttitude-coordinates.js
./mqttitude-coordinates.js: 1: ./mqttitude-coordinates.js: Syntax error: "(" unexpected

The content of mqtt-coordinates.js is exactly like your owntracks-coordinates.js:

var location = JSON.parse(input);
result = location.lat + "," + location.lon;

One more thing, whenever I tried adding this line:

val PointType home = new PointType("12.121212, 123.123123")

It’s always generating an error like this:

[ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Mqtt Position - Ben': null

That won’t work; it’s not a shell command.

You don’t want a space after the comma. On openHAB 2, you should not have to import the class PointType. So I don’t know what the problem is. Are you sure that that line is the cause of the problem? The problem might be elsewhere in the rule file.