You are not logged in.

#1 2012-09-02 10:50:02

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

[SOLVED] How to read whether laptop is on AC or battery?

Hello,

I've made a powersave script that uses pm-powersave to decide whether it should turn on power saving (true) or restore the defaults (false).

However, I've switched to systemd now and that does not support pm-utils. This means I'll have to find another way to read whether or not the laptop is using AC or battery. I've looked briefly at laptop-mode-tools' source and AFAIK it does so with a udev rule:

ACTION=="change", SUBSYSTEM=="power_supply", RUN+="/lib/udev/lmt-udev auto"

Is this the way I should go, to be able to let my script determine if I'm on battery or AC? If so, then how can I make this udev rule see if I'm on AC or battery? If possible, I'd like to make a rule that detects AC and then runs powersave false and (either in the same rule or another one, I won't mind using two rules) when it detects battery that it runs powersave true.

Is this possible? Are there any better ways to accomplish what I want?

Thanks in advance!

Last edited by Unia (2012-09-02 13:13:29)


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#2 2012-09-02 11:22:21

nierro
Member
From: Milan, Italy
Registered: 2011-09-02
Posts: 849

Re: [SOLVED] How to read whether laptop is on AC or battery?

I'm using systemd and pm-utils right now, without any kind of problem...what are you referring to? I didn't know systemd doesn't support pm!

Offline

#3 2012-09-02 11:30:00

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] How to read whether laptop is on AC or battery?

nierro wrote:

I'm using systemd and pm-utils right now, without any kind of problem...what are you referring to? I didn't know systemd doesn't support pm!

I mean, it doesn't support sleep/hibernate using pm-utils and as such, it seems a bit redundant to keep it on my system only to run my script. That's why I'm looking for an alternative way to read AC/battery status.


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#4 2012-09-02 11:54:53

nierro
Member
From: Milan, Italy
Registered: 2011-09-02
Posts: 849

Re: [SOLVED] How to read whether laptop is on AC or battery?

Yes, since upower 0.9.18 it is true.
I was having the same thought...i use pm only to use my powersave function.
I asked systemd developer to implement pm-powersave functions in systemd (since it already handles some acpid events and pm-suspend/hibernate) but i wasn't convinced.
So i asked the upower developer to make upower run every script i want, instead of the only /power.d/* . But again, he told me that powersave script aren't so useful, because what it's good on battery is also good on ac...so don't know how can we reach this behaviour without pm-utils for now.

Offline

#5 2012-09-02 12:02:13

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] How to read whether laptop is on AC or battery?

If I can just find out how other applications read whether the system is on AC or battery, I can make my script work.

So, can anyone answer this?


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#6 2012-09-02 12:08:19

nierro
Member
From: Milan, Italy
Registered: 2011-09-02
Posts: 849

Re: [SOLVED] How to read whether laptop is on AC or battery?

Well, if you find the right way, i hope you'll share it with us wink

Offline

#7 2012-09-02 12:10:32

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,422
Website

Re: [SOLVED] How to read whether laptop is on AC or battery?

/sys/class/power_supply/BAT1/status


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2012-09-02 12:13:10

65kid
Member
From: Germany
Registered: 2011-01-26
Posts: 663

Re: [SOLVED] How to read whether laptop is on AC or battery?

nierro wrote:

...he told me that powersave script aren't so useful, because what it's good on battery is also good on ac...

I have always been skeptical regarding all these powersave scripts. Shouldn't the kernel know best about the settings? Are all these settings actually useful? Do they actually save power? Especially the cpufreq powersave governor: I read somewhere (don't have a source, sorry) that it does't actually save power. The ondemand governor may increase the frequency on load, but therefore the cpu is also done faster with the job and can idle longer. So all in all, it doesn't really make sense to use anything other than ondemand.

I'm not saying that these power saving scripts are completely useless - I don't know that. But I would love to see some comparions on how much effect all these settings actually have.

I know I haven't actually helped here, but I wanted to get his off my mind, sorry.... wink

Offline

#9 2012-09-02 12:19:48

nierro
Member
From: Milan, Italy
Registered: 2011-09-02
Posts: 849

Re: [SOLVED] How to read whether laptop is on AC or battery?

Sorry for the OT:
Yes, i use conservative governor, a little tweaked. I know powersave governor is useless.
But since with my script my battery life has increased of something like 30-40%, i guess those settings aren't at least so bad.
Yes, we can use our de-power-manager (in my case xfce-p-m), but they lack of lots of settings. And i prefer to have my hard disk power management set the way i like, not the way xfce developers like.
/close OT.

Offline

#10 2012-09-02 12:32:34

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] How to read whether laptop is on AC or battery?

65kid wrote:
nierro wrote:

...he told me that powersave script aren't so useful, because what it's good on battery is also good on ac...

I have always been skeptical regarding all these powersave scripts. Shouldn't the kernel know best about the settings? Are all these settings actually useful? Do they actually save power? Especially the cpufreq powersave governor: I read somewhere (don't have a source, sorry) that it does't actually save power. The ondemand governor may increase the frequency on load, but therefore the cpu is also done faster with the job and can idle longer. So all in all, it doesn't really make sense to use anything other than ondemand.

I'm not saying that these power saving scripts are completely useless - I don't know that. But I would love to see some comparions on how much effect all these settings actually have.

I know I haven't actually helped here, but I wanted to get his off my mind, sorry.... wink

The general solutions (DE-dependant powermanagers, laptop-mode-tools, cpu frequency) don't save that much power. However, Taylorchu's powerdown (find here on Community Contributions) and my script (based on Taylorchu's) do save alot of power. Without my script, I get ~1 hour of battery life. With my script, it's ~1:45. That's roughly 75% more! (and yes, my battery is fried, I know tongue)

Trilby, I know about the /sys interface. But to my knowledge, I'd have to make a script loop in checking that directory. I don't think this is the best solution, e.g. I think (if possible) udev rules or something along those lines is cleaner.


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#11 2012-09-02 12:42:05

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,422
Website

Re: [SOLVED] How to read whether laptop is on AC or battery?

Ah, sorry, I didn't read very carefully.  I just read up to wanting a script to know if you were on battery or AC.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#12 2012-09-02 12:53:13

nierro
Member
From: Milan, Italy
Registered: 2011-09-02
Posts: 849

Re: [SOLVED] How to read whether laptop is on AC or battery?

Will something like

SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/etc/mypowersavescript true"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="/etc/powersavescript false"

work?
EDIT: i'm studying and i have no time to try it, so, please, tell me whether it works or not! Thanks!

Last edited by nierro (2012-09-02 12:54:03)

Offline

#13 2012-09-02 13:13:09

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] How to read whether laptop is on AC or battery?

nierro wrote:

Will something like

SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/etc/mypowersavescript true"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="/etc/powersavescript false"

work?
EDIT: i'm studying and i have no time to try it, so, please, tell me whether it works or not! Thanks!

You the man! I had already found this on Launchpad: https://bugs.launchpad.net/ubuntu/+sour … bug/250355

But that didn't work. What you proposed, however, does work!

I've updated everything on GitHub so should someone want to test, you can find it there. The PKGBUILD is in the PKGBUILDS repo.

Thanks everyone!


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#14 2012-09-02 13:18:44

nierro
Member
From: Milan, Italy
Registered: 2011-09-02
Posts: 849

Re: [SOLVED] How to read whether laptop is on AC or battery?

Glad it helps you, and me too wink
Well, can you tell me how will you set the right state after a suspend cycle? (you know, we now have /usr/lib/pm-utils/sleep.d/00powersave. But without pm-utils, how can we reach that?)
As i thought, udev can't handle suspend/hibernate. (yes i *was* studying big_smile )
So, we have to create a suspend script for systemd in /usr/lib/systemd/system-sleep/powersave and inside it, make a check whether we're on battery or ac and do the right thing.
EDIT: ok, if someone need it, here it is:

cat /usr/lib/systemd/system-sleep/00powersave
#!/bin/sh

case $1 in
    pre) /etc/powersave false ;;
    post)       
	if cat /sys/class/power_supply/ADP1/online | grep 0 > /dev/null 2>&1
	then
    		/etc/powersave true	
	else
    		/etc/powersave false
	fi
    ;;
esac
exit 0

Be aware that ADP1 is fine for my notebook, but probably not for yours (AC0 is quite often right).
And now, pm-utils is no more a dep of upower, please remove it!

pacman -Rdd pm-utils

is something i had always wait to do! And finally, i got it! wink
EDIT2: no, i did not get it indeed. It seems that xfce4 relies on pm-utils to suspend/hibernate, even if i'm using upower 0.9.18...Don't know why, but without pm-utils, i can't suspend/hibernate from menu, or closing the lid anymore.
I do not want to use acpid, or create buttons on my panel to run "systemctl suspend". And i want to go on using xfce4-power-manager. So i'll stick with pm-utils only as dep of upower and xfce, waiting for a patch for xfce-p-m.

Last edited by nierro (2012-09-02 14:04:56)

Offline

#15 2012-09-02 14:26:50

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] How to read whether laptop is on AC or battery?

That's something I can't test, sorry. I don't use a DE nor a power manager.

For me, it all works now. Unplug laptop -> powersave true. Plug back in -> powersave false. When I unplug and suspend, it wakes up with powersave true. When I suspend and then unplug, it wakes up and sets powersave true.

I only used upower to run my script and only had pm-utils for its pm-powersave function and its sleep function. Now I don't need either of it anymore, not do I need acpid to run pm-suspend when the lid is closed.


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#16 2012-09-02 14:31:28

nierro
Member
From: Milan, Italy
Registered: 2011-09-02
Posts: 849

Re: [SOLVED] How to read whether laptop is on AC or battery?

Are you using the "/usr/lib/systemd/system-sleep/00powersave" script or for you it magically works out of the box?

Offline

#17 2012-09-02 14:34:55

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] How to read whether laptop is on AC or battery?

┌─[jente @ lappy ~] 16:34:07 
└─■ cat /usr/lib/systemd/system-sleep/00powersave
cat: /usr/lib/systemd/system-sleep/00powersave: Bestand of map bestaat niet

Don't have it wink


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#18 2012-09-02 14:37:10

nierro
Member
From: Milan, Italy
Registered: 2011-09-02
Posts: 849

Re: [SOLVED] How to read whether laptop is on AC or battery?

Wow, I had to use it indeed!
very strange, well, as always, i'm not lucky enough to get everything work as expected smile

Last edited by nierro (2012-09-02 14:37:22)

Offline

#19 2012-09-02 14:40:54

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] How to read whether laptop is on AC or battery?

nierro wrote:

Wow, I had to use it indeed!
very strange, well, as always, i'm not lucky enough to get everything work as expected smile

I'm just glad I'm not that person this time lol

Happy you got it working after all!


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#20 2012-09-02 15:57:07

nierro
Member
From: Milan, Italy
Registered: 2011-09-02
Posts: 849

Re: [SOLVED] How to read whether laptop is on AC or battery?

I think this should be added somewhere in the wiki. I can do this, but...where can i add this? Under systemd page, or under laptop page?Or under pm-utils page?
EDIT:@unia: well, it seems on my netbook the udev rule is enough indeed...without the system-sleep script, it works! But why on my home laptop it needs that script? Well, as long as it work anyway, i won't mind wink

Last edited by nierro (2012-09-02 15:58:43)

Offline

#21 2012-09-02 16:36:44

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] How to read whether laptop is on AC or battery?

Yes, I suppose we could add this somewhere to the wiki. It's not systemd specific, and has nothing to do with pm-utils.

I think laptop would be a nice category, and perhaps make a a small intro on it on the systemd page which links to the full article?


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#22 2012-09-02 17:31:41

nierro
Member
From: Milan, Italy
Registered: 2011-09-02
Posts: 849

Re: [SOLVED] How to read whether laptop is on AC or battery?

Here it is: https://wiki.archlinux.org/index.php/La … _udev_rule
If anyone of you can improve it, any help is welcomed!

Offline

#23 2012-09-02 17:44:54

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] How to read whether laptop is on AC or battery?

Are you using one udev rule with two lines? I'm using two. Otherwise, it looks good. Only the spelling is a bit messy, but I guess that's because you're from Italy big_smile I don't want to offend you, but shall I fix that?


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#24 2012-09-02 18:14:56

nierro
Member
From: Milan, Italy
Registered: 2011-09-02
Posts: 849

Re: [SOLVED] How to read whether laptop is on AC or battery?

Yes, fix everything...nope it's not because i'm from italy, it's because i've been using my laptop all day to study and then to test that udev rule...i'm really tired wink
And yes, i use a two lines single rule.
Thanks!

Last edited by nierro (2012-09-02 18:15:17)

Offline

#25 2012-09-08 03:12:00

bwat47
Member
Registered: 2009-10-07
Posts: 638

Re: [SOLVED] How to read whether laptop is on AC or battery?

nierro/unia your script works great smile I may be able to get rid of pm-utils once I get that upower update (although I have a feeling gnome power manager will probably do the same thing xfce power manager is doing for you and require it anyway haha)

EDIT: Yeah, even with the new upower gnome and/or upower still needs pm-utils to work correctly. If I Rdd pm-utils then gnome will not suspend (just goes to black screen and I have to hard reset, also suspend option disappears from gnome status menu)

EDIT2: I also do not need the 00powersave script here. I can unplug (and it enabled powersave) > suspend > plug and when it wakes powersave is disabled. Good thing you already have an easy workaround script for those that it won't work for though!

Last edited by bwat47 (2012-09-09 15:59:07)

Offline

Board footer

Powered by FluxBB