Find distance between 2 objects (Lat/Lon required)

Moderator: leecollings

Post Reply
User avatar
FireWizard
Posts: 1745
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Find distance between 2 objects (Lat/Lon required)

Post by FireWizard »

Hello All,

While reading some posts on this forum, I came across viewtopic.php?f=65&t=37123&p=280417&hil ... ta#p280417 and saw that the Toyota API is obviously capable to indicate
Distance to home - Shows the distance between the car and home
Of course it is questionable, what the use case is for such a function, but you might think of an action to carry out (start the coffee machine: D ), in case you are approaching your home (e.g. the distance is less than .... or the duration to reach your home is less than ....)

On a rainy afternoon, I decided to implement it for my Volvo Car and the distance to my Home, but it can also be used for the distance between two persons or between a person and the home address. But the final use case is up to your imagination :).

Requirements:
  • Object has to present the Lat/Lon co-ordinates
  • Domoticz connected to the Internet
  • MQTT broker (Mosquitto installed
  • Node Red installed
Ad 1. For tracking the location of your smartphone. you have to install "Owntracks" or "Nexttracks" on your smartphone.
See: https://owntracks.org/ or https://f-droid.org/en/packages/org.nexttracks.android/

Ad 2. This application uses the API from http://project-osrm.org/ (OSRM: Open Source Routing Machine) with uses the data from OpenStreetMap).
For detailed information of the API, see the documentation on the OSRM website.

I created the following Node Red flow:

Screenshot_Distance_flow.png
Screenshot_Distance_flow.png (50.73 KiB) Viewed 1264 times
First, at the top left, we will see an "Inject" node. Nothing special, but it will trigger a "HTTP Request" node in order to get the settings from Domoticz as put in the Latitude and Longitude fields. Normally this is a "one time" setting and it will hopefully not change very often :). If it changes, you have another problem and we should make something to detect earthquakes. You can also put that value in the "Inject" node.
It requests the Lat/Lon of your "Home" only once, as the flow is started.

As I own a Volvo Car and use the Volvo On Call application (See: viewtopic.php?f=38&t=26549&hilit=Volvo),
I will receive the Lat/Lon co-ordinates from VoC only if the car has come to a standstill. So it is probably better to use the Smartphone.

But for the first release it is fine.

The two different sets of co-ordinates (Home and Car) are joined and parsed in the next "Change" node.

Screenshot_Distance_Change_node.png
Screenshot_Distance_Change_node.png (49.83 KiB) Viewed 1264 times

The next step is to prepare the URL to query the OSRM API.

The contents of the "Function" node is as follows:

Code: Select all

var url = "http://router.project-osrm.org";
var service = "route";
var version = "v1";
var profile = "driving";
var options = "?overview=false&generate_hints=false&skip_waypoints=true"

msg.url = url + "/" + service + "/" + version + "/" + profile + "/" + msg.payload.home_lon + "," + msg.payload.home_lat + ";" + msg.payload.car_lon.toFixed(6).toString() + "," + msg.payload.car_lat.toFixed(6).toString() + options;

return msg;
This "Function" node configures the different possible settings. See the previous mentioned documentation.

I limited the number of digits of Lat and Lon after the decimal point to 6.

In order to avoid flooding of the OSRM API, I did use a "Filter" node (previous called a "RBE' node.

You will receive the "Duration" in "Seconds" and the "Distance" in "Meters". In order to avoid a huge number of seconds, if you are far away, I converted it to milliseconds (with the "Change" node and the JSONATA expression and the "Moment" node.

The next question is, what type of Domoticz Sensor to use?

Domoticz has a Sensor of the type "Distance", but you can only set that to cm. Not very useful
The next possibility is the "Custom" sensor. However that sensor can only display 1 value.
As I wanted 2 values (Distance and Duration) and decided to use the "Text" sensor (Icon has been changed)

The result is as follows (for a car at home):

Screenshot_Distance_Widget.png
Screenshot_Distance_Widget.png (40.97 KiB) Viewed 1264 times

Please find the complete flow below:

Code: Select all

[{"id":"cb78f72ce183cc3f","type":"tab","label":"Car-Home Distance","disabled":false,"info":""},{"id":"714e4cf506f868f1","type":"http request","z":"cb78f72ce183cc3f","name":"HTTP Domoticz","method":"GET","ret":"obj","paytoqs":"ignore","url":"http://192.168.10.50:8080/json.htm?type=settings","tls":"","persist":false,"proxy":"","authType":"","credentials":{},"x":400,"y":80,"wires":[["8e57561dad7fa6af"]]},{"id":"5c0992b908348937","type":"inject","z":"cb78f72ce183cc3f","name":"Request Home GPS","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"home_GPS","payloadType":"date","x":160,"y":80,"wires":[["714e4cf506f868f1"]]},{"id":"6a9ba37b7eec1ef5","type":"mqtt in","z":"cb78f72ce183cc3f","name":"MQTT-Owntracks Volvo","topic":"owntracks/volvo/ABC123","qos":"2","datatype":"json","broker":"2d06e6e96ce54335","nl":false,"rap":false,"x":380,"y":160,"wires":[["8e57561dad7fa6af"]]},{"id":"eb27ba6c0c8636e9","type":"http request","z":"cb78f72ce183cc3f","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":570,"y":280,"wires":[["49b658305e195907"]]},{"id":"b8729301cea6773c","type":"function","z":"cb78f72ce183cc3f","name":"Create URL OSRM","func":"var url = \"http://router.project-osrm.org\";\nvar service = \"route\";\nvar version = \"v1\";\nvar profile = \"driving\";\nvar options = \"?overview=false&generate_hints=false&skip_waypoints=true\"\n\nmsg.url = url + \"/\" + service + \"/\" + version + \"/\" + profile + \"/\" + msg.payload.home_lon + \",\" + msg.payload.home_lat + \";\" + msg.payload.car_lon.toFixed(6).toString() + \",\" + msg.payload.car_lat.toFixed(6).toString() + options;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":170,"y":280,"wires":[["bd723b5404883010"]]},{"id":"8e57561dad7fa6af","type":"join","z":"cb78f72ce183cc3f","name":"Join GPS","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":600,"y":120,"wires":[["9b499945b6a77dd2"]]},{"id":"9b499945b6a77dd2","type":"change","z":"cb78f72ce183cc3f","name":"","rules":[{"t":"move","p":"payload.home_GPS.Location.Latitude","pt":"msg","to":"payload.home_lat","tot":"msg"},{"t":"move","p":"payload.home_GPS.Location.Longitude","pt":"msg","to":"payload.home_lon","tot":"msg"},{"t":"move","p":"payload[\"owntracks/volvo/ABC123\"].lat","pt":"msg","to":"payload.car_lat","tot":"msg"},{"t":"move","p":"payload[\"owntracks/volvo/ABC123\"].lon","pt":"msg","to":"payload.car_lon","tot":"msg"},{"t":"delete","p":"payload.home_GPS","pt":"msg"},{"t":"delete","p":"payload[\"owntracks/volvo/ABC123\"]","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":120,"wires":[["b8729301cea6773c"]]},{"id":"bd723b5404883010","type":"rbe","z":"cb78f72ce183cc3f","name":"Filter","func":"rbe","gap":"","start":"","inout":"out","septopics":false,"property":"payload","topi":"topic","x":390,"y":280,"wires":[["eb27ba6c0c8636e9"]]},{"id":"d68a160b6fa6988b","type":"inject","z":"cb78f72ce183cc3f","name":"Reset","props":[{"p":"reset","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":210,"y":340,"wires":[["bd723b5404883010"]]},{"id":"001542253fa72c7b","type":"function","z":"cb78f72ce183cc3f","name":"Prepare Domoticz","func":"msg.payload = {\n    \"command\": \"udevice\",\n    \"idx\": 11,\n    \"nvalue\": 0,\n    \"svalue\": \"Distance: \" + (msg.payload.routes[0].distance / 1000).toFixed(4).toString() + \" km \\n Duration: \" + (msg.payload.routes[0].duration).toString()\n    };\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":750,"y":360,"wires":[["932177559ea7358f"]]},{"id":"932177559ea7358f","type":"mqtt out","z":"cb78f72ce183cc3f","name":"To Domoticz","topic":"domoticz/in","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"f9f13036.e28b58","x":970,"y":360,"wires":[]},{"id":"88187caba49c039b","type":"moment","z":"cb78f72ce183cc3f","name":"Format Time","topic":"","input":"payload.routes[0].duration","inputType":"msg","inTz":"ETC/GMT","adjAmount":0,"adjType":"days","adjDir":"add","format":"HH:mm:ss","locale":"en_US","output":"payload.routes[0].duration","outputType":"msg","outTz":"ETC/GMT","x":970,"y":280,"wires":[["001542253fa72c7b"]]},{"id":"49b658305e195907","type":"change","z":"cb78f72ce183cc3f","name":"To millisecs","rules":[{"t":"set","p":"payload.routes[0].duration","pt":"msg","to":"msg.payload.routes[0].duration*1000","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":280,"wires":[["88187caba49c039b"]]},{"id":"2d06e6e96ce54335","type":"mqtt-broker","name":"Jonas_MQTT_Server","broker":"192.168.10.24","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"f9f13036.e28b58","type":"mqtt-broker","name":"localhost","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]
I'm open for suggestions, ideas, comments etc.

Have Fun and Best Regards
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests