You are not logged in.

#1 2023-01-27 17:36:30

hully
Member
Registered: 2022-11-14
Posts: 161

Can't get notification for battery

I have put this into `/etc/udev/rules.d/99-lowbat.rules`

SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="[16-100]", RUN+="dunstify --urgency=normal 'Battery discharging'"

`sudo udevadm monitor --property` shows:

UDEV  [17599.502111] change   /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0A:00/power_supply/BAT1 (power_supply)
ACTION=change
DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0A:00/power_supply/BAT1
SUBSYSTEM=power_supply
POWER_SUPPLY_NAME=BAT1
POWER_SUPPLY_TYPE=Battery
POWER_SUPPLY_STATUS=Discharging
POWER_SUPPLY_PRESENT=1
POWER_SUPPLY_TECHNOLOGY=Li-ion
POWER_SUPPLY_CYCLE_COUNT=0
POWER_SUPPLY_VOLTAGE_MIN_DESIGN=11550000
POWER_SUPPLY_VOLTAGE_NOW=11429000
POWER_SUPPLY_CURRENT_NOW=2149000
POWER_SUPPLY_CHARGE_FULL_DESIGN=4590000
POWER_SUPPLY_CHARGE_FULL=4026000
POWER_SUPPLY_CHARGE_NOW=2435000
POWER_SUPPLY_CAPACITY=60
POWER_SUPPLY_CAPACITY_LEVEL=Normal
POWER_SUPPLY_MODEL_NAME=AP20CBL
POWER_SUPPLY_MANUFACTURER=COSMX
POWER_SUPPLY_SERIAL_NUMBER=3807
SEQNUM=6606
USEC_INITIALIZED=17599501914

But still I don't get any notification for the change in battery level?

Offline

#2 2023-01-27 17:43:15

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

Re: Can't get notification for battery

You need to add quite a bit to that udev rule.  I doubt you want to run your graphical notification tool as root, you'll need it to run as your user.  You'll also need to ensure the relevant environment variables (e.g., DISPLAY and XAUTHORITY) for the dunstify process.

Start here:
https://wiki.archlinux.org/title/Udev#T … _udev_rule

EDIT: actually that section of the wiki is a bit odd.  It provides two different ways of acheiving the goal but seems to imply both should be used which is most definitely not the case.  You can export variables and su from the udev rule, or from a wrapper script.

Last edited by Trilby (2023-01-27 17:48:19)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2023-01-27 17:43:21

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 23,290

Re: Can't get notification for battery

A udev rule runs as root, for a notification from a root process to reach your user session you minimally need to have DBUS_SESSION_BUS_ADDRESS of your user session ("unix:path=/run/user/1000/bus" by default  or whatever your uid is if it isn't 1000) exported and maybe DISPLAY=:0 as well e.g.

SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="[16-100]", ENV{DBUS_SESSION_BUS_ADDRESS }="unix:path=/run/user/1000/bus", ENV{DISPLAY}=":0", RUN+="dunstify --urgency=normal 'Battery discharging'"

Offline

#4 2023-01-27 19:56:16

hully
Member
Registered: 2022-11-14
Posts: 161

Re: Can't get notification for battery

Thanks.

I changed it like so but it still isn't working:

❯ cat 99-lowbat.rules 

# Suspend the system when battery level drops to 5% or lower
# taken from: https://wiki.archlinux.org/title/Laptop#Hibernate_on_low_battery_level
SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="[16-100]", ENV{DBUS_SESSION_BUS_ADDRESS}="unix:path=/run/user/1000/bus", ENV{DISPLAY}=":0", RUN+="dunstify --urgency=normal 'Battery discharging'"
SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="[0-5]", RUN+="/usr/bin/systemctl poweroff -i"

Offline

#5 2023-01-27 20:05:00

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

Re: Can't get notification for battery

There's still no XAUTHORITY there, but rather than guessing, check the output.  I'm not sure if udev actions are logged to the journal, you could check there, or if it were me I'd just add logging to the command - e.g., call a shell to invoke dunstify and redirect stdout/stderr to a file.  It can be easiest to do this with a wrapper script to avoid getting tangled up with the nested quoting that'd be required.

Also, according to the man page, your RUN parameter needs to use the full path to dunstify (as it is not in /usr/lib/udev/).


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#6 2023-01-27 20:08:02

Head_on_a_Stick
Member
From: The Wirral
Registered: 2014-02-20
Posts: 8,333
Website

Re: Can't get notification for battery

Change the RUN bit to

RUN+="/usr/bin/su $user -c '/usr/bin/dunstify --urgency=normal 'Battery discharging''"

Replace $user with the actual user name. Not sure about those nested quotes though. Might be best to run dunstify in a script and call the script as your user with su.

EDIT: added full path to dunstify as well.

Last edited by Head_on_a_Stick (2023-01-27 20:14:51)


Para todos todo, para nosotros nada

Offline

#7 2023-01-27 20:18:01

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

Re: Can't get notification for battery

In HoaS's version, the full path to dunstify should not be needed as the full path to su is provided, and su itself invokes a shell (when the -c flag is used).  For testing purposes, just get rid of the space so quoting issues can be ruled out for the moment:

RUN+="/usr/bin/su YOURUSERHERE -c 'dunstify --urgency=normal BatteryDischarging > ~/udev_rule.log 2>&1'"

Then check for the udev_rule.log file after the rule should have run.

Last edited by Trilby (2023-01-27 20:18:39)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#8 2023-01-28 01:42:29

hully
Member
Registered: 2022-11-14
Posts: 161

Re: Can't get notification for battery

So this rule is triggered:

SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", RUN+="/usr/bin/touch /home/USERNAME/Documents/battery.triggered'"

But this rule isn't:

SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="[0-100]", RUN+="/usr/bin/touch /home/USERNAME/Documents/battery.triggered'"

I don't think those square brackets are a valid syntax?

Offline

#9 2023-01-28 01:59:21

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

Re: Can't get notification for battery

Ah, I didn't even look at that.  No, there is no such syntax there - the rule only runs if the attribute string matches what you put in quotes.

You may just want a wrapper script to be called whenever the AC is plugged/unplugged, and the script can then check the battery level and act appropriately based on the level.

Last edited by Trilby (2023-01-28 02:12:21)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#10 2023-01-29 14:51:19

hully
Member
Registered: 2022-11-14
Posts: 161

Re: Can't get notification for battery

Yes, I thought that "[0-5]" means "numbers between 0 and 5". Instead it's a regex.

I will update the wiki when I find time.

Offline

#11 2023-05-31 04:22:22

andrhua
Member
Registered: 2019-10-23
Posts: 17

Re: Can't get notification for battery

Excuse me for a bit of necroposting, but just in case somebody stumbles upon this, udev manpage explicitly states what bracket syntax means:

"[]"
           Matches any single character specified within the brackets.
           For example, the pattern string "tty[SR]" would match either
           "ttyS" or "ttyR". Ranges are also supported via the "-"
           character. For example, to match on the range of all digits,
           the pattern "[0-9]" could be used. If the first character
           following the "[" is a "!", any characters not enclosed are
           matched.

Offline

Board footer

Powered by FluxBB