Script to full charge until end_threshold, even if charging is interrupted (unplug/replug)

##### SET VALUES #####
# end_threshold has to be bigger than start_threshold
# add "-f force_start_value end_start_value" to command to force "charging to end"
wanted_start_threshold=20
wanted_end_threshold=93   #
time_interval=5    #
####################
end_threshold=$wanted_end_threshold
force_start=$2
forec_end=$3
force=$1
if [[ $force_start != "" ]];then
 wanted_start_threshold = $force_start
fi
if [[ $force_end != "" ]];then
wanted_start_threshold = $force_end
fi
echo "setup was:"
echo "start at: " $wanted_start_threshold
echo "end at: " $end_threshold

echo $force
# initialization
echo "INTIALIZING"
echo $end_threshold | sudo tee /sys/class/power_supply/BAT0/charge_control_end_threshold
while :
do
  actual_capacity=$(cat /sys/class/power_supply/BAT0/capacity)
  status=$(cat /sys/class/power_supply/BAT0/status)
  echo "end_threshold is: "$end_threshold
  echo "status is: "$status
  echo "capacity is: "$actual_capacity
  # if start_threshold is reached
  if [[ $actual_capacity -lt $wanted_start_threshold ]] || [[ $1 == "-f" ]]; then
    echo "STARTING charge to end"
    echo $(($end_threshold - 1)) | sudo tee /sys/class/power_supply/BAT0/charge_control_start_threshold
  fi
  # if end_threshold is being reached
  if [[ $(cat /sys/class/power_supply/BAT0/charge_control_start_threshold) -ge $(($end_threshold - 1)) ]]; then
    echo "REACHING end_threshold"
  fi
  # If end_threshold is reached then reset start_threshold
  if [[ $actual_capacity -ge $((end_threshold -1)) ]]; then
    echo "ENDING: end_threshold reached, reseting threshold_files"
    echo $wanted_start_threshold | sudo tee /sys/class/power_supply/BAT0/charge_control_start_threshold
  fi
  sleep $time_interval
done
3 Likes

script updated

2 Likes