This rule template uses the Google Maps API to query for and calculate the time and distance to travel between two points at a specific desired arrival time. This is distinct from the Driving Time and Distance using Waze [5.2.0.0;5.9.9.9] rule template which only calculates the travel time for leaving right now.
What drove the creation of this template is that I found that the alert rule I created would have us leave too late because the traffic builds up after we leave the house. It doesn’t take into account historic data to predict what the traffic may actually look like after I leave the house. Unfortunatelty Waze doesn’t provide access to this in their API but Google Maps does.
The template has eight properties:
| Property | Purpose | Default |
|---|---|---|
| Google Maps API Key | Allows access to the Google Maps services (see below). | |
| Start | Full address or lat,lon coordinates of the starting point | |
| Destination | Full address or lat,lon coordinates of the destination | |
| Target Arrival Time | Time you desire to arrive at the destination | |
| Look Ahead Window | Time in minutes to look ahead. for example, if I leave in 20 minutes how long will it take to get to the destination assuming traffic conditions that have me arrive at 7:50 am? | 20 minutes |
| Traffic Model Strategy | How Google should predict the traffic based on historic data. | Pessimistic |
| Travel Time Item | Number:Time Item that the rule will update with the calculated travel time. | |
| Travel Time Distance | Number:Length Item the rule will update with the calculated travel distance. |
This rule template does not have any triggers or conditions. To avoid hitting the Google Maps servers unnecessarily (you need to provide payment options to gain access to the API and have up to 5000 requests per day for free) I leave the triggers and conditions up to you. I recommend only polling up to once per minute (I use every two minutes) for the half hour window in which you normally need to leave only on the days when you commute to this location. Add conditions to skip holidays and such automatically.
Alternatively you can create a separate rule or rules that call the instantiated rule for the triggers. This makes regeneration of the template easier as you won’t have to recreate the triggers and conditions.
If more than one route is desired or more than one arrival time is desired, create one instance of the rule from the template for each.
There is a companion embedded Google Maps widget at Embedded Google Maps Widget which shows the route, traffic and current travel times. It can use the same Google Maps API Token.
Obtaining a Google Maps API Token
Step 1: Create a Google Cloud Account
- Open your browser and navigate to the Google Cloud Console.
- Sign in using your preferred Google account.
- If this is your first time using the platform, accept the Terms of Service.
- Note on Billing: Google requires a valid credit card to activate an account to prevent bot abuse. However, Google provides $200 in free credits every single month for Maps Platform APIs.
Step 2: Create a New Project
- Look at the top blue header bar. Next to the “Google Cloud” logo, click the Project Dropdown Menu (it will either show a default project name or a blank selector).
- A window will pop up. Click New Project in the upper-right corner.
- Name your project something recognizable, such as
openHAB. - Leave the “Organization” field as No organization or default, and click Create.
- Wait a few seconds for the notification bell icon to spin, then click the Project Dropdown again and select your newly created project.
Step 3: Enable the 4 Core APIs
You must manually enable the API engines before your key can request data from them.
- Click the Navigation Menu (the three horizontal lines ☰ in the top-left corner).
- Hover over APIs & Services and click on Library.
- Use the search bar in the middle of the screen to find and enable these four specific APIs one by one:
- Directions API (Required for drawing route lines inside the companion map widget)
- Distance Matrix API (Required for the rule to calculate the distance and travel times)
- Geocoding API (Required for translating text addresses into coordinates)
- Maps Embed API (Required for embedding the companion map widget)
- For each API, click its card in the search results, and click the blue Enable button. Use the browser’s back button to return to the library and search for the next one.
Step 4: Generate the API Key
- Open the left Navigation Menu (☰) again.
- Navigate to APIs & Services → Credentials.
- Click the + Create Credentials button located at the top of the main dashboard panel.
- Select API Key from the dropdown options.
- A pop-up box will appear displaying your brand-new API key string (e.g.,
AIzaSy...). Copy this key to your clipboard, but do not click close yet. Click the Edit Key link in the bottom right of that pop-up window to secure it immediately.
Step 5: Configure Key Restrictions
Because this key will be hardcoded into your frontend openHAB widget code (making it visible in web browser page sources), you must restrict it so it cannot be stolen and used on other expensive Google cloud services.
- On the Edit Key page, scroll down to the Application restrictions section.
- Leave this set to None. (Setting this to ‘Websites’ will break your backend openHAB rules because server-side Java requests do not send browser HTTP referrers).
- Scroll down to the API restrictions section and select the Restrict key radio button.
- Click the Select APIs dropdown menu that appears.
- Scroll through the checklist and check the boxes next to the four core APIs you enabled in Step 3:
- Directions API
- Distance Matrix API
- Geocoding API
- Maps Embed API
- Click OK to close the checklist dropdown.
- Click the blue Save button at the bottom of the page.
Your key is now live and fully locked down. You can copy-paste this single key directly into your new ohrt-futureDriveTime openHAB rule template configuration properties and your dashboard map widget parameters!
Language: JS Scripting
Dependencies:
- JS Scripting openhab-js version 5.21.0+
- Future versions may depend on OHRT
Changelog
Version 0.1
- initial release
