How to Set Custom Mouse/Keyboard Battery Level Alert?

And I think we're back to the same issue @peternlewis had here.

Sorry, totally out of ideas now :frowning:

'Twas a valiant effort, and much appreciated.

The big annoyance with wireless mice is that they always run out of battery just when you are in a hurry...

And of course that is even more annoying with the Magic Mouse as it has to be laid on its back to charge.

The best option I have found is to have two mice, with one always switched off but charging...

1 Like

Yes, it looks like you cannot read the value of the attribute for some inexplicable reason, but you can find the one that matches a value using whose. Weird.

Doesn't help in your case if you want to read the value, unless you know it is one of a certain set of possible values, then you could conduct whose tests until you find it.

2 Likes

That was my thought on the way to work this morning.

@noisneil -- what's the return from the following? Set trueNum to the displayed battery % and falseNum to anything but the % before running:

set trueNum to "90"
set falseNum to "99"
tell application "System Events"
	tell process "Control Centre"
		set btMenu to item 1 of (every menu bar item of menu bar 1 whose value of attribute "AXAttributedDescription" is "Bluetooth")
		click btMenu
		set trueList to every UI element of checkbox 1 of scroll area 1 of group 1 of window 1 whose attribute "AXAttributedDescription" contains trueNum
        set falseList to every UI element of checkbox 1 of scroll area 1 of group 1 of window 1 whose attribute "AXAttributedDescription" contains falseNum
	end tell
end tell
return "True: " & count of trueList & "      False: " & count of falseList

Totally untested. But if it works you could loop through 11-20 and exit with "needs charging" as soon as there's a match.

Even better would be if contains " 90%" works, then you could from 1% to 20%...

The script fails for me. Does it work for you? Are you on Ventura too?

"Totally untested" was the literal truth... Home now, and on Ventura, but back to best-guessing your UI elements from previous. I also missed out value of -- D'oh!

So going by the version that got you the "error -10000" and including the " " and "%" in the check (again, change the first two lines to numbers that do and don't match current battery level)...

set trueNum to " 90%"
set falseNum to " 99%"
tell application "System Events"
	tell process "Control Centre"
		set btMenu to item 1 of (every menu bar item of menu bar 1 whose value of attribute "AXAttributedDescription" is "Bluetooth")
		click btMenu
		set trueList to (every checkbox of scroll area 1 of group 1 of window 1 whose value of attribute "AXAttributedDescription" contains trueNum)
		set falseList to (every checkbox of scroll area 1 of group 1 of window 1 whose value of attribute "AXAttributedDescription" contains falseNum)
	end tell
end tell
return "True: " & (count of trueList) & "      False: " & (count of falseList)

Sorry for the delayed response. That outputs the following.

True: 1 False: 0

Seems promising...?

Hell yeah!

So the hope is that the following will return Low Battery if your % is <=20, Battery is Good otherwise, and you can then use that in a KM condition.

tell application "System Events"
	tell process "Control Centre"
		set btMenu to item 1 of (every menu bar item of menu bar 1 whose value of attribute "AXAttributedDescription" is "Bluetooth")
		click btMenu
		repeat with x from 20 to 0 by -1
			set theMatch to " " & x & "%"
			set trueList to (every checkbox of scroll area 1 of group 1 of window 1 whose value of attribute "AXAttributedDescription" contains theMatch)
			if (count of trueList) = 1 then return "Low Battery"
		end repeat
	end tell
end tell
return "Battery is Good"

The bad news is that it will do all the loops most of the time because your mouse battery will be >20%. I don't think that will cause much of a delay to the sleep, but try it and see.

1 Like

Wahey!!! You are a clever one!

It seems to work even without showing the menubar item (via Bartender), so nothing visibly happens onscreen. Should I assume that the value won't be accurate if I leave that bit out as the bluetooth menu only updates itself when clicked?

Here's the macro I'm using:

Mouse - Low Battery Alert.kmmacros (23 KB)

Macro screenshot

It runs nice and quick too!

Not a clue. I can't access the menu item to filter the attributes unless the menu is opened, but Bartender might be doing something funky (perhaps the menu is always open but Bartender hides it?), so you'll just have to try it and see.

Frankly -- I'm amazed we've got this far!

1 Like

Turns out I can't use the sleep trigger; the mac actually sleeps before it has time to run the script. Instead, what I've decided to do is:

Wake: runs the AS and sets a global variable to Charged or Low.
Sleep: If the variable is Low, it sends me a Pushover Notification.

Seems to be working! Thanks Nige!

How are you triggering sleep? If it's by macro (IIRC we messed around with pmset) then re-order things to put this new bit before the sleep command. If using the standard sleep commands or a schedule you might be able to do something with caffeinate (though I've never tried that after the sleep command has been issued!).

Via Alfred. What I might do is switch to using KM for that. I already have a sleep routine that does a bunch of stuff (most notably, turning off all my studio gear), and that runs fine. Probably because it's all background stuff that doesn't need any GUI interaction.

Edit: Yep, that works. It does delay sleep by about a second but that's fine by me. As you can see, the Put Computer to Sleep action is early in the macro, but it everything after it runs fine.

Screenshot

1 Like

Hi @noisneil, unfortunately I can't help you. My Mac's are still running on macOS Big Sur and macOS Monterey.

But maybe I can give you an idea how to design your pushover notification. Battery less than 10%:

2022_11_10_Support_2

Daily battery status notification (keyboard, trackpad and mouse):

2022_11_10_Support_4

Thanks but I can't see any real difference aside from including the date/time (which I don't need) and an icon (which I don't need). Have I missed something else?