WARNING: Users who have this template already installed for versions of OH 5.1 and before, after removing the old template and installing this new one, open your rule(s), click on the code tab, and change the ruleTemplate property to ohrt-driveTime before regenerating the rule.
This rule template uses the Waze RoutingManager API to query for and calculate the time and distance to drive between two points. My original idea for this template was to get an alert in the mornings so we know when to leave early because there’s yet another accident on the interstate.
The template has four properties:
starting coordinates in “lat,lon” format
ending coordinates in “lat,lon” format
Number:Time Item that will be updated with the current driving time
Number:Length Item that will be updated with the current driving distance
This rule template does not have any triggers or conditions. To avoid hammering the Waze servers this rule remains inert until you further add your own triggers and conditions. I recommend a polling period in minutes at the most and adding conditions so the rule does not run at times when it’s not needed.
If more than one route is desired, create multiple rules using this template, one for each route.
When regenerating your rule from the template, don’t forget to restore your trigger. Alternatively you can create a separate rule that calls this rule and implement the trigger there.
Language: JS Scripting
Dependencies:
JS Scripting add-on installed: Version: 5.18.2+
Future versions may depend on OHRT
Changelog
Version 1.0
migrated to the YAML file format for OH 5.2
use the wrapper
standardize the logger names
Version 0.4
the region property had two options with the same mapping which caused problems, those two options were merged to avoid the problem
Version 0.3
changed to use Waze’s new URLs
minor changes to make URL generation based on region easier
moved code to github
Version 0.2
added property to choose region to avoid Internal Error problems for users outside the US
changed the logger name and added more logging at the debug and trace levels
Hello and thank you for your template. I’m using it but I noticed a very strange bahaviour: essentially, the driving time reported is always underestimated, some times by a factor of 2. Setting the same origin/destination ina an actual browser reports a time uch higher. I’m connecting from EU. Any hints about what is going wrong?
Not really. It’s all handled on Waze’s end. All that gets passed to Waze are the two locations and it calculates the drive time and distance based on that. I have no more control over what Waze does with it beyond that.
In fact, the problem I had with waze was it was not taking into consideration historical traffic averages. So it was underestimating my travel time because it didn’t take into consideration how the traffic builds up after I leave the house. So I created Driving Time and Distance using Google Maps [5.2.0.0,5.9.9.9] because Google’s API does take that historic data into consideration.
However, dealing with the data isn’t so easy. If the rule runs now, it calculates the time and distance if I leave in 20 minutes. So to get an alert right before it’s time to leave I need to set a timer for the alert for 18 minutes. But things can change in the mean time so I have to keep assessing and rescheduling the timer right up until it’s time to leave.
Thank you for the answer. I’m using it just for info, not to set reminders, nonetheless using traffic history is obviusly needed to do meningful predictions. Still, the Waze behaviour is rather odd. I’m now testing the Google implementation. BTW, there should be something wrong with the repository of the driving time with Google template, I installed the template via config files. The GUI-triggered installation fails with : ERROR
org.openhab.core.addon.marketplace.internal.community.CommunityRuleTemplateAddonHandler
Failed to add rule template from the marketplace: [Template] java.lang.IllegalArgumentException: Cannot construct instance of org.openhab.core.automation.dto.RuleTemplateDTO (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (’ <…
I’m not sure what to make of that error. I don’t have the same error on my side so it’s going to be a challenge to diagnose and fix. In the mean time you should be able to download the raw yaml file and place it in $OH_CONF/yaml and OH will load the template from the file instead of trying to install it from the marketplace.
Waze seems to have restricted automated access, resulting in a 403 error on queries. While I get valid results with reconstructed in browser, scripted query runs into 403 [ERROR]:
SyntaxError: SyntaxError: Invalid JSON: <json>:1:0 Expected json literal but found <
<!doctype html><meta charset="utf-8"><meta name=viewport content="width=device-width, initial-scale=1"><title>403</title>403 Forbidden
^
Several hints suggest to help with added http headers, but I didn’t succeed yet.
How fast are you polling the API endpoint? It does not let you do it super fast and even if it’s slow it won’t let you poll like every five minutes all day long. You have to choose one or two one hour windows or so and then I only poll every five minutes. If you go too fast they will IP ban you for sure.
When I have time I’ll have a look at this. I’m in the progress of migrating this all over to N8N so as I do that I should figure out how to structure the URL and headers. Of course, if Waze is serious about blocking this access it might be a dead end.