Automatically change your Microsoft Teams working location (with Home Assistant!)

Loryan Strant
REgarding 365
Published in
6 min readJun 5, 2023

--

In the past couple of weeks, Microsoft has finally made available features they presented over 6 months ago; namely the ability to set your work location.

First this was available in Outlook on the web (formerly known as Outlook Web Access, or OWA), and then last week in Microsoft Teams.

This is a very handy solution in the hybrid world of work, and many people are now well established in a rhythm for when they are in the office vs. when they are working from home (or elsewhere).

Personally, I only work from the office on Wednesday — so it made it quite easy for me to schedule:

The Challenge

But… what if I change the day I come in? Or what if I decide to come in an extra day one week? Or two days?

How will the system handle that???

I mean, I could simply remember to set my location at the time in either Outlook on the web or the Microsoft Teams desktop client to show where I am that day… but what if I forget?

Or I could wait for Microsoft Places to one day be released and help with this world-ending issue.

But what if my colleagues, thinking they’re going to have an online meeting with me, aren’t aware that I’m sitting a few desks from them, and we could in fact be meeting in person?

Can you imagine??? Surely this would cause a tear in the fabric of space and time!!!

Thankfully, if you want to go full nerd (as I like to), then you can automate this — especially if you use Home Assistant.

Even if you don’t, you can still achieve the same luxurious automation but with a different trigger method.

Let’s have a look, shall we?

(Also, while you’re reading on… have a think about how much time I’ll save by having this automation work vs. how much time it took me to figure this out, build it, test it, and write this blog.)

The Solution

Scouring through Microsoft Graph documentation I couldn’t see anywhere to set working location, and realistically the average user wouldn’t be able to do anything using Graph without IT admin involvement.

Moving on, I inspected the web traffic when setting the working location in Outlook on the web and found that I would most likely need to perform the update using PowerShell — another dead-end.

Doing the same for Microsoft Teams in a browser, I noticed that changing the working location was calling a web-based URL that I’d seen before…

And then it hit me! I’d previously used that URL to set my Microsoft Teams status message to show what I was playing on Spotify, or to have Alexa change my presence to Do Not Disturb. (Both crucial functions, make sure you check them out!)

So, using the same approach I should be able to set my work location as the endpoint called is https://emea.presence.teams.microsoft.com/v1/me/workLocation.

(NOTE: my account is in EMEA, so you may need to change the start of the URL to something else — the previous blog posts explain this.)

I also discovered that payload being sent when I changed my work location took the form of: {location: 1, expirationTime: “Mon, 05 Jun 2023 13:59:59 GMT”}

Working through the different options, I’ve mapped out that:

0 = Clear
1 = Office
2 = Remote

Now that we’ve worked what to send and how to send it, let’s head into the kitchen!

Changing my working location through a Power Automate workflow

Here’s how the workflow works:

  1. Trigger the workflow with a payload of my location
  2. Add 12 hours to the current time of the workflow run (to give sufficient time to last until the end of the day, on the chance I may come in early or leave late)
  3. Convert the time into full date/time pattern as that’s what is required for the request
    (At this point, I know that the previous two steps could be achieved in a single compose but I’m not exactly paying by the action here, so am happy to “wear the cost” of extra actions in exchange for visual simplicity.)
  4. Initialise the variable for the location, as this is set within the Switch
  5. The Switch sets the location to a numeric value that corresponds with “Office” or “Remote” in code, based on where Home Assistant tells it to
  6. Send the HTTP request to the Microsoft Teams presence endpoint by cheating and using a SharePoint HTTP request action with our location variable and dynamically generated end time

Expanding out the trigger, I have included a JSON Schema for the payload:

Expanding out the time calculations:

While I’m in GMT+10, I noticed in my research that it uses GMT time in the payload — so I kept it that way.

Initialising the variable and putting in a switch condition for the value:

And the final step, hijacking the “Send a HTTP request to SharePoint” action to send the request to the Microsoft Teams presence endpoint:

It’s all very well and good to have figured this out, but how will I trigger it automatically and send the right location?

I thought initially of a scheduled workflow instead that would somehow retrieve the location of my Windows device from Microsoft Graph — but again, I didn’t want to abuse my Global Admin rights to get that data.

Instead, I realised that I already had location states set in Home Assistant based on map zones — so when I’m within 100m of my office the state of the Loryan Actual Location entity I’ve configured is set to “Work”.
(I use this entity as an amalgam of both GPS-based location tracking, as well as room-based tracking.)

Triggering this from Home Assistant

In the configuration.yaml file I configured a RESTful command to call my Power Automate workflow trigger and send the payload with my location:

If I wanted to manually call this service, here’s what it would look like:

So, over in my automation I configure it to be triggered based on the entity location being set to “Work”:

Summary

And there you have it.

The moment I walk within range of the office, even on a day when I’m scheduled to be working from home — these two automations will set my work location to show that I’m in the office, just in case nobody else in our single office space was aware of my presence.

So worthwhile, so valuable, so much time saving and experience improving!

Originally published at Loryan Strant, Microsoft 365 MVP.

--

--

Microsoft 365 MVP, author, cloud guy, thought opinionater, public speaker, distance gazer. Passionate about productivity and life/work balance.