DEV Community

Barret Blake
Barret Blake

Posted on • Originally published at barretblake.dev on

Automatically Adding Travel Time To Your Calendar

Even in this world of video meetings on Teams, Zoom, or wherever, there are still occasions when you need to travel to an onsite meeting somewhere. In these cases, it can be useful to automatically block out travel time before and after to ensure that your calendar reflects when you’re not going to be in the office. Power Automate makes it easy to add some travel time to your calendar.

Note: As with most of the more useful connectors, some of the connectors mentioned here will require a paid Power Automate account.

This flow makes use of the following connectors:

Bing Maps

The first thing you’re going to need is a developer account for Bing Maps. Don’t worry. It’s easy to sign up and for limited use, it’s free. Head over to the Bing Maps Developer Portal and create an account. Once you have created your account, open the “My keys” menu under the “My account” header. Click to create a new key and select a key type of “Basic” and an application type of “Dev/Test”. This is the free account tier and will provide plenty of access for the purpose of this flow.

Bing Maps Developer Portal create key menu

Once you have created the key, copy it down. We’ll need it for our Power Automate connector.

Flow Trigger

Now we can go ahead and start creating our flow in Power Automate. You’ll want to trigger the flow whenever an event gets added to your calendar. In our case, we’re going to use Outlook.com’s “When a new event is created (V2)” trigger.

Trigger for when a new event is created

Select the calendar ID of the calendar you want to have to trigger the flow. The default is just called “Calendar” for most Outlook types. We don’t need to detail any of the other parameters.

The next thing I’m going to add is a variable to use to calculate the amount of travel time to block. You don’t strictly need the variable. I’m just breaking it out into multiple steps to demonstrate various pieces of logic. This will be an integer variable and I’ll call it timeDiff.

Condition

The next step I’m going to add is a Condition action. We only want to try and add travel time if the meeting invite includes an address in the location field of the event. The exact conditions will vary depending on your needs, but the following conditions are a good starting point.

  • Location text length > 0
  • Location does not contain the word Zoom
  • Location does not contain the word Teams
  • Location does not contain the word Webex

We can check the length of the location field with the following expression:

length(triggerOutputs()?['body/Location'])
Enter fullscreen mode Exit fullscreen mode

The final result will look something like this:

Condition action list of conditions to check if we should proceed

Bing – Get Route

Under our “If yes” branch, we’re going to add a few more steps to calculate our travel time and then add it to our calendar. Add an action in the box and search for the Bing Maps “Get route” action. When you select this action you’ll be asked to create a new connector if you don’t already have one. In this connector, you’ll need to give it a name and add the API key that you created in the Bing Maps Developer Portal.

For this action, you will pass in a number of parameters such as your start and end addresses. The result that comes back will be a description of your journey in JSON data format. It will include quite a few details, like step-by-step driving directions. But we only care about the travel time value (called Travel Duration) for this demo. This value gets returned as seconds. For example, for a test I ran, driving from my office to the Columbus airport returned 1847 seconds.

For Waypoint 1 you’ll enter your starting location. Put in the address of wherever you usually work from. You can get fancy and come up with ways of calculating where you might be starting from, but we’re keeping it simple for now. For this demo I put in the office location I work from when I’m in the office.

For Waypoint 2 we’re going to pull the location value from the trigger event. When this step runs, the value of location will be passed into Bing as your destination. If Bing Maps can’t figure out what the destination actually is based on that value, it will throw an error and your flow will fail. You can either add some appropriate error tracking or just leave it like this because in this case, if we can’t get a route from Waypoint 1 to Waypoint 2 then we don’t want to add anything to our calendar.

For Travel mode we’ll want to set “Driving”.

You can change the other default values as appropriate for your driving habits. For this demo, I changed Optimize to timeWithTraffic (which will account for typical traffic patterns). Unfortunately, the “Get route” action doesn’t let you pass in the date and time of when you will be traveling, so the traffic delays will be calculated based on the time that you make the request. But, it’s better than nothing.

The other default I changed is the Distance unit. It defaults to kilometer, but being in the US myself I changed it to Mile. It really doesn’t make a difference for this demo, but if you wanted to get a little fancier and include something like the distance you will drive in the travel time calendar block, then it will be needed.

Add Travel Time To My Calendar

The last few steps will calculate how much travel time to add and add that as an event on my calendar that will appear immediately prior to the trigger event. First, we’re going to calculate how many minutes our travel time will include. We’re going to calculate this as a negative number of minutes (I’ll explain why in a moment). We’re also going to add a buffer of 15 minutes to allow time to walk to my car from my office, time to walk from the car to the meeting location at the destination, and a couple of extra minutes to allow for traffic variations. This isn’t a perfect system, but it’s a good place to start.

Add a “Set variable” action and we’ll set the value of our timeDiff variable to the following expression:

sub(sub(0,15), div(outputs('Get_route')?['body/travelDurationTraffic'], 60))
Enter fullscreen mode Exit fullscreen mode

This takes the travelDurationTraffic value, divides it by 60 to convert seconds into minutes, then includes our extra 15 minutes of buffer. This gives us a negative value of minutes. In my example above of traveling to the airport, I get a value of 46 minutes.

Now we need to convert this number of minutes into a dateTime value. Here’s why we got our value as a negative number. Power Automate has an “addMinutes” function, but not a subtract minutes function. So we’ll take the start time of the original trigger event and then add that number of negative minutes to get the starting time of our Travel Time event. The following expression gets us what we want:

addMinutes(triggerOutputs()?['body/Start'], variables('timeDiff'))
Enter fullscreen mode Exit fullscreen mode

Add a Compose action to the flow and set the Inputs to that expression. This will give us a dateTime value. Now, all we need to do is add a new event to our calendar to block off that travel time.

Add an Outlook.com “Create event (V3)” action to our flow. Select the Calendar id of the calendar to add your event. As before the default Outlook calendar id is “Calendar”. Give your event a subject. I just used the value “TRAVEL TIME” so it stands out a bit.

For the Start time of the event, use the output from our previous Compose action. And for End time we’ll use the starting time of our trigger event.

None of the other fields are required, but you could add things if you wanted. For example, you might include the distance calculated. You might also include the returned step-by-step travel directions in case your GPS fails to work the day of. Who knows what you might come up with?

Conclusion

That’s all you need to add some travel time to your calendar. You can enhance this with some other ideas.

  • Add another step to travel back to the office
  • If the start time is the beginning of the day, start from home instead of the office
  • If the event is all day, skip travel time entirely

There are all kinds of things you might add depending on your circumstances, but this is more than enough to get you started. A good way to get help get your calendar better organized.

The post Automatically Adding Travel Time To Your Calendar first appeared on Barret Codes.

Top comments (0)