You are not logged in.

#1 2025-08-18 12:39:33

Gargantuar
Member
Registered: 2025-02-26
Posts: 11

udev rules wrongly triggered when charging, not discharging

To prolong my battery life, I created a udev rule which hibernates my laptop when falling under 30%, as per the wiki:

/etc/udev/rules.d/99-lowbat.rules
-----
# Suspend the system when battery level drops to 30% or lower
# For some reason, battery is already empty at ~23%
SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="[0-2][0-9]", RUN+="/usr/bin/systemctl hibernate"

Weirdly enough, it's not triggered when discharging, but when charging: when plugging in my charger, my laptop freezes for a second, then hibernates.

The same happens with the udev rules test on the same wiki page:

/etc/udev/rules.d/98-discharging.rules
-----
SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", RUN+="/usr/bin/touch /home/archie/discharging"

The file "discharging" is created when plugging in my charger (`cat /sys/class/power_supply/BAT0/status` says "Charging").

If it helps, I run Wayland (swayfx). Let me know if you need any other information.

Offline

#2 2025-08-18 14:47:45

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 69,141

Re: udev rules wrongly triggered when charging, not discharging

Does /home/archie/discharging frequently update (w/ level changes) or only when you un/plug the charger?

Online

#3 2025-08-18 16:33:58

Gargantuar
Member
Registered: 2025-02-26
Posts: 11

Re: udev rules wrongly triggered when charging, not discharging

seth wrote:

Does /home/archie/discharging frequently update (w/ level changes) or only when you un/plug the charger?

I don't quite understand what you mean with "level changes", but yes, the creation timestamp is changed every time I plug/unplug the charger.

Offline

#4 2025-08-18 19:34:28

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 69,141

Re: udev rules wrongly triggered when charging, not discharging

When the battery drops from eg. 32% to 31%, does the udev rule trigger at this point?
A lot of batteries don't cause ACPI events for level changes > 5% or 10% at all and then the entire approach is moot and you'll have to frequently poll the charge and act based on that.

R/n I'd say that's the case and you *only* get events when un/plugging the charger which happen on the previous charging state ie. you'd actually fork off a script that tests the charging status after a couple of seconds and acts according to that, but again: completely moot when you're not getting level change events (at 30%) anyway.

Online

#5 2025-08-18 20:57:28

Gargantuar
Member
Registered: 2025-02-26
Posts: 11

Re: udev rules wrongly triggered when charging, not discharging

seth wrote:

When the battery drops from eg. 32% to 31%, does the udev rule trigger at this point?
A lot of batteries don't cause ACPI events for level changes > 5% or 10% at all and then the entire approach is moot and you'll have to frequently poll the charge and act based on that.

R/n I'd say that's the case and you *only* get events when un/plugging the charger which happen on the previous charging state ie. you'd actually fork off a script that tests the charging status after a couple of seconds and acts according to that, but again: completely moot when you're not getting level change events (at 30%) anyway.

To clarify, this is the surprising thing: the hibernation kicks in when the battery is below 30% and I'm plugging in my charger. If the battery is 30% or above, then plugging in my charger only changes the charging status, no hibernation. As for the udev test rule, the "discharging" file is written even when my battery is full, but is only rewritten when I plug in my charger. Seems that udev rules are triggered, but for the wrong `status`.

Offline

#6 2025-08-18 21:02:27

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 69,141

Re: udev rules wrongly triggered when charging, not discharging

I pretty much got that.
The point is, if you're not getting events while the system is discharging from 32% to 31% to 30%to 29%  … you cannot use this system to enter hibernation when the charge level drops below 30% anyway.
A rules that *only* triggers when you un/plug the charger doesn't help you tbw, right?

Online

#7 2025-08-19 17:33:55

Gargantuar
Member
Registered: 2025-02-26
Posts: 11

Re: udev rules wrongly triggered when charging, not discharging

seth wrote:

When the battery drops from eg. 32% to 31%, does the udev rule trigger at this point?
A lot of batteries don't cause ACPI events for level changes > 5% or 10% at all and then the entire approach is moot and you'll have to frequently poll the charge and act based on that.

R/n I'd say that's the case and you *only* get events when un/plugging the charger which happen on the previous charging state ie. you'd actually fork off a script that tests the charging status after a couple of seconds and acts according to that, but again: completely moot when you're not getting level change events (at 30%) anyway.

Ah I see, so you're saying that since my battery doesn't trigger ACPI events when only the percentage changes, udev rules are never triggered.

In that case, which tool does provide scripts/battery polling to make my life easier? I already have tlp, but there is no functionality of hibernating at a specific battery level. But then there is also laptop mode tools and upower. What do you recommend?

Offline

#8 2025-08-19 19:15:35

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 69,141

Re: udev rules wrongly triggered when charging, not discharging

I think https://man.archlinux.org/man/batsignal.1 polls the battery and you could abuse the command to run a script that inspects the battery level and triggers radom actions from there.
Or you you just run some background script that sources /sys/class/power_supply/BAT0/uevent checks status and charge and then acts accordingly or sleeps for the next minute or so.

Online

Board footer

Powered by FluxBB