While i´am thinking about some useful features for my home-automation system based on openHAB, i got the idea of displaying current traffic information based on the street condition of my daily route.

The difficult part at first was to choose the best data-source. In the selection process, I first looked at Google Maps, the benefits: 2.500 request per day with the distance matrix API, structured JSON Response without crappy overhead, good documentation – the disadvantage: real-time traffic information only for paying customers 🙁

The second selection was Bing Maps (yes I´m serious). The benefits: real-time traffic information J disadvantage: 125.000 requests per year, crappy overhead in JSON response and confusing documentation – but it’s something.

After a while of reading, I have decided to use BingMaps REST Services and the Routes API. With a simple GET Request and some parameters you get a complete route calculation, including direction information, travel time, etc. in the JSON type.

You need to change the wp.0 parameter (Source), wp.1 parameter (destination) and also your API-Key. In some cases, only GPS coordinates will work instead of street and city name.

That’s a lot of information which I not really, so I took a look at the request parameters for some optimizing. I added optmz=timeWithTraffic and routeAttributes=routeSummariesOnly to the request string.

{"authenticationResultCode":"ValidCredentials","brandLogoUri":"http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png","copyright":"Copyright © 2015 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.","resourceSets":[{"estimatedTotal":1,"resources":[{"__type":"Route:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1","id":"v66,h-1126757592,i0,a0,cen-US,d_z839HUoyis1,y1,s1,m1,o1,t0,wpQ6UBPMa_f81~A9pxGcWh55gDAADgAfp39T4A0~TG9uZG9uLCBMb25kb24sIFVuaXRlZCBLaW5nZG9t0~~~,w6azHBBy23P81~A9pxGcUBEbUBAADgAQAAAAAA0~TGVlZHMsIExlZWRzLCBVbml0ZWQgS2luZ2RvbQ2~~~,k1,n2","distanceUnit":"Kilometer","durationUnit":"Second","travelDistance":310.683,"travelDuration":11890,"travelDurationTraffic":14343}]}],"statusCode":200,"statusDescription":"OK","traceId":"8c381a3bf3d04b50a979e43f48dc595e|BN20130423|1.0.0.0|BN2SCH020171549, BN2SCH020201454, BN2SCH020181019, BN2SCH020180721, BN2SCH010050548, BN2SCH020180952, BN20271629, BN20100432"}

Not perfect (like Google Maps) but usable. The important part for me is the time calculation (including real-time traffic) for the route, specified in the field travelDurationTraffic.

Let’s start with the scripting. The script is written in PHP and will extract the information for travelDistance, travelDuration & travelDurationTraffic. After that it will convert the time into minutes (the original JSON response is in seconds).

Now I have the values in my variables, but I need them in openHAB. So I created three number Items in the openHAB backend. The handover of the values to openHAB is also realized in PHP using the REST API:

The important part in the function is the declaration of the $data var as string, otherwise the file_get_contents() -function will fail.

For the cron, i create a small shell script called traffic.sh:

Within the crontab I scheduled a job every five minutes:

For the integration in openHAB i create a new frame with two text-widgets, like shown below:

habmin_sitemap

And this is the result in the GreenT UI:

openhab_travel_time

That’s it!

  1. Hi Sven,
    Great idea! I would like to test implenting this in my openHAB as well but I have never worked with JSON script so could you give me some support of where to create those scripts / parts?

    functions.php and the content of it (mentioned below functions.php) should be clear but where to put:

    This in openHAB ?
    if ($travelDurationTraffic != 0) {
    sendCommand(“GL_Traf_Work_TimeNormal”,$travelDuration);
    sendCommand(“GL_Traf_Work_TimeTraffic”,$travelDurationTraffic);
    sendCommand(“GL_Traf_Work_KM”,$travelDistance);
    }
    and the JSON part?

    • Sven Kolodziej

      Hi,

      all these scripts are written in PHP, in the end of today i will provide my script as an download, maybe then it is more clear how to implement the scripts.

      Some additional info, you need the php-cli & php-curl package on your server.

      Regards,
      Sven

  2. Hi Sven,

    Yes, that would really be helpful. I am able to extract the Information of BingMaps by PHP already but struggle how to forward it to openHAB. Any samples would be great!!

    Regards,
    Paul

  3. Hi Sven,

    i found a easier way with the http binding.

    openhab.cfg:

    http:bingmapstowork.url=http://dev.virtualearth.net/REST/V1/Routes/Driving?o=json&wp.0=XXXGPSXXX&wp.1=XXXGPSXXX&avoid=minimizeTolls&optmz=timeWithTraffic&key=XXXAPI-KEYXXX
    http:bingmapstowork.updateInterval=3600000

    items/default.items

    Number traffic_to_work_distance "Distanz zur Arbeit [%.1f km]" { http="<[bingmapstowork:10000:JS(trafficdistance.js)]" }
    Number traffic_to_work_duration "Fahrzeit zur Arbeit [%.1f min]" { http="<[bingmapstowork:10000:JS(trafficduration.js)]" }
    Number traffic_to_work_durationreal "Fahrzeit zur Arbeit inklusive Verkehr [%.1f min]" { http="<[bingmapstowork:10000:JS(trafficdurationreal.js)]" }
    Number traffic_to_work_duration_delay "Verzögerung um [%.1f min]" { http="<[bingmapstowork:10000:JS(trafficdelay.js)]" }
    String traffic_to_work_road "Route über [%s]" { http="<[bingmapstowork:10000:JS(trafficroad.js)]" }

    transform/trafficdistance.js

    JSON.parse(input).resourceSets[0].resources[0].travelDistance;

    transform/trafficduration.js

    (JSON.parse(input).resourceSets[0].resources[0].travelDuration)/60;

    transform/trafficdurationreal.js

    (JSON.parse(input).resourceSets[0].resources[0].travelDurationTraffic)/60;

    transform/trafficroad.js

    (JSON.parse(input).resourceSets[0].resources[0].routeLegs[0].description);

    transform/trafficroad.js

    (JSON.parse(input).resourceSets[0].resources[0].travelDurationTraffic)/60-(JSON.parse(input).resourceSets[0].resources[0].travelDuration)/60;

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">