Looking to add color coding and an alert to battery meter [SOLVED]

Add functionality to your desktop
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
User avatar
Logansfury
Level 8
Level 8
Posts: 2375
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Looking to add color coding and an alert to battery meter [SOLVED]

Post by Logansfury »

Hello everyone,

I've a conky with a battery meter that includes a "time remaining" readout. Here is the existing code:

Code: Select all

${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4" "$5}'}
$4 is the numeric value, $5 is the word hours or minutes.

When $4 drops to 25 I would like the text to change from white to yellow, and when $4 drops to 10 I would like the text to change from yellow to red.

Can an IF statement exist in a conkyrc or must that be in a bash script called upon by conky?

Thanks for reading,

Logan
Last edited by Logansfury on Tue May 21, 2024 2:23 am, edited 1 time in total.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 8
Level 8
Posts: 2079
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Looking to add color coding and an alert to battery meter

Post by Koentje »

I could help you, but i probably get banned from this forum in a few minutes... i called an a-hole moderator an a-hole! :roll:
Image
User avatar
Logansfury
Level 8
Level 8
Posts: 2375
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Looking to add color coding and an alert to battery meter

Post by Logansfury »

Koentje wrote: Mon May 20, 2024 4:03 pm I could help you, but i probably get banned from this forum in a few minutes... i called an a-hole moderator an a-hole! :roll:
I certainly hope that works itself out >.<

I just googled a page where someone is doing colored battery meters I think it may give me the answer, I will report back.

Page: https://stackoverflow.com/questions/156 ... ch-and-and
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 8
Level 8
Posts: 2375
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Looking to add color coding and an alert to battery meter

Post by Logansfury »

I think I have it!!!

Code: Select all

${if_match ${battery_percent BAT0} >=26} ${color1}${alignr}${offset -206}remaining: ${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4" "$5}'}${else}
${if_match ${battery_percent BAT0} <=25}${if_match ${battery_percent BAT0} >=11} ${color2}${alignr}${offset -206}remaining: ${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4" "$5}'}{endif}${else}
${if_match ${battery_percent BAT0} <=10} ${color3}${alignr}${offset -206}remaining: ${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4" "$5}'}${endif}${endif}${endif}
I am currently at discharging state, time remaining 45.4 minutes. It is displaying in white the correct color1.

Keeping an eye on screen for it to turn yellow at 25%
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 8
Level 8
Posts: 2375
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Looking to add color coding and an alert to battery meter

Post by Logansfury »

I needed to add two more ${endif} statements to the end.

In total, 1 for the first line, 2 for the second with two IF conditionals, and 1 for the last line equaling 4

Its coloring the text perfectly now
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 8
Level 8
Posts: 2079
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Looking to add color coding and an alert to battery meter

Post by Koentje »

Nice, you worked it out!

I also made a little battery widget for my laptop.

Image
Image
User avatar
Logansfury
Level 8
Level 8
Posts: 2375
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Looking to add color coding and an alert to battery meter

Post by Logansfury »

Koentje wrote: Mon May 20, 2024 5:39 pm Nice, you worked it out!

I also made a little battery widget for my laptop.

Image
That is beautiful!

I have encountered an issue however.

The original script:

Code: Select all

${alignr}${offset -72}${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "to\ full"}
${alignr}${offset -72}${voffset -20}${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4" "$5}'}
displays either "time to fill" or "time to empty"

With the color routine:

Code: Select all

${alignr}${offset -72}${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "to\ full"}
${if_match ${battery_percent BAT0} >=26} ${color1}${alignr}${offset -136}${voffset -20}remaining: ${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4" "$5}'}${else}
${if_match ${battery_percent BAT0} <=25}${if_match ${battery_percent BAT0} >=11} ${color2}${alignr}${offset -136}${voffset -20}remaining: ${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4" "$5}'}${else}
${if_match ${battery_percent BAT0} <=10} ${color3}${alignr}${offset -136}${voffset -40}remaining: ${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4" "$5}'}${endif}${endif}${endif}${endif}${endif}${endif}${endif}
Now that its charging, I have time to fill showing and instead of not appearing, I have a red "time to empty" on top of it.

How do I get the time to empty to know when to display please?
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 8
Level 8
Posts: 2375
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Looking to add color coding and an alert to battery meter

Post by Logansfury »

I just had a brainstorm. I will edit the other line into the IF ELSE bit.

Hopefully I am on the right track.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 8
Level 8
Posts: 2375
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Looking to add color coding and an alert to battery meter

Post by Logansfury »

No good. I lost my display of time remaining to fill and had a remaining that didn't belong.

Here is what didnt work:

Code: Select all

${if_match ${battery_percent BAT0} >=26}${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "to\ full"}${color1}${alignr}${offset -136}${voffset -20}remaining: ${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4" "$5}'}${else}
${if_match ${battery_percent BAT0} <=25}${if_match ${battery_percent BAT0} >=11}${voffset -20}${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "to\ full"}${color2}${alignr}${offset -136}${voffset -20}remaining: ${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4" "$5}'}${else}
${if_match ${battery_percent BAT0} <=10}${voffset -40}${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "to\ full"}${color3}${alignr}${offset -136}${voffset -40}remaining: ${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4" "$5}'}${endif}${endif}${endif}${endif}
and I just got another brainstorm. I will grep for both identifiers and print what is there~!

Stand by.....
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 8
Level 8
Posts: 2079
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Looking to add color coding and an alert to battery meter

Post by Koentje »

I already have that in my script! ;)
Schermafdruk van 2024-05-21 00-19-25.png
Schermafdruk van 2024-05-21 00-19-25.png (16.86 KiB) Viewed 368 times
Schermafdruk van 2024-05-21 00-22-11.png
Schermafdruk van 2024-05-21 00-22-11.png (17.33 KiB) Viewed 368 times
Image
User avatar
Logansfury
Level 8
Level 8
Posts: 2375
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Looking to add color coding and an alert to battery meter

Post by Logansfury »

No good.

I am out of ideas, I need help >.<
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 8
Level 8
Posts: 2375
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Looking to add color coding and an alert to battery meter

Post by Logansfury »

Koentje wrote: Mon May 20, 2024 6:24 pm I already have that in my script! ;)

Schermafdruk van 2024-05-21 00-19-25.png

Schermafdruk van 2024-05-21 00-22-11.png
I see. That is a .png that changes based on state?

I need to see if I can incorporate that into what I am doing.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 8
Level 8
Posts: 2079
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Looking to add color coding and an alert to battery meter

Post by Koentje »

Plus the last line of text tells:
if on charger, how long to full and
if discharging, how long to empty

Also just made an adjustment to the backgroundcolor of the empty part of the battery.. it was dark green, now it's grey. Looks a bit better.
Schermafdruk van 2024-05-21 00-36-46.png
Schermafdruk van 2024-05-21 00-36-46.png (15.89 KiB) Viewed 342 times
Image
User avatar
Logansfury
Level 8
Level 8
Posts: 2375
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Looking to add color coding and an alert to battery meter

Post by Logansfury »

Logansfury wrote: Mon May 20, 2024 6:31 pm
Koentje wrote: Mon May 20, 2024 6:24 pm I already have that in my script! ;)

Schermafdruk van 2024-05-21 00-19-25.png

Schermafdruk van 2024-05-21 00-22-11.png
I see. That is a .png that changes based on state?

I need to see if I can incorporate that into what I am doing.
May I see the script that controls that image display please Koentje?
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 8
Level 8
Posts: 2079
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Looking to add color coding and an alert to battery meter

Post by Koentje »

https://www.cobrasoft.nl/download/conky/laptop-battery
Wait... that is the old one! I have to update that one.. i'll give a heads up when it's done!
Image
User avatar
Logansfury
Level 8
Level 8
Posts: 2375
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Looking to add color coding and an alert to battery meter

Post by Logansfury »

Koentje wrote: Mon May 20, 2024 6:46 pm https://www.cobrasoft.nl/download/conky/laptop-battery
Wait... that is the old one! I have to update that one.. i'll give a heads up when it's done!
Right on, standing by!
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 8
Level 8
Posts: 2079
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Looking to add color coding and an alert to battery meter

Post by Koentje »

Image
User avatar
Logansfury
Level 8
Level 8
Posts: 2375
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Looking to add color coding and an alert to battery meter

Post by Logansfury »

Koentje wrote: Mon May 20, 2024 7:06 pm Go go go!

https://www.cobrasoft.nl/download/conky/laptop-battery/
Got it :)

Gonna see what I can apply to what im doing. I'll post results!
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 8
Level 8
Posts: 2375
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Looking to add color coding and an alert to battery meter

Post by Logansfury »

I believe I have it!! It's brute force but it works:

Code: Select all

${alignr}${offset -170}${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "state" | awk '{$1=$1; print}'}
${alignr}${offset -72}${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "to\ full"}
${alignr}${offset -156}${if_match "${exec cat /sys/class/power_supply/BAT0/status}" == "Discharging"}${if_match ${battery_percent BAT0} >= 26}${color1}remaining: ${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4" "$5}'}${endif}${endif}
${alignr}${offset -156}${if_match "${exec cat /sys/class/power_supply/BAT0/status}" == "Discharging"}${if_match ${battery_percent BAT0} <=25}${if_match ${battery_percent BAT0} >=11} ${color2}remaining: ${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4" "$5}'}${endif}${endif}${endif}
${alignr}${offset -156}${if_match "${exec cat /sys/class/power_supply/BAT0/status}" == "Discharging"}${if_match ${battery_percent BAT0} <=10} ${color3}remaining: ${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4" "$5}'}${endif}${endif}
when plugged in, the state IF was working and the word "Remaining" never appeared on screen. Now it's unplugged, and properly reading "Remaining: 1.4 hours" in white text.

The text should become orange at 25% and red at 10%
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 8
Level 8
Posts: 2375
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Looking to add color coding and an alert to battery meter

Post by Logansfury »

I pushed my luck further.

Now battery percentage is in white text when charging, yellow text when discharging.

The State display is now white when charging, yellow when discharging.

Time til full is always white

The time remaining text is yellow from 100-36% battery pwr, orange from 35-16% battery pwr, and red from 15%-0%

Code: Select all

conky.text = [[

#leet chars: ┤┤└ └ ┴ ┴ ┐┐│ │┘ ┘┌ ┌ ├ ├ ┬ ┬ ┼ ┼ ┴ ┴ ── ││ ▽▼△▲▵▴▾▿
${alignr}${voffset 16}${offset -216}${if_match "${exec cat /sys/class/power_supply/BAT0/status}" == "Charging"}${color1}${exec acpi | grep "Battery 0" | awk '{print $4}'}${battery_percent BAT0}%${endif}
${alignr}${voffset -13}${offset -216}${if_match "${exec cat /sys/class/power_supply/BAT0/status}" == "Discharging"}${color2}${exec acpi | grep "Battery 0" | awk '{print $4}'}${battery_percent BAT0}%${endif}
${alignr}${offset -170}${if_match "${exec cat /sys/class/power_supply/BAT0/status}" == "Charging"}${color1}${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "state" | awk '{$1=$1; print}'}${endif}
${alignr}${offset -170}${voffset -14}${if_match "${exec cat /sys/class/power_supply/BAT0/status}" == "Discharging"}${color2}${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "state" | awk '{$1=$1; print}'}${endif}
${alignr}${offset -72}${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "to\ full"}
${alignr}${offset -156}${voffset -20}${if_match "${exec cat /sys/class/power_supply/BAT0/status}" == "Discharging"}${if_match ${battery_percent BAT0} >= 36}${color2}remaining: ${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4" "$5}'}${endif}${endif}
${alignr}${offset -156}${if_match "${exec cat /sys/class/power_supply/BAT0/status}" == "Discharging"}${if_match ${battery_percent BAT0} <=35}${if_match ${battery_percent BAT0} >=16} ${color3}remaining: ${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4" "$5}'}${endif}${endif}${endif}
${alignr}${offset -156}${voffset -20}${if_match "${exec cat /sys/class/power_supply/BAT0/status}" == "Discharging"}${if_match ${battery_percent BAT0} <=15} ${color4}remaining: ${exec upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4" "$5}'}${endif}${endif}
]];
Last edited by Logansfury on Tue May 21, 2024 5:43 pm, edited 1 time in total.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
Post Reply

Return to “Compiz, Conky, Docks & Widgets”