Home Assistant Guide

Simple tutorials for powerful automations

Ask Home Assistant for Travel Times Using Your Voice

Checking travel times by voice is a built-in feature of assistants like Google and Alexa - and it's something I personally use all the time. This guide will show you how to add the same travel time feature to your own Home Assistant voice assistant, so you can get real-time answers (including current traffic) just by asking.

This is part of a series of "Feature" articles designed to help you turn your Home Assistant voice assistant into a truly useful alternative to Google, Alexa, or Siri.

Why Use Voice for Travel Times?

  • Get answers quickly: Just ask and hear the answer, without picking up your phone or opening Google Maps.
  • Plan your day: Know how long your commute will be before you leave, or check traffic before picking someone up.
  • Automate your smart home: Trigger routines or send notifications if travel time is longer than usual.

What You Need

  • Home Assistant running on your network
  • Extended OpenAI integration set up in Home Assistant
    • (If you haven't set this up yet, check out the dedicated article on this site.)
  • A Google Cloud account and a Google Maps API key (explained below)

Getting a Google Maps API Key (Step-by-Step)

  1. Create or log in to your Google Cloud Platform account:
    Go to Google Cloud Console. You'll need a Google account to sign in.
  2. Create a new project:
    Click the project dropdown at the top left and select "New Project." Give it a name (like "Home Assistant Travel Time") and click "Create."
  3. After creating the project, you might see a popup letting you select it. If so, click "Select Project." If not, use the same top-left project picker to choose your new project.
  4. Enable billing:
    Google Maps APIs require billing. Set up billing by following the prompts in the Billing section of the Cloud Console. You will need to add a credit/debit card, but Google gives you free monthly usage (see below).
  5. Enable the Distance Matrix API:
    In your project, go to the left sidebar, click APIs & Services → Library. Search for "Distance Matrix API", click it, and hit "Enable."
  6. Create API credentials (API key):
    In the left sidebar, click APIs & Services → Credentials. Click "Create Credentials" and choose "API key."
    • Tip: After enabling the Distance Matrix API, you may see a popup right away with your new API key and restriction options. Copy your key - you'll use it in Home Assistant.
  7. (Recommended) Restrict your API key:
    Set an API restriction to "Distance Matrix API" - this limits your key so it only works for travel time requests and keeps it safe if it's ever leaked.
    Tip: You can also add an IP address restriction, but this is not recommended unless you have a static IP address at home. For most users, API restriction alone is secure and hassle-free.
  8. Google Maps API Pricing and Free Quota

  • Free tier: Google provides $200/month free usage for most Maps APIs, including the Distance Matrix API.
    • This covers roughly 10,000 requests per month for basic travel time queries. Most home users will never hit the limit.
  • Beyond free tier: Each additional request costs a small amount (currently $5 per 1,000 extra requests for Distance Matrix API – check Google Maps pricing for up-to-date rates).
  • Billing required: Even if you only use the free quota, Google requires you to set up billing.

The Spec Function for Voice-based Travel Times

Once you have your API key, you'll want to set up a function for Extended OpenAI so Home Assistant can call the Google Maps Distance Matrix API whenever you ask for travel time. To do this, go to "Settings" in Home Assistant, then "Devices & services" -> "Extended OpenAI Conversation". Click the edit icon (cog wheel), and then paste the following code into it. Be sure to replace "API_Key" with your actual key!

- spec:
    name: check_travel_time
    description: Check travel time between two locations using Google Maps Distance Matrix API.
    parameters:
      type: object
      properties:
        origins:
          type: string
          description: The starting location.
        destinations:
          type: string
          description: The destination location.
      required:
      - origins
      - destinations
  function:
    type: rest
    resource_template: "https://maps.googleapis.com/maps/api/distancematrix/json?origins={{ origins }}&destinations={{ destinations }}&key=API_KEY&mode=driving&departure_time=now"
    value_template: >-
      {% set el = value_json.rows[0].elements[0] %}
      {% if el.duration_in_traffic is defined and el.duration_in_traffic %}
        {{ el.duration_in_traffic.text }}
      {% elif el.duration is defined and el.duration %}
        {{ el.duration.text }}
      {% else %}
        No travel time found
      {% endif %}
    

What Does Each Part Do?

  • name: This is what you (or your assistant) will call the function. In this case, "check_travel_time."
  • description: Explains what the function does - checks travel time between two places.
  • parameters:
    • origins: Where you are starting from. (For example, "Home" or "123 Main St, City"). This will be filled in by the voice assistant, you just leave it as {{ origins }} in your code.
    • destinations: Where you want to go. (For example, "Work" or "Airport, City"). As with origins, just leave {{ destinations }} in your code.
  • function / type: rest: This means Home Assistant will call a web API (the Google Maps Distance Matrix API) to get the travel time.
  • resource_template: This is the web address used to call the API. {{ origins }} and {{ destinations }} are replaced by what you ask. Replace API_KEY with your real key. The parameters set it to driving mode and ask for current traffic ("departure_time=now").
  • value_template: This is a bit of code that takes the result and extracts the travel time. It will:
    • Show the travel time with current traffic (duration_in_traffic) if available.
    • If not, show the typical travel time (duration).
    • If nothing is found, it will say "No travel time found."

What's Next?

Once you've added this function, you'll be able to ask your Home Assistant voice assistant questions like:

  • "How long will it take to drive from London to Oxford?"
  • "What's the travel time to the airport?"

Home Assistant will get live, accurate answers straight from Google Maps!