Display Microsoft Teams Presence Status in Home Assistant

by Danny
8 comments

READING TIME: 11 MINUTES

In this guide, I’m going to show you how to display your Teams presence status in Home Assistant. There are a few ways to configure this. Each method has certain advantages/disadvantages with restrictions for each method, but I’ve found Option 2 below to be the easiest to setup.

Displaying your Teams status (Busy, Available, Do Not Disturb, Away, etc) within Home Assistant is pretty awesome. It allows you to build automations that reference your status to do certain things.

For example, if your status is Busy, you could change the color of an RGB bulb to red so your kids or family members know you are on a call or in a meeting. If you answer a call, your music stops.

Here are three ways you could view your Microsoft Teams presence status inside Home Assistant:

  1. Use Hass Workstation Service. This is a program installed on your Windows computer that can get the attibutes of certain things on your computer. Microphone in use, webcam in use, etc.
  2. Powershell script that looks at the status of Microsoft Teams log file for presence changes. Requires Local Admin credentials on your computer, but works really well. (RECOMMENDED METHOD)
  3. Use Microsoft Graph API to get the status directly from Microsoft. Requires some permissions and configuration from your Office 365 tenant. (If you aren’t an O365 admin at your company, you probably don’t want to go this route.) While I do have permissions to set this up, I’m going to assume most of you do not, which is why I’m not showing you how to do this option.

I will be going over #1 and #2 in this guide. There is an excellent video from This Smart House for #3 here: https://www.youtube.com/watch?v=DUBvaNHpCIg


Option 1: Hass Workstation Service

I won’t go into this option too deep in this guide. I’ve already created a How To Install Hass Workstation Service tutorial that walks you through exactly how to set this up. Just follow that guide and add your webcam and microphone!

With this option, we are basically checking to see if your webcam or microphone is in-use, which usually means you are on a call or in a meeting.

Another advantage of using Hass Workstation Service is that it should work with any meeting client – Zoom, Webex, GoToMeeting, etc.

Even if you won’t use this option, I still highly recommend Hass Workstation Service simply because it allows you to control a bunch of things on your computer such as mute, volume up/down, restart, or locking your PC.


Option 2: Powershell Script to display Teams Status

Reminder: You’ll need to be a local administrator of your computer to configure this option.

For this option, we are using a Powershell script created by EBOOZ called TeamsStatus. This script is monitoring the Teams desktop application logfile located at %appdata%\Microsoft\Teams\logs.txt. From my experience, this script works really well & updates the Home Assistant sensors almost immediately.

Part of this setup involves creating a few sensors in your configuration.yaml file and copying a Powershell file to your computer. The sensors that get created are:

  • sensor.teams_status
  • sensor.teams_activity

The sensor.teams_status will display the availability (presense) of the Teams desktop application from your taskbar.

The sensor.teams_activity will display if you are in a call or not.

Here’s a quick gif showing how it works (status was changed by me from my Teams mobile app, and a call was placed from my Teams desktop client)


Step 1: Generate Long-Lived Access Token in Home Assistant

First, you’ll need to generate a long-lived access token. To do this, click your profile picture in Home Assistant:

Then, scroll to the bottom until you see the Long Lived Access Tokens section.

Click Create Token and name it TeamsStatus.

Copy the generated token to your clipboard and save it somewhere temporarily. It will not be shown again, so make sure to document it before closing the window.


Step 2: Create Sensors in configuration.yaml

Next, edit your configuration.yaml file and add the following sensors and input_text.

input_text:
  teams_status:
    name: Microsoft Teams status
    icon: mdi:microsoft-teams
  teams_activity:
    name: Microsoft Teams activity
    icon: mdi:phone-off
sensor:
  - platform: template
    sensors:
      teams_status: 
        friendly_name: "Microsoft Teams status"
        value_template: "{{states('input_text.teams_status')}}"
        icon_template: "{{state_attr('input_text.teams_status','icon')}}"
        unique_id: sensor.teams_status
      teams_activity:
        friendly_name: "Microsoft Teams activity"
        value_template: "{{states('input_text.teams_activity')}}"
        unique_id: sensor.teams_activity

Then restart Home Assistant from Configuration > Settings.


Step 3: Create Scripts folder for Powershell TeamsStatus script

Next, create a folder on your C:\ drive called “Scripts” folder. (C:\Scripts)

Navigate to this URL, click the green Code button. Then Download Zip. Download to your Downloads directory and then extract the contents to your C:\Scripts folder.

Make sure these files are in the root of the C:\Scripts folder. By default, it will extract them to C:\Scripts\TeamsStatus-main\TeamsStatus-main\. You need to copy those out C:\Scripts.

In C:\Scripts, right-click Settings.ps1 > Edit. This should open the file in Powershell.

Add your Home Assistant token, your Windows username, and the URL of Home Assistant. Then save the file.


Step 4: Run Powershell script as Admin

Next, start an elevated Powershell ISE window.

First, change directory to C:\Scripts:

cd C:\Scripts

Then paste this:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
Unblock-File .\Settings.ps1
Unblock-File .\Get-TeamsStatus.ps1
Start-Process -FilePath .\nssm.exe -ArgumentList 'install "Microsoft Teams Status Monitor" "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-command "& { . C:\Scripts\Get-TeamsStatus.ps1 }"" ' -NoNewWindow -Wait
Start-Service -Name "Microsoft Teams Status Monitor"

Click “Yes to All” on the Execution Policy Change window.


Step 5: Add Entity to Home Assistant

Next, simply add the add the sensor.teams_status and sensor.teams_activity entities to an Entities card in Lovelace.

If all goes well, it should look like this and report the correct status!


Start Powershell Script Automatically After Computer Reboots

So, now that we got everything working – the last step is to run this powershell script automatically after your computer restarts. Otherwise, you’ll get an “unknown” status like this, or it’ll retain the the status it last detected.

There are a few ways to do this, but using Windows Task Scheduler is the easiest route. I tried a few other methods to get it to get this script to launch (startup folder, batch file, etc), but most of those of those ended up showing a CMD window. This method will just run silently in the background.

Note: You’ll need to have Teams running, preferably in your taskbar!

  1. Search Windows for Task Scheduler
  2. Right-click Task Scheduler Library > Create Task
  3. Give it a name like “Start TeamsStatus”.
    1. Set to “Run whether user is logged in or not”
    2. Set to “Run with Highest privileges”
    3. Configure for Windows 10
  4. On the Triggers tab, set to At System Startup.
  5. Under Actions tab, choose Start a Program.
    1. Program/script: C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe
    2. Add arguments: C:\Scripts\Get-TeamsStatus.ps1

Now, the next time you reboot your computer, the script will start running automatically!

The only gotcha is that you also must have Teams running! If you don’t, then the script can’t find the log files it needs. If you have Teams running and it’s active in the taskbar, it updates the HA sensors quicker. However if it’s just running in the background, it’ll still update, but not quite as quickly.


Create Teams Presence Automations

The last step is to create an automation that will turn on/off a light if the status changes, or create an automation to turn on/off a light when you are on a call.

You could also create one to change an RGB bulb to red if you want.

To create these automations, just go to Configuration > Automations > Create New Automation.

Under Triggers, set to State. For the trigger, I use Call-Service and select one of my smart plugs.

Capitalization DOES matter for the various states! Make sure to capitalize the first letter of the state and leave the rest not capitalized.

Turn On Light When Teams Status is Set

This automation would turn on a light if the Teams Presence was set to Busy, Be right back, or Do not disturb.

Available state options are: Busy, Be right back, Do not disturb, Appear away, or Appear offline.

alias: Turn on Light if Teams Status is Busy
description: ''
trigger:
  - platform: state
    entity_id: sensor.teams_activity
    to: Busy
    id: Busy
  - platform: state
    entity_id: sensor.teams_status
    id: BeRightBack
    to: Be right back
  - platform: state
    entity_id: sensor.teams_status
    to: Do not disturb
    id: DND
condition: []
action:
  - service: switch.turn_on
    target:
      entity_id: switch.smart_plug_04cb3a05_on_off
mode: single

Turn On Light if on Teams Call, Turn Off When Call Ends

This automation would be used if you are on a call or in a meeting. If sensor.team_activity has a state of “In a call”, it turns on the light. If the state changes from “in a call” to “Not in a call”, the lights turn back off.

Available state options are: In a call, Not in a call

alias: Turn on light is on a Teams Call
description: ''
trigger:
  - platform: state
    entity_id: sensor.teams_activity
    to: In a call
    id: In a call
  - platform: state
    entity_id: sensor.teams_activity
    from: In a call
    to: Not in a call
    id: Not in a call
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: In a call
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.smart_plug_04cb3a05_on_off
      - conditions:
          - condition: trigger
            id: Not in a call
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.smart_plug_04cb3a05_on_off
    default: []
mode: single

Wrapping Up

This was another really fun project to dive into, so hopefully you guys found it useful!

Understandably, not everyone will be able to use this (especially if their corporate laptops are locked down with Standard user rights), but for those of you who’ve installed Teams on their personal computers and VPN in – it works out great!

The same automations can also be used with the Hass Workstation Service option. If webcam or microphone is turned on, then turn on a light or change an RGB bulb color.


My Favorite Home Assistant Devices

Below are some of the Home Assistant-compatible devices I personally use in my home. I highly recommend each of them.

The full list of all Home Assistant compatible & recommended devices I use can be found on my Equipment List page.

8 comments

You may also like

8 comments

Mike February 3, 2022 - 6:29 pm

Hello, this is a fantastic idea, but I’m running into an issue and was wondering if you could help explain it to me.

I believe I’ve followed the steps, I can get the my status in Teams, but it only shows as “Unknown” in HA, and I think I can see part of the problem. The script section:

Invoke-RestMethod -Uri “$HAUrl/api/states/$entityStatus” -Method POST -Headers $headers -Body ([System.Text.Encoding]::UTF8.GetBytes($params)) -ContentType “application/json”

consistently fails with:

Invoke-RestMethod : 404: Not Found
At C:\Scripts\Get-TeamsStatus.ps1:164 char:5
+ Invoke-RestMethod -Uri “$HAUrl/api/states/$entityStatus” -Method …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Unfortunately while I’m pretty comfortable in powershell I’m not so comfortable with the rest API calls. I’ve tried running them separately and the values appear correct, but always generates the 404.

Any ideas?

Reply
Mike February 3, 2022 - 6:38 pm

Found it. the trailing slash at the end of the URL got me.

Reply
Danny February 3, 2022 - 7:41 pm

Glad you got it working, and thanks for coming back to post your fix in case others run into the same issue!

Reply
Trent February 8, 2022 - 11:54 am

Hey,

I’m not getting any errors when running the script, however I am running W11 and the path is %appdata%\Roaming\Microsoft\Teams\logs.txt. I changed it within the script itself, but for whatever reason I’m still getting unknowns on both.

For the “User” in settings is that the HA user or the windows user?… I’m at a loss right now as to what to do, i did remove the trailing / as Mike mentioned but still no luck.

Any help would be appreciated.

Reply
Trent February 8, 2022 - 12:00 pm

Update:
It does update in HA when I issue .\Get-TeamsStatus.ps1 -SetStatus “away” in powershell.

So still at a loss why the status isn’t changing.

Reply
Trent February 8, 2022 - 12:22 pm

Okay, I got it working now, but now the powershell window is up when I log in.

Is there a way to start the script without opening the window?

Reply
Dan May 2, 2022 - 10:42 pm

Ive run across an issue with the script and HA restarts — once powershell gets a connection error, it never tries to reconnect again.

The PS script / scheduled task has to be stopped and started again manually.

Reply
Robi September 23, 2022 - 1:31 am

I made this. Thank you! This is awesome.

Reply

Leave a Comment