Actually, I think this is a better version for getting both the low and the high warning:
#!/bin/bash
# In order for cron to play sound files it needs to export an environment variable:
export XDG_RUNTIME_DIR="/run/user/1000"
state=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "state" | awk '{print $2}')
battery=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "percentage" | awk '{print $2}')
battery=${battery/\%/}
if [[ $state == discharging && $battery -le 40 ]]; then
mplayer /home/purism/batalarm.ogg -loop 2 && espeak "charge battery"
elif [[ $state == charging && $battery -eq 90 ]]; then
mplayer /home/purism/batalarm.ogg -loop 2 && espeak "90% charge"
fi
That lets the low-battery warning keep playing until plugged in, and the 90% charge notification play just once (probably), given that the repetition is every 5 minutes, as specified in the cron job.
When charging, the level would certainly rise fast enough from 90%, so the notification wouldn’t keep sounding. But the -eq (is equal to) could be replaced with -ge (is greater than or equal to) to allow the notification to repeat every 5 minutes if still charging, if desired. The “90% charge” message could also be changed to, e.g. “charge complete” or “disconnect charger.”