How to Set Custom Mouse/Keyboard Battery Level Alert?

I use iMac 27, the system alerts me only when the charge of the mouse and the keyboard reaches 5%. Is there any way to let Keyboard Maestro alert me earlier like say 20%?

Hello @AkshayHallur, I have no solution for your request, but a tip.

I use Magic Battery mini (2.29 €) from the Mac App Store. With this app you can either check the battery status in the Notification Center, or you can do something else with KM.

I created a macro that shows me the status for 8 seconds at the bottom left.
I created the same macro with "At sytem wake".

I can get Bluetooth info from the shell with

ioreg

I can get the Bluetooth info for my Apple Mouse from the shell with

ioreg -n BNBMouseDevice

and for my older Apple Keyboard (~2012) with

ioreg -n IOAppleBluetoothHIDDriver

Then we can build a macro around that, like this:

mkb-pty-fs8
Mouse & Keyboard Battery Charge Level Warning.kmmacros (5.2 KB)

The macro triggers every 3 minutes. It will give you a notification when the level falls below a configurable threshold. The notification will show you the threshold and the current battery levels of mouse and keyboard:

22-pty-fs8

You can set the threshold in the green action.

As said: This works specifically for my Apple Mouse and for my Apple Keyboard from 2012. ATM I’m still trying to make it work for my newer 2016 Keyboard.

So, try it and let me know if it works with your devices.


Source: Mac Bluetooth Keyboard/Mouse Battery Level in Terminal


Update (2018-06-06 10:39 UTC)

I have finally found the key that works with my Magic Keyboard from 2016. It is

AppleDeviceManagementHIDEventService

So, if my macro doesn’t show the battery for your keyboard then replace “IOAppleBluetoothHIDDriver” in the keyboard shell script action with that key.

2 Likes

Alternatively, I can recommend you iStat Menus 6 by bjango.

It’s a $18 menu bar app but it is worth every penny. It can show you those battery level notifications and much, much more.

Notifications:

CPU in menu bar:

51-pty-fs8

It shows you also: Network, RAM, disk I/O, storage, weather, time, moon, sensors, fans, energy consumption, …

2 Likes

@Tom.

For 2017, iMac your update worked great!

AppleDeviceManagementHIDEventService

It returns both keyboard and mouse battery levels. So, I had to regex the results to get keyboard and mouse levels separately.

Here's the final macro!

Mouse & Keyboard Battery Charge Level Warning.kmmacros (5.7 KB)

OK, I understand. So, the “new generation” devices (the ones with the integrated battery) run under the same Bluetooth key (or node), while the older ones have separate keys.
‌


‌

Here is a slightly streamlined version of your macro:

newer-pty-fs8

Mouse & Keyboard Battery Charge Level Warning (Newer Devices).kmmacros (4.9 KB)

I couldn’t test it with real shell output, since I only have one new-generation device. But if your macro has worked for you then this should work too.

1 Like

This is great. I also tried to accomplish it, but not really a regex guy.

Is there any way to access the current battery percentage of a third party mouse? The battery level of my MX Master is displayed under the Bluetooth menubar item, so I'd think it must be accessible somehow, but was surprised not to find anything on the forum about it.

I used to have these battery levels displayed with Ăśbersicht, but the underlying command line trick stopped working a couple of OS versions ago. My pal Google and I were never able to find a contemporary way to do it. :face_with_raised_eyebrow:

I use this shell script with Swiftbar on Monterey with my Magic Mouse (Bluetooth). It just looks for batterypercent in ioreg's output. You could test that on the command line to see if it works for your mouse.

mousecharge.3m.sh.zip (743 Bytes)

Do you have an Apple Mouse? I'm using a Logitech one. I tried running this in Terminal:

#!/bin/bash

level=$(ioreg -n AppleDeviceManagementHIDEventService | grep -i '"batterypercent" =' | sed 's/[^[:digit:]]//g')

echo "$level%"

...and got zsh: no such file or directory: #/bin/bash.

Yes, my shell script is a Bash script, not a Zsh shell script. And on your system, Terminal can't find Bash.

My inexperience is showing. I'll have to look up what a bash script is!

Were you actually typing in the Terminal or running the script from a text file?

If typing you'll need to do
bash<Return> to go into the bash shell then
level...blah...blah...blah<Return> to run the command then
echo "$level%"<Return> to print the value of the variable then
exit<Return> to exit bash and go back to zsh.

Just drop the shell script in an Execute Shell Script action like so:

Execute a Shell Script.kmactions (871 B)

Keyboard Maestro Export

If you get anything besides a percentage sign, it works for your mouse. Mine is an Apple Magic Mouse and it works on that. Good luck!

...is what I got. Having idly perused the usual online automator haunts, I get the impression this kind of thing only works for Apple mouses (mice?).

CleanShot 2022-11-07 at 22.50.24@2x

It must be gettable, surely. My plan is to set up an alert as part of my sleep routine that tells me to stick my mouse on charge if it's below 20%.

Do you get anything usable if you type

ioreg -c BNBMouseDevice

...in Terminal?

The blunt force approach would be to check the percentage in your Bluetooth menu bar display and then run ioreg with no arguments in Terminal, saving it to a file if you like so you can hunt down the 90% or whatever the value is and see which parameter is reporting it.

Errmmmm... :man_shrugging:t2:

ioreg -c BNBMouseDevice.zip (11.2 KB)

Ok I just clicked on the bluetooth menu item to show the MX Master battery percentage, then immediately ran ioreg in Terminal, copied and pasted it to TextEdit and searched for "90", which the percentage actually happened to be at the time. It found nine entries, none of which seemed to relate to the mouse, although to be fair, I have no idea what I'm looking at!

ioreg.txt.zip (11.0 KB)

Me :man_shrugging: too...

It's clipping the output (the $ at the end of every line). You could try

ioreg -c BNBMouseDevice -w 0 | grep -i battery

...to force complete lines and then filter to only lines containing "battery".