{"id":5417,"date":"2021-06-07T21:04:48","date_gmt":"2021-06-08T02:04:48","guid":{"rendered":"https:\/\/www.summet.com\/blog\/?p=5417"},"modified":"2021-06-08T08:08:21","modified_gmt":"2021-06-08T13:08:21","slug":"enabling-a-maximum-battery-charge-amount-charge-threshold-on-lenovo-x1-in-ubuntu","status":"publish","type":"post","link":"https:\/\/www.summet.com\/blog\/2021\/06\/07\/enabling-a-maximum-battery-charge-amount-charge-threshold-on-lenovo-x1-in-ubuntu\/","title":{"rendered":"Enabling a maximum battery charge amount (charge threshold) on Lenovo X1 in Ubuntu"},"content":{"rendered":"<p>Most of the time when using my laptop I am plugged into it&#8217;s AC charger. But, I don&#8217;t want my lithium-ion battery charged to 100% all of the time, as that is unhealthy for the battery and will lead to it prematurely losing capacity.<\/p>\n<p>My Lenovo X1 Carbon allows you to set a value less than 100 in the:<br \/>\n\/sys\/class\/power_supply\/BAT0\/charge_control_end_threshold<br \/>\nfield and will stop charging your battery if it goes above that amount.<\/p>\n<p>So you can issue a command like the following to prevent your laptop from charging your battery above 80%:<\/p>\n<pre>echo \"80\" | sudo tee \/sys\/class\/power_supply\/BAT0\/charge_control_end_threshold<\/pre>\n<p>(and if you wanted to get fancy, you could tell it to not start charging the battery until it fell below 70% with a command like the following)<\/p>\n<pre>echo \"70\" | sudo tee \/sys\/class\/power_supply\/BAT0\/charge_control_start_threshold\r\n<\/pre>\n<p>Then, you would have to unplug the laptop to discharge the battery below 80, and it would just stay there. \u00a0 If you were planning on taking your laptop somewhere without a power cord, you would want to reset this to 100 and fully charge it before you left.<\/p>\n<p>On the X1 carbon with Ubuntu 21.04, I got some &#8220;\/sys\/class\/power_supply\/BAT0\/charge_control_end_threshold: Invalid argument&#8221; type errors when trying to set the end amount to a value lower than the current start amount (or the start amount to a value higher than the current end amount). So if you are messing with one, you should probably set the other appropriately before-hand. For example, I use the following commands, in this specific order to limit to 80 (start charging below 70):<\/p>\n<pre>echo \"70\" | sudo tee \/sys\/class\/power_supply\/BAT0\/charge_control_start_threshold\r\necho \"80\" | sudo tee \/sys\/class\/power_supply\/BAT0\/charge_control_end_threshold\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>And then to reset to 100 (start charging below 99 or less) I use the commands in this order:<\/p>\n<pre>echo \"100\" | sudo tee \/sys\/class\/power_supply\/BAT0\/charge_control_end_threshold\r\necho \"99\" | sudo tee \/sys\/class\/power_supply\/BAT0\/charge_control_start_threshold\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Using this ordering prevents the Invalid argument errors for me.<\/p>\n<p>The above commands reset the default values, and will not survive a reboot. So if you want them to always be active, you can set up a system service which will fire every time your computer reboots by creating a file in \/etc\/systemd\/system\/ \u00a0 called charge-threshold.service that looks like the following:<\/p>\n<pre>[Unit]\r\nDescription=Set the battery charge threshold\r\nAfter=multi-user.target\r\nStartLimitBurst=0\r\n\r\n[Service]\r\nType=oneshot\r\nRestart=on-failure\r\nExecStart=\/bin\/bash -c 'echo 70 | tee \/sys\/class\/power_supply\/BAT0\/charge_control_start_threshold; echo 80 | tee \/sys\/class\/power_supply\/BAT0\/charge_control_end_threshold'\r\n\r\n[Install]\r\nWantedBy=multi-user.target<\/pre>\n<p>Then enable the service with the following:<\/p>\n<pre>X1:\/etc\/systemd\/system$ sudo systemctl enable charge-threshold.service\r\nX1:\/etc\/systemd\/system$ sudo systemctl start charge-threshold.service\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Most of the time when using my laptop I am plugged into it&#8217;s AC charger. But, I don&#8217;t want my lithium-ion battery charged to 100% all of the time, as that is unhealthy for the battery and will lead to &hellip; <a href=\"https:\/\/www.summet.com\/blog\/2021\/06\/07\/enabling-a-maximum-battery-charge-amount-charge-threshold-on-lenovo-x1-in-ubuntu\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[26],"class_list":["post-5417","post","type-post","status-publish","format-standard","hentry","category-linux","tag-ubuntu"],"_links":{"self":[{"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/posts\/5417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/comments?post=5417"}],"version-history":[{"count":6,"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/posts\/5417\/revisions"}],"predecessor-version":[{"id":5424,"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/posts\/5417\/revisions\/5424"}],"wp:attachment":[{"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/media?parent=5417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/categories?post=5417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/tags?post=5417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}