So I just registered and have not enough Kharma to comment on the original post (but I can ask a new question and this makes no sense to me; go figure), but I got this excellent script from Will Cain from here (https://apple.stackexchange.com/questions/254703/get-low-battery-notifications-for-mouse-earlier), and I modified it a little.

The intention is to keep the battery between 20 and 80%, but I don't want the messages to keep appearing, for example, if the battery is lower then 20% but is already charging; or if the battery is highter then 80% and it's not charging anymore.

I'm pretty noob when it comes to bash scripting, though. But here's the general idea:

       #!/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 keyboard -A 20 | grep BatteryPercent | cut -d= -f2 | cut -d' ' -f2`
    
    #Devices is connected, not charging
    CONN=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i connected - A 20`
    
    #Device is charging
    CHARG=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i charging -A 20`
    
    # defaults to warn at 20%; accepts other number as 1st argument (useful for testing)
    MIN=${1:-20}
    
    # defaults to warn at 20%; accepts other number as 1st argument (useful for testing)
    MAX=${1:-80}
    
    if [ -z "$BATT" ]; then
      echo 'No keyboard found.'
      exit 0
    fi
    
    if ((( BATT < MIN )) && ((CONN))); then
      osascript -e "display notification \"Keyboard battery is at ${BATT}%.\" with title \"Keyboard Battery Low\""
    fi
    
    if ((( BATT > MAX )) && ((CHARG))); then
      osascript -e "display notification \"Keyboard battery is at ${BATT}%.\" with title \"Keyboard Battery High\""
    fi

Obviously line 11 (CHARG) doesn't work because ioreg doesn't seem to contain that information. Any suggestions, maybe something more elegant then this?