Help with transforming google maps distance matrix API data

Hey everyone, I’m trying to extract the travel time to work from the google maps distance matrix API.

I’ve got the correct call to the API which returns data like this…

{
   "destination_addresses" : [ "xxxxxxx, New Zealand" ],
   "origin_addresses" : [ "xxxxxx, New Zealand" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "25.4 km",
                  "value" : 25445
               },
               "duration" : {
                  "text" : "25 mins",
                  "value" : 1481
               },
               "duration_in_traffic" : {
                  "text" : "23 mins",
                  "value" : 1401
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

I’m doing this based on some example I saw somewhere on Google+ but I think my issue is when I try and use a transformation JS script. I’ve not created a JS transformation script before so hopefully it’s an easy fix for someone who knows what they are doing.

This is all I found on Google+ and they said it should go on the JS script file…

JSON.parse(input).rows[0].elements[0].duration_in_traffic.value / 60;

Could someone help me work out what the entire contents of the JS file should be>?

I have exactly this in my transform/fahrzeit.js file:

JSON.parse(input).rows[0].elements[0].duration_in_traffic.value / 60;

@TommySharp what’s actually going wrong?

This what my Item looks like…

Number TTW_Sonay “Sonay TTW [%s]” (GroupOutdoors) { http="<[https://maps.googleapis.com/maps/api/distancematrix/json?origins=OriginAddressHere&destinations=DestinationAddressHere&key=GoogleAPIKeyHere&language=en&departure_time=now:300000:JS(ttw_trafficTimesNumber.js)]"}

And then this is the entire content of the ttw_trafficTimesNumber.js file that I have put in the transform folder.

JSON.parse(input).rows[0].elements[0].duration_in_traffic.value / 60;

The OpenHAB error log has stuff like this in it now…

2016-06-11 16:10:37.053 [ERROR] [.service.AbstractActiveService] - Error while executing background thread HTTP Refresh Service
java.util.UnknownFormatConversionException: Conversion = 'R’
at java.util.Formatter$FormatSpecifier.conversion(Unknown Source) ~[na:1.8.0_60]
at java.util.Formatter$FormatSpecifier.(Unknown Source) ~[na:1.8.0_60]
at java.util.Formatter.parse(Unknown Source) ~[na:1.8.0_60]
at java.util.Formatter.format(Unknown Source) ~[na:1.8.0_60]
at java.util.Formatter.format(Unknown Source) ~[na:1.8.0_60]
at java.lang.String.format(Unknown Source) ~[na:1.8.0_60]
at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:136) ~[na:na]
at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:156) ~[na:na]
at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:173) ~[na:na]

I am guessing that OriginAddressHere, DesintationAddressHere or GoogleAPIKeyHere contain a % or a $ followed shortly thereafter with the letter R. If so, try to change them so there is no % or $ in the URL (possibly meaning to generate a new Google API key).

That @watou, that seems to be the cause… It seems to the the spaces in my address which are causing the issue. Spaces in the URL are represented as %20

Is there any trick to making this work?

Have you tried replacing the URL-escaped spaces %20 with regular spaces? It might make sense to add an option to the HTTP binding to disable the string formatting around the line in your stacktrace, so this situation can be resolved without having to change the URL, but in the meantime, will regular spaces work?

Yeah google API says it’s an invalid URL…
How is the best place to propose a change to the HTTP Binding to resolve this?

In the meantime I could possibly get by with a GPS location rather than street address maybe…

Hi Tommy, please create a new issue, referencing the collision between URLs that have URL escapes, and this line of code (and possibly others) that assumes that the URL does not accidentally contain formatter marks. A simple solution in code might be to just catch and ignore the java.util.UnknownFormatConversionException (but maybe add a DEBUG log entry in case the user means to do replacements but got the format wrong).

Hi again, I’ve made a test JAR to replace your HTTP binding. To use it, add format=false to http.cfg, or http:format=false to openhab.cfg. This will cause the binding to skip its attempt at replacing into the URL the current time or state string, and thereby ought to solve what you encountered.

I had considered just silently ignoring the exception you encountered, but that doesn’t help the situation where your URL accidentally contains a valid format string, resulting in its being successfully substituted, but that’s not what the user wants.

Would you give it a try and report back, @TommySharp?

Is it okay to use it in my 1.7 OpenHAB?

I believe it should work on openHAB 1.7.

PR created.

@TommySharp: any feedback on the test JAR above and setting http:format=false in your openhab.cfg file?

Thanks!

Maybe I’m doing something wring but this is what I’m getting in the logs now…

2016-06-16 12:33:35.644 [ERROR] [.service.AbstractActiveService] - Error while executing background thread HTTP Refresh Service
java.util.UnknownFormatConversionException: Conversion = 'R’
at java.util.Formatter$FormatSpecifier.conversion(Unknown Source) ~[na:1.8.0_60]
at java.util.Formatter$FormatSpecifier.(Unknown Source) ~[na:1.8.0_60]
at java.util.Formatter.parse(Unknown Source) ~[na:1.8.0_60]
at java.util.Formatter.format(Unknown Source) ~[na:1.8.0_60]
at java.util.Formatter.format(Unknown Source) ~[na:1.8.0_60]
at java.lang.String.format(Unknown Source) ~[na:1.8.0_60]
at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:136) ~[na:na]
at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:156) ~[na:na]
at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:173) ~[na:na]
2016-06-16 12:33:35.675 [INFO ] [.myopenhab.internal.MyOHClient] - Connected to my.openHAB service (UUID = xxxxxxxxxxxxxxxxxxxxxxxxxx, base URL = http://localhost:8080)
2016-06-16 12:33:36.373 [WARN ] [i.internal.GenericItemProvider] - Attempted to register a second BindingConfigReader of type ‘http’. The primaraly reader will remain active!
2016-06-16 12:33:36.386 [INFO ] [.service.AbstractActiveService] - HTTP Refresh Service has been started
2016-06-16 12:33:36.395 [ERROR] [.service.AbstractActiveService] - Error while executing background thread HTTP Refresh Service
java.lang.ClassCastException: org.openhab.binding.http.internal.HttpGenericBindingProvider cannot be cast to org.openhab.binding.http.HttpBindingProvider
at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:145) ~[na:na]
at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:156) ~[na:na]
at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:173) ~[na:na]

I left my item using the GPS location in the HTTP url, so no spaces and also removed the setting you wanted me to add to the config file.

So all I have now is your new JAR file and I still get an error message.

2016-06-16 13:29:04.876 [ERROR] [.service.AbstractActiveService] - Error while executing background thread HTTP Refresh Service
java.lang.ClassCastException: org.openhab.binding.http.internal.HttpGenericBindingProvider cannot be cast to org.openhab.binding.http.HttpBindingProvider
at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:145) ~[na:na]
at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:156) ~[na:na]
at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:173) ~[na:na]

So I’m going to revert back for now as it’s working okay with the GPS locations.

The openHAB server could be confused at that point after both JARs were present and might need to be stopped and restarted (mixing old and new classes maybe). Had you started the server with only the new JAR in place and the old one removed from addons?

I had renamed the original 1.7 file to have an extension of “raj”, that should have excluded it?

I would think so, but just moving it out of addons altogether would remove the question.

Hi @TommySharp, could you try this again? The test JAR is here, and if you remove your HTTP binding JAR from addons and put this one in its place, and make sure your active openhab.cfg has the line

http:format=false

Your URLs that contain character substrings like %20R should be ignored. I would like to verify this PR is good, so your help is much appreciated!