Wrong Battery Charge Level Displayed - How to Fix
#5
(12-23-2022, 07:47 PM)acid andy Wrote: I can confirm that commenting out that line of code in the UPower daemon results in the correct charge level being displayed again on my PinePhone.

To see if you need this fix, go into your Power settings and see how many batteries it displays. You'll probably see up to 4 of them. Unless you have the PinePhone keyboard connected, it's likely only one of them will show greater than 0% charge. This one is the actual charge level of your PinePhone's battery. The others seem to be little more than red herrings that confuse UPower!

If the displayed level of charge on the top tray is much lower than the one visible on the Power settings (If 4 batteries are shown, it may be exactly a quarter of it), and your distro isn't more than about 6 months old, then you most likely need the fix. If the two charge levels match up then you're all good. (Note: I have read that when the PinePhone keyboard's battery is connected it can actually read up to 200% - the fix I'm describing here won't stop that behavior, although to me in a way that makes sense when you have two real batteries connected).

I'll give you an outline of how I built the UPower daemon on my PinePhone. This was on postmarketOS but it should be similar on your distro, maybe with a different package manager.
  • First we need some tools to edit and build the code:

Code:
$ sudo apk add meson ninja gcc g++ cmake git nano
  • UPower has quite a few dependencies. I had to search for their package names when the build kept complaining they were missing. I don't have a full list of the commands I ran, so you may need some others (Let me know if you still get build errors and I may be able to help out):

Code:
$ sudo apk add glib-dev gobject-introspection-dev libgudev-dev libmobiledevice-dev gtk-doc
  • Now we can download the UPower source code:

Code:
$ git clone https://gitlab.freedesktop.org/upower/upower.git
  • Now, the fun part, we get to apply the patch, the old-fashioned way:

Code:
$ cd upower/src
$ nano up-daemon.c
  • You need to go to somewhere near line 249 where the following block of code is:

Code:
        g_debug ("Calculating percentage and time to full/to empty for %i batteries", num_batteries);

        /* use percentage weighted for each battery capacity
         * fall back to averaging the batteries.
         * ASSUMPTION: If one battery has energy data, then all batteries do
         */
        if (energy_full_total > 0.0)
                percentage_total = 100.0 * energy_total / energy_full_total;
        else
                percentage_total = percentage_total / num_batteries;
  • You need to comment out the "else" and the line that follows it:

Code:
        g_debug ("Calculating percentage and time to full/to empty for %i batteries", num_batteries);

        /* use percentage weighted for each battery capacity
         * fall back to averaging the batteries.
         * ASSUMPTION: If one battery has energy data, then all batteries do
         */
        if (energy_full_total > 0.0)
                percentage_total = 100.0 * energy_total / energy_full_total;
        /* else
         *      percentage_total = percentage_total / num_batteries; */
  • In nano press Ctrl-X then Y then Return to save it.
  • Now we can try and set up our meson build directory:

Code:
$ cd ..
$ meson setup build
  • Read any error messages that come back here and correct them. For me, they were all about missing dependencies which meant installing the corresponding packages.

  • If your OS isn't using systemd  Wink , it might say something like "ERROR: Dependency "systemd" not found, tried pkgconfig and cmake".
    • To disable the systemd dependency, we need to do:
      Code:
      $ nano ./meson_options.txt
    • Find the section:
      Code:
      option('systemdsystemunitdir',      type : 'string',      description : 'Directory for systemd service files ("no" to disable)')
    • And add a line saying "value : 'no',":
      Code:
      option('systemdsystemunitdir',      type : 'string',      value : 'no',      description : 'Directory for systemd service files ("no" to disable)')
    • Ctrl-X then Y then Return again to save it.
    • Then you can try again:
      Code:
      $ meson setup build
  • Once the build setup completes successfully we can try and compile UPower:

Code:
$ cd build
$ meson compile
  • If you got that far, congratulations, we should be nearly done. After carefully backing up your system, you could try and use ninja to fully install UPower (In which case you may find it wants to put everything under /usr/local/ instead of just /usr/ which may need a prefix configuring), but instead I just found the "upowerd" executable it had built - strangely inside build/src - and copied that into /usr/libexec after first renaming my original upowerd:

Code:
$ sudo mv /usr/libexec/upowerd /usr/libexec/upowerd.orig
$ sudo cp ./src/upowerd /usr/libexec/
$ sudo chmod +x /usr/libexec/upowerd
  • Cross your fingers, reboot, and hopefully your PinePhone still boots and even better the indicated charge level should be accurate again! If something broke, you need to reinstate the backup copy of /usr/libexec/upowerd that you made. If it's on an SD card, you could boot off your eMMC or put the card in another computer to fix it. If it's on the eMMC, boot off an SD card and fix it on the phone.

Good luck. I'd love to hear from anyone who attempts this and may be able to offer help if you get stuck with the build. Not many people are talking about this issue so it would be great to gauge how many are affected so we can look at who to talk to about a more permanent fix either in the PinePhone distros or UPower.

I accept no responsibility for any loss or damage resulting from the use of what I've written here. You could break your phone. Be careful and take backups.

Hi,


I just got my pinephone and keyboard last week and had exactly this problem. I followed your build process and now the battery shows upto 200%, before it was always upto 65%.

Thats was really super helpfull. Thank you so much for this! 

I'am on the latest postmarketOS phosh version.

 I still have an issue that percentage is not displayed correct when I turn off the keyboard battery. So when I start the phone with the keyboard battery off. It shows 100% (phone is fully charged) When I then turn the keyboard battery on it shows 180% (keyboard is almost fully charged) but when I turn the keyboard battery off it stays at 180%. One small issue is also that the Battery symbol is broken when keyboard battery is on.
  Reply


Messages In This Thread
RE: Wrong Battery Charge Level Displayed - Possible Causes - by kontogames - 02-13-2023, 10:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Increasing battery life slondr 8 3,243 03-19-2024, 02:15 PM
Last Post: aular
  Genode OS Now Live (finally a battery friendly PP OS?) PineFone 0 1,505 02-04-2023, 09:38 AM
Last Post: PineFone
  Interested in learning OS-level programming 8453sixtyfour 6 6,491 04-30-2022, 05:10 PM
Last Post: TRS-80
  Megapixels 1.3.0 - colors feels wrong hashfactory 0 1,143 09-11-2021, 09:26 AM
Last Post: hashfactory
  Pine phone Battery life vs operating system bcnaz 19 21,661 08-27-2021, 09:35 PM
Last Post: bcnaz
  Battery Charge Status not reporting correctly? (KDE Community Edition) Gregorius Techneticies 11 8,982 02-12-2021, 01:01 PM
Last Post: Giddyup
  Battery life for various OS & UI lat 8 9,766 12-21-2020, 08:20 PM
Last Post: ryo
  Low Battery indication (Manjaro/PostmarketOS with phosh) MamlinAV 3 4,515 12-16-2020, 03:44 AM
Last Post: dukla2000
  theoretical battery life on Pinephone hiimtye 6 6,864 12-12-2020, 01:48 AM
Last Post: jmorris

Forum Jump:


Users browsing this thread: 1 Guest(s)