Get low battery notifications for mouse earlier

batterymouse

Is it possible to tweak the % that I get warning of low battery for my Magic Mouse 2?

I currently only get it at 2% at which point it's very close to dying so I have to stop what I'm doing and plug it in. A reminder about 10-15% would allow me to plug it in the next time I have a natural break.

Best Answer

For those willing to copy and run a bash script and add a cron job, here is a complete solution that is free and current (as of macOS Catalina 10.15.3) and requires no programming (I've done that part for you), just a little system administration:

Save this to a bash script like ~/.mouse-battery-check.sh:

#!/usr/bin/env bash
PATH=/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin

# actual battery level
BATT=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i mouse -A 20 | grep BatteryPercent | cut -d= -f2 | cut -d' ' -f2`

# defaults to warn at 20%; accepts other number as 1st argument (useful for testing)
COMPARE=${1:-20}

if [ -z "$BATT" ]; then
  echo 'No mouse found.'
  exit 0
fi

if (( BATT < COMPARE )); then
  osascript -e "display notification \"Mouse battery is at ${BATT}%.\" with title \"Mouse Battery Low\""
fi

Open up Terminal and make the script executable:

chmod +x ~/.mouse-battery-check.sh

Now you can test that the script sends a desktop notification by running it when your BlueTooth mouse is connected (this tests for battery less than 101%, so it should always send a notification):

~/.mouse-battery-check.sh 101

To test again with the default setting (20%):

~/.mouse-battery-check.sh

When a BlueTooth mouse is not detected, the script will return, "No mouse found."

Checking Automatically

Now to test periodically and notify you automatically, add a new cron job:

env EDITOR=nano crontab -e

Add an entry like to check every 15 minutes:

*/15 * * * * cd ~ && bash ~/.mouse-battery-check.sh

(You can again pass the battery percentage as a parameter to the script here.)

Press Control+X then Y then Enter to exit the editor and save the cron job.

Compatibility

This has been tested on:

  • Catalina 10.15.3
  • Mojave 10.14.6
  • High Sierra 10.13.x in June 2018

Acknowledgements

For battery check, this answer:

adjust battery-warning-level of magic mouse

For notification, this answer:

How can I trigger a Notification Center notification from an AppleScript or shell script?

For cron jobs, this blog:

https://ole.michelsen.dk/blog/schedule-jobs-with-crontab-on-mac-osx.html

User sirclesam for compatibility testing.

Enhancement - also check keyboard (and maybe trackpad)

I modified the script above to check both Apple Magic Mouse and Keyboard. With the addition of one word it could check Trackpads, or any other overpriced Apple Magic doo dads, as well. I only have the mouse and keyboard so I started with that. I also changed the default to 10% which still gives me a few days to plug it in.

#!/usr/bin/env bash
# Check battery level of Apple Magic Mouse and Keyboard and Notify if low

PATH=/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin

# Warn at 10% or according to first parameter.  Pass in 101 for testing.
COMPARE=${1:-10}

# Check each device.  
# If you have a Trackpad please add the suitable token to this list and test it.
for HIDThingy in Keyboard Mouse; do
    # Determine battery level of Apple Magic Thingy
    BATT=`ioreg -c AppleDeviceManagementHIDEventService -r -l \
         | grep -i $HIDThingy -A 20 | grep BatteryPercent | sed -e 's/.* //'`

    if [ -z "$BATT" ]; then
      echo 'No $HIDThingy found.'
    elif (( BATT < COMPARE )); then
      osascript -e "display notification \"$HIDThingy battery is at ${BATT}%.\" with title \"$HIDThingy Battery\""
    fi
done