Send Bluetooth battery values to home assistant via Powershell

Afbeelding

Everytime when I needed to charge the battery of my keyboard/mouse I was too late and busy with my daily work. I was wondering if I could manage to get a notification on my phone when the battery value is below 10% for example. This is how to send Bluetooth battery values to home assistant via Powershell.

Part 1: Getting the battery value from Powershell

First you need to know the name of the particular bluetooth device.
You can find this to run this powershell command:

Get-PnpDevice -Class 'Bluetooth'

In this case the friendly name of the device is “MX Anywhere 2”

Next you need the keyname of the desired battery value.

Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'MX Anywhere 2' | Get-PnpDeviceProperty

In my case the keyname is “{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2”
(notice the space in the string)

The final command to output the battery status value is:

Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'MX Anywhere 2' | Get-PnpDeviceProperty -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' | select-Object -ExpandProperty Data

Offcourse you should use your own friendly name and keyname. Write this command down. You’ll need it later in the next steps…

Part 2: Prepare Home assistant

  • log into Home assistant and click on your name in the bottom left
  • Scroll all the way down to the bottom and create a long-lived access token
    choose a name. For example: Bearer
  • A code will be generated. Copy this code. You will need it later


Now let’s create an input_number helper in Home assistant

  • Click on “settings” in the left menu and choose “Devices and services”. Then choose the “Helpers” tab
  • Choose create Helper —> Number with these settings

Part 3: Send value to Home assistant input_number entity

Open notepad and create the following ps1 Powershell script:

$mouse = Get-PnpDevice -Class 'Bluetooth' -FriendlyName '<friendly name step 1>' | Get-PnpDeviceProperty -KeyName '<keyname step 1>' | select-Object -ExpandProperty Data 
$muis_state = '{"state": "' + $mouse + '", "attributes": {"unit_of_measurement": "%","mode":"box"}}'
Invoke-RestMethod -Method POST -Uri '<YOUR_HA_URL>/api/states/input_number.<device step 2>' -Verbose:$false -Headers @{
    'Authorization' = '<Bearer or your_token_name> <your_token_code>'
    'Content-Type' = 'application/json'
} -Body  $muis_state

Now let’s create a schedule task which runs this script for example every 10 minutes

Part 4: Create a scheduled task

  • Goto task scheduler
  • Choose create task with the following settings
“run whether user logged on or not”. You won’t see the Powershell window appear
program / script:
powershell.exe

Arguments:
-ExecutionPolicy Bypass -WindowStyle hidden <path to your ps1 file>

Congratulations!



You have integrated your Windows bluetooth device battery status into Home assistant.
Feel free to build automation / scripts etc around the input_number helper

Lees verder