# Revision history [back]

Hei,

I managed to patch together a shell script that handles the charging on the Jolla phone. It limits charging at around 80% of SoC and (if charger was not disconnected) starts charging at around 40% of SoC. To make full use of it, I installed Situations app from Jolla Store and created a situation that is triggered when charger accessory is connected and runs my script.

The contents of charging_control.sh script:

#!/bin/sh
# We need to take control over this, every time after reboot.
chmod 777 /sys/class/power_supply/usb/charger_disable
# Set it to allow charging.
echo 0 > /sys/class/power_supply/usb/charger_disable

#Set the variables.
soc=$(cat /sys/class/power_supply/battery/energy_now) present=$(cat /sys/class/power_supply/usb/present)
state=$(cat /sys/class/power_supply/usb/charger_disable) #Start the loop and keep cycling while the charger is present. while (($present > 0))
do
present=$(cat /sys/class/power_supply/usb/present) soc=$(cat /sys/class/power_supply/battery/energy_now)

#If SoC is over around 80%, charging is disabled.
if (($soc > 5400000 &&$state == 0)); then
echo 1 > /sys/class/power_supply/usb/charger_disable
state=1
fi

#If the SoC drops below around 40% and the charger is still connected, charging is enabled.
if (($soc < 2500000 &&$state == 1)); then
echo 0 > /sys/class/power_supply/usb/charger_disable
state=0
fi

#Sleep for a while before re-checking the situations.
sleep 120
done

#Re-enable charging when the charger is removed. Preparing for the next time.
if (($present < 1)); then echo 0 > /sys/class/power_supply/usb/charger_disable fi  The contents of the charging_control.desktop file: [Desktop Entry] Type=Application Name=Charging Control #The location of my script: Exec=/home/nemo/.scripts/charging_control.sh Icon=icon-l-terminal  The .desktop file can be put into any of the two folders: 1. /home/nemo/.local/share/applications 2. /usr/share/applications Limitations: With Situations app (by Pastilli Labs) currently only the second location works. The script can only be run as root. Disclaimer: As always, proceed at your own risk. I will be sorry if you happen to brick you device, but it won't be my fault. This script is working for me, though. I know that this script is not at all professional. It might have limitations or defects. If you notice one, please let me know, so I can update my post. PS.: If somebody feels to create a daemon out of the above, please do it. But send me a copy. LVPVS out. Hei, I refined my method over time. I am leaving the original script as well, but I am posting the way I currently use it. Here it goes: I created two short scripts, one for starting, one for stopping charging. All the timeing and battery checking is done by Situations App via the script 'What'. I figured that this leaves the least amount of over-head. Situations created: 'Start charging': 'When': Accessory = Charger && Battery !> 70% remaining 'What': Command: /the/path/to/charging_start.sh 'Stop charging': 'When': Accessory = Charger && Battery !< 80% remaining 'What': Command = /the/path/to/charging_stop.sh 'Charging': 'When': Accessory = Charger 'What': Command (at end) = /the/path/to/charging_start.sh  Contents of 'charging_start.sh': #!/bin/sh # We need to take control over this, every time after reboot. chmod 777 /sys/class/power_supply/usb/charger_disable # Set it to allow charging. echo 0 > /sys/class/power_supply/usb/charger_disable  Contents of 'charging_stop.sh': #!/bin/sh # We need to take control over this, every time after reboot. chmod 777 /sys/class/power_supply/usb/charger_disable # Set it to stop charging. echo 1 > /sys/class/power_supply/usb/charger_disable  Start of the original post: I managed to patch together a shell script that handles the charging on the Jolla phone. It limits charging at around 80% of SoC and (if charger was not disconnected) starts charging at around 40% of SoC. To make full use of it, I installed Situations app from Jolla Store and created a situation that is triggered when charger accessory is connected and runs my script. The contents of charging_control.sh script: #!/bin/sh # We need to take control over this, every time after reboot. chmod 777 /sys/class/power_supply/usb/charger_disable # Set it to allow charging. echo 0 > /sys/class/power_supply/usb/charger_disable #Set the variables. soc=$(cat /sys/class/power_supply/battery/energy_now)
present=$(cat /sys/class/power_supply/usb/present) state=$(cat /sys/class/power_supply/usb/charger_disable)

#Start the loop and keep cycling while the charger is present.
while (($present > 0)) do present=$(cat /sys/class/power_supply/usb/present)
soc=$(cat /sys/class/power_supply/battery/energy_now) #If SoC is over around 80%, charging is disabled. if (($soc > 5400000 && $state == 0)); then echo 1 > /sys/class/power_supply/usb/charger_disable state=1 fi #If the SoC drops below around 40% and the charger is still connected, charging is enabled. if (($soc < 2500000 && $state == 1)); then echo 0 > /sys/class/power_supply/usb/charger_disable state=0 fi #Sleep for a while before re-checking the situations. sleep 120 done #Re-enable charging when the charger is removed. Preparing for the next time. if (($present < 1)); then
echo 0 > /sys/class/power_supply/usb/charger_disable
fi


The contents of the charging_control.desktop file:

[Desktop Entry]
Type=Application
Name=Charging Control
#The location of my script:
Exec=/home/nemo/.scripts/charging_control.sh
Icon=icon-l-terminal


The .desktop file can be put into any of the two folders:

1. /home/nemo/.local/share/applications

2. /usr/share/applications

Limitations:

With Situations app (by Pastilli Labs) currently only the second location works.

The script can only be run as root.

Disclaimer: As always, proceed at your own risk. I will be sorry if you happen to brick you device, but it won't be my fault. This script is working for me, though.

I know that this script is not at all professional. It might have limitations or defects. If you notice one, please let me know, so I can update my post.

PS.: If somebody feels to create a daemon out of the above, please do it. But send me a copy.

LVPVS out.

Hei,

Update for SailfishX

I was afraid this hack won't work on SailfishX. But, hey! SailfishX has a better way to do it:

/sys/class/power_supply/battery/baterry_charge_enable

If '0' is written in it, battery charging stops immediately. And the discharging is way less than JP1301 had. I believe, in that case, the external power supply was logically cut and the phone went on battery only. But this in case, USB power supply is used and the battery is only there for buffering.

I just wanted to share my experiences.

/Update on SailfishX

I refined my method over time. I am leaving the original script as well, but I am posting the way I currently use it.

Here it goes:

I created two short scripts, one for starting, one for stopping charging. All the timeing and battery checking is done by Situations App via the script 'What'. I figured that this leaves the least amount of over-head.

Situations created:

'Start charging':
'When': Accessory = Charger && Battery !> 70% remaining
'What': Command: /the/path/to/charging_start.sh

'Stop charging':
'When': Accessory = Charger && Battery !< 80% remaining
'What': Command = /the/path/to/charging_stop.sh

'Charging':
'When': Accessory = Charger
'What': Command (at end) = /the/path/to/charging_start.sh


Contents of 'charging_start.sh':

#!/bin/sh
# We need to take control over this, every time after reboot.
chmod 777 /sys/class/power_supply/usb/charger_disable
# Set it to allow charging.
echo 0 > /sys/class/power_supply/usb/charger_disable


Contents of 'charging_stop.sh':

#!/bin/sh
# We need to take control over this, every time after reboot.
chmod 777 /sys/class/power_supply/usb/charger_disable
# Set it to stop charging.
echo 1 > /sys/class/power_supply/usb/charger_disable


Start of the original post:

I managed to patch together a shell script that handles the charging on the Jolla phone. It limits charging at around 80% of SoC and (if charger was not disconnected) starts charging at around 40% of SoC. To make full use of it, I installed Situations app from Jolla Store and created a situation that is triggered when charger accessory is connected and runs my script.

The contents of charging_control.sh script:

#!/bin/sh
# We need to take control over this, every time after reboot.
chmod 777 /sys/class/power_supply/usb/charger_disable
# Set it to allow charging.
echo 0 > /sys/class/power_supply/usb/charger_disable

#Set the variables.
soc=$(cat /sys/class/power_supply/battery/energy_now) present=$(cat /sys/class/power_supply/usb/present)
state=$(cat /sys/class/power_supply/usb/charger_disable) #Start the loop and keep cycling while the charger is present. while (($present > 0))
do
present=$(cat /sys/class/power_supply/usb/present) soc=$(cat /sys/class/power_supply/battery/energy_now)

#If SoC is over around 80%, charging is disabled.
if (($soc > 5400000 &&$state == 0)); then
echo 1 > /sys/class/power_supply/usb/charger_disable
state=1
fi

#If the SoC drops below around 40% and the charger is still connected, charging is enabled.
if (($soc < 2500000 &&$state == 1)); then
echo 0 > /sys/class/power_supply/usb/charger_disable
state=0
fi

#Sleep for a while before re-checking the situations.
sleep 120
done

#Re-enable charging when the charger is removed. Preparing for the next time.
if (($present < 1)); then echo 0 > /sys/class/power_supply/usb/charger_disable fi  The contents of the charging_control.desktop file: [Desktop Entry] Type=Application Name=Charging Control #The location of my script: Exec=/home/nemo/.scripts/charging_control.sh Icon=icon-l-terminal  The .desktop file can be put into any of the two folders: 1. /home/nemo/.local/share/applications 2. /usr/share/applications Limitations: With Situations app (by Pastilli Labs) currently only the second location works. The script can only be run as root. Disclaimer: As always, proceed at your own risk. I will be sorry if you happen to brick you device, but it won't be my fault. This script is working for me, though. I know that this script is not at all professional. It might have limitations or defects. If you notice one, please let me know, so I can update my post. PS.: If somebody feels to create a daemon out of the above, please do it. But send me a copy. LVPVS out. Hei, Update for SailfishX I was afraid this hack won't work on SailfishX. But, hey! SailfishX has a better way to do it: /sys/class/power_supply/battery/baterry_charge_enable If '0' is written in it, battery charging stops immediately. And the discharging is way less than JP1301 had. I believe, in that case, the external power supply was logically cut and the phone went on battery only. But this in case, USB power supply is used and the battery is only there for buffering. I just wanted to share my experiences. /Update on for SailfishX I refined my method over time. I am leaving the original script as well, but I am posting the way I currently use it. Here it goes: I created two short scripts, one for starting, one for stopping charging. All the timeing and battery checking is done by Situations App via the script 'What'. I figured that this leaves the least amount of over-head. Situations created: 'Start charging': 'When': Accessory = Charger && Battery !> 70% remaining 'What': Command: /the/path/to/charging_start.sh 'Stop charging': 'When': Accessory = Charger && Battery !< 80% remaining 'What': Command = /the/path/to/charging_stop.sh 'Charging': 'When': Accessory = Charger 'What': Command (at end) = /the/path/to/charging_start.sh  Contents of 'charging_start.sh': #!/bin/sh # We need to take control over this, every time after reboot. chmod 777 /sys/class/power_supply/usb/charger_disable # Set it to allow charging. echo 0 > /sys/class/power_supply/usb/charger_disable  Contents of 'charging_stop.sh': #!/bin/sh # We need to take control over this, every time after reboot. chmod 777 /sys/class/power_supply/usb/charger_disable # Set it to stop charging. echo 1 > /sys/class/power_supply/usb/charger_disable  Start of the original post: I managed to patch together a shell script that handles the charging on the Jolla phone. It limits charging at around 80% of SoC and (if charger was not disconnected) starts charging at around 40% of SoC. To make full use of it, I installed Situations app from Jolla Store and created a situation that is triggered when charger accessory is connected and runs my script. The contents of charging_control.sh script: #!/bin/sh # We need to take control over this, every time after reboot. chmod 777 /sys/class/power_supply/usb/charger_disable # Set it to allow charging. echo 0 > /sys/class/power_supply/usb/charger_disable #Set the variables. soc=$(cat /sys/class/power_supply/battery/energy_now)
present=$(cat /sys/class/power_supply/usb/present) state=$(cat /sys/class/power_supply/usb/charger_disable)

#Start the loop and keep cycling while the charger is present.
while (($present > 0)) do present=$(cat /sys/class/power_supply/usb/present)
soc=$(cat /sys/class/power_supply/battery/energy_now) #If SoC is over around 80%, charging is disabled. if (($soc > 5400000 && $state == 0)); then echo 1 > /sys/class/power_supply/usb/charger_disable state=1 fi #If the SoC drops below around 40% and the charger is still connected, charging is enabled. if (($soc < 2500000 && $state == 1)); then echo 0 > /sys/class/power_supply/usb/charger_disable state=0 fi #Sleep for a while before re-checking the situations. sleep 120 done #Re-enable charging when the charger is removed. Preparing for the next time. if (($present < 1)); then
echo 0 > /sys/class/power_supply/usb/charger_disable
fi


The contents of the charging_control.desktop file:

[Desktop Entry]
Type=Application
Name=Charging Control
#The location of my script:
Exec=/home/nemo/.scripts/charging_control.sh
Icon=icon-l-terminal


The .desktop file can be put into any of the two folders:

1. /home/nemo/.local/share/applications

2. /usr/share/applications

Limitations:

With Situations app (by Pastilli Labs) currently only the second location works.

The script can only be run as root.

Disclaimer: As always, proceed at your own risk. I will be sorry if you happen to brick you device, but it won't be my fault. This script is working for me, though.

I know that this script is not at all professional. It might have limitations or defects. If you notice one, please let me know, so I can update my post.

PS.: If somebody feels to create a daemon out of the above, please do it. But send me a copy.

LVPVS out.