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

Budgetplanner

Afbeelding

Deze budgetplanner heb ik altijd zelf gebruikt voor mijn eigen financiën (als appje op m’n telefoon). Ik heb het noodzakelijkerwijs zelf ontworpen. Het is heel simpel. Maar wel doeltreffend. Ik kom altijd goed uit aan het einde van de maand. Zo houd je je uitgaven inzichtelijk.

– voert de datum in wanneer je je salaris / inkomen krijgt.
– Voer het saldo in wat je nu kan uitgeven. (Ik heb bijv een aparte rekening voor mijn
vaste lasten. M’n dagelijkse uitgaven houdt ik dan op m’n algemene rekening over)
– Nu kan je zien wat je uit kan geven op die dag.

Zie het als een leidraad. Als je wat meer uitgeeft als aangegeven kan je de volgende dag weer wat minder uitgeven.

Lindsey Bier

Uitgelicht

Lindsey Bier heeft nog heel even nodig om de juiste smaak en koolzuurgehalte te krijgen. Nog even geduld. Het bier Lindsey is vernoemd naar mijn lieve pasgeboren nichtje Lindsey

Het etiket is nog onbekend. Deze zal ik onthullen zodra het bier rijp is voor consumptie

Elfstedentocht

Uitgelicht

Pa en ik gaan binnenkort de Elfstedentocht fietsen. Hieronder de route:

De route is zo’n 200 km lang. We gaan het dan ook niet in een dag doen