Microsoft Teams Status

Works fine at a first test.
I suggest to variables for username, URL and the token.
This way you only have to change the values on top of the script. More comfortable to use :wink:

Done! :slight_smile:

That was quick :smiley:
Thanks a lot for this. My wife loves it!
Now I need to automate a small light with esp8266

1 Like

I have the same error and I can’t find any typo. The service is “Paused”.

image

I would just make sure that your file path and file names are correct. That is the nature of that error. It’s likely you setup the service to run a path that doesn’t match the powershell script path.

For what it’s worth to those reading, this script does actually run not as a service if you can’t elevate permissions as well.

This loooks interesting you have a tad more info how this works within the flow , does the app push the status to the node red instance ? does node red pull data from the app ?

Node red makes the endpoints available vis the http in node. The PresenceLight app pushes the status to the endpoint using a GET method.

What I need is an automation that uses a presence detection in the chair and immediately shuts off the camera when I get up so no one sees i’m wearing shorts or sweatpants.

@EBOOZ Why did you choose template sensors for the script? On not, for example helpers (e.g. input_select)?

I’m trying to find a way to force the state of a sensor to a specific value from within HA and not by a PS script. Use case: If I shutdown the PC without first closing Teams, the state in HA will stay the same infinitely. I’d like to reset / force it to "Offline’ after a set duration without state changes.

Probably because the sensor wasn’t added if I didn’t :slight_smile: Don’t know for sure anymore, but the template is incorrect anyway.

I’m using Node-Red to set my status to offline when I cut of the power of my home office, and I’ve created a seperate PS to set the status to offline when a Windows Event is logged when the computer is shutting down via Task Manager.

Would you mind adding that script to the GitHub repo?

I think it would be a good idea to add parameter functionality to the orignal script, so you can run it with -Status “”.

When this has been added, you can run it when a shutdown EventID is logged:

I’ll start working on it later today :slight_smile:

I was thinking way to complex. I’ve added a check to see if Teams is running or not, and start monitoring the log if it is. Otherwise the status is set to Offline. This solves the issue the status showing incorrect when the computer is shutdown.

I’ve added some other improvements as well, which have been updated at Github.

1 Like

First off thank you! This is awesome and exactly what I needed to control lighting in my office. I prefer to work without lights on but when doing a video call I need the lights on. This allows me to automate based on my activity. Really appreciate this creativity!!

1 Like

A guy did this:

Any Webex Teams users reading. I have opened a PR for this feature in Home Assistant core.

Regarding th token: any user can generate a new token for this at developer.webex.com

I went the Powershell route here and I think my code is quite optimized: it requires only one line and works flawlessly!

Get-Content $env:APPDATA\Microsoft\Teams\logs.txt -Wait -Tail 0 | ? { $_ -match "(?<=StatusIndicatorStateService: Added )(\w+)" } | % { if($matches[0] -ne "NewActivity") {& "C:\Scripts\TeamsColor.ps1" $matches[0] }}

Get-Content -Wait -Tail 0 retrieves every line of the Teams log file, waits for new lines, while ignoring previous content of the file. These lines are then fed into a regex match filter. (After multiple tries, I found that this filter works best, as it still reports the updated status on top of a NewActivity status. It retrieves a single word after the matched expression.) For each matched object that is not NewActivity, another script (TeamsColor.ps1) is called with the status as argument.

This second script calls the HomeAssistant API at
/api/states/sensor.teams_raw with the state passed as argument:

$newval = [Convert]::ToString($args[0])

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer your_bearer_code")
$headers.Add("Content-Type", "application/json")

$body = "{`"state`": `"$newval`"}"

$response = Invoke-RestMethod 'https://your_domain/api/states/sensor.teams_raw' -Method 'POST' -Headers $headers -Body $body

This might not be necessary depending on your needs, but I also created a second template sensor sensor.teams which allow me to easily customize the displayed values from the raw values sent to HA:

sensor:
  - platform: template
    sensors:
      teams:
        friendly_name: "Microsoft Teams"
        unique_id: sensor.teams
        value_template: >-
            {% if states("sensor.teams_raw") in ["Available"] %}
              Available
            {% elif states("sensor.teams_raw") in ["DoNotDisturb","Presenting","Focusing"] %}
              Do not disturb
            {% elif states("sensor.teams_raw") in ["Busy","OnThePhone","DoNotDisturb","Presenting","Focusing","InAMeeting"] %}
              Busy
            {% elif states("sensor.teams_raw") in ["Away","BeRightBack"] %}
              Away
            {% else %}
              {{ states("sensor.teams_raw") }}
            {% endif %} 

Finally, I simply created an automation that is based on the sensor.teams_raw status:

- id: '1608509876133'
  alias: Update status light with Teams
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.teams
  condition: []
  action:
  - service: light.turn_on
    data:
      entity_id: light.statuslamp
      rgb_color: '{% if states("sensor.teams_raw") in ["Available"] %} [146, 195,
        93] {% elif states("sensor.teams_raw") in ["DoNotDisturb","Presenting","Focusing"]
        %} [255, 30, 50] {% elif states("sensor.teams_raw") in ["Busy","OnThePhone","DoNotDisturb","Presenting","Focusing","InAMeeting"]
        %} [255, 30, 50] {% elif states("sensor.teams_raw") in ["Away","BeRightBack"]
        %} [252, 209, 22] {% else %} [0, 0, 0] {% endif %}
        '
    entity_id: light.statuslamp
  mode: single

In addition to a sleep/shutdown scheduled script, I am also going to add a simple ping sensor as a condition for the lights, that pings my work laptop every 10 seconds or so to determine if it is online. Low tech, but I expect it to work quite nicely. Note that the Teams status sets itself to Away before putting the computer to sleep anyway, and depending on your purpose, that could be enough by itself!

Here is a small video demonstration: https://youtu.be/8kBtrowtfmk. I can provide more details if you are interested!

3 Likes

That looks great. Can you indeed share more details, also for the TeamsColor.ps1 script?

I updated my original post with more detail. :smile:
Do not hesitate if you have any further questions!

1 Like

Thanks. How are you automatically running the first script? Are you using NSSM for that?