Check Traffic with a Scheduler-Based Logic App

The following article takes you through steps that show how you can build a logic app with a scheduler trigger, which runs every weekday and checks the travel time, including traffic, between two places. If the time exceeds a specific limit, the logic app sends email with the travel time and the extra time necessary for your destination. (Original post by Esther Fan. Big thanks!)

Prerequisite

So what are the steps involved?

  • Create a blank logic app.
  • Add a trigger that works as a scheduler for your logic app.
  • Add an action that gets the travel time for a route.
  • Add an action that creates a variable, converts the travel time from seconds to minutes, and saves that result in the variable.
  • Add a condition that compares the travel time against a specified limit.
  • Add an action that sends email if the travel time exceeds the limit.

Create Resource Group (optional)

  • Create a Resource Group, so that you can manage your resources easily (optional).

Create Logic App

  • In your resource group. Click Add.
  • Search for Logic Apps, select the relevant option and click Create.

Add Scheduler Trigger

Every logic app must start with a trigger, which fires when a specific event happens or when new data meets a specific condition.

  • On the Logic App Designer, search and add Schedule – Recurrence.
  • Optionally, Click the ellipses (…) button, and select Rename. Rename the trigger with this description “Check travel time every weekday morning”.
  • Inside the trigger, choose Show advanced options.
  • Provide the schedule and recurrence details for your trigger as shown:

This trigger fires every weekday, every 1 minute, starting at 7:00 AM and ending at 5:00 PM. You can choose shorter interval, or your mail box will be flooded . The Preview box shows the recurrence schedule.

For more information, see Schedule tasks and workflows and Workflow actions and triggers.

NOTE: To hide the trigger’s details for now, click inside the shape’s title bar. Also, save your logic app at regular intervals.

Get the Travel Time for a Route

Add an action that gets the travel time between two places. Logic Apps provides a connector for the Bing Maps API so that you can easily get this information. Before you start this task, make sure that you have a Bing Maps API key as described in prerequisites.

  • In the Logic App Designer, under your trigger, choose + New step > Add an action.
  • Search for “maps”, and select this action: Bing Maps – Get route.
  • If you don’t have a Bing Maps connection, you’re asked to create a connection. Provide these connection details and choose Create. Provide a Connection Name and API Key.
  • Optionally rename the action with this description: “Get route and travel time with traffic”.

For more information about these parameters, see Calculate a route.

Create Variable to Store Travel Time

By default, the previous Get route action returns the current travel time with traffic in seconds through the Travel Duration Traffic field. By converting and storing this value as minutes instead, you make the value easier to reuse later without converting again.

  • Under the Get route action, choose + New step > Add an action.
  • Search for “variables” and select this action: Variables – Initialize variable.
  • Optionally rename this action with this description: “Create variable to store travel time”.
  • To create the expression for the Value field, click inside the field so that the dynamic content list appears. If necessary, widen your browser until the list appears. In the dynamic content list, choose Expression.
    • In the expression editor, enter this expression: div(,60).
    • Put your cursor inside the expression between the left parenthesis (() and the comma (,). Choose Dynamic content.
    • In the dynamic content list, select Travel Duration Traffic.
    • After the field resolves inside the expression, choose OK.
    • The result would be something like this: div(body(‘Get_route_and_travel_time_with_traffic’)?[‘travelDurationTraffic’],60)

Compare Travel Time with Limit

Now, add a condition that checks whether the current travel time is greater than a specific limit.

  • Under the previous action, choose + New step > Add a condition.
  • Optionally rename the condition with this description: “If travel time exceeds limit”
  • Build a condition that checks whether travelTime exceeds your specified limit as described and shown here:
  • Inside the condition, click inside the Choose a value box, which is on the left (wide browser view) or on top (narrow browser view).
  • From either the dynamic content list or the parameter list, select the travelTime field under Variables.
  • In the comparison box, select this operator: is greater than
  • In the Choose a value box on the right (wide view) or bottom (narrow view), enter this limit: 10

Send Email When Limit Exceeded

Now, add an action that emails you when the travel time exceeds your limit. This email includes the current travel time and the extra time necessary to travel the specified route.

  • In the condition’s If true branch, choose Add an action.
  • Search for “send email” and select the email connector and the “send email action” that you want to use. In this example, I have selected a personal account using Gmail – Send email. It will ask you to authenticate. You can also use Outlook.com for personal live accounts and Office 365 Outlook for Azure work or school accounts.
  • Optionally rename the action with this description: “Send email with travel time”
  • In the To box, enter the recipient’s email address. For testing purposes, use your email address.
  • In the Subject box, specify the email’s subject, and include the travelTime variable.
    • Enter the text “Current travel time (minutes):” with a trailing space.
    • From either the parameter list or the dynamic content list, select travelTime under Variables.
  • In the Body box, specify the content for the email body.
    • Enter the text “Add extra travel time (minutes):” with a trailing space.
    • If necessary, widen your browser until the dynamic content list appears. In the dynamic content list, choose Expression.
    • In the expression editor, enter this expression so that you can calculate the number of minutes that exceed your limit: sub(,15).
    • Put your cursor inside the expression between the left parenthesis (() and the comma (,). Choose Dynamic content.
    • Under Variables, select travelTime.
    • After the field resolves inside the expression, choose OK.

Save your Logic App.

Run your Logic App

To manually start your logic app, on the designer toolbar bar, choose Run. If the current travel time stays under your limit, your logic app does nothing else and waits for the next interval before checking again. But if the current travel time exceeds your limit, you get an email with the current travel time and the number of minutes above your limit. Here is an example email that your logic app sends:

Obviously, you can make a better email body than shown below. But I hope you get the idea

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.