Dynamic Colour Changes in Conky

Conky is a popular system monitor that I have blogged about before. Its advantages are that it can be whatever you want it to be. You decide what is displayed and the format and so much more.
I’ve got a couple of Conkys on my regular desktop. The main one displays system info and is the one I’m updating today. The other one displays the Now Playing details. It works with both Amarok and Clementine as I switch between them all the time. It only appears when either is in use.
The main Conky includes battery information for both the batteries in my Thinkpad. The information includes the current charge level of each battery, the time remaining on the battery that is currently being used and the status of the batteries i.e. if they are discharging or charging. There is an additional line that shows whether the AC Adapter is attached or not, actually whether it is supplying power or not.
It all has worked well for a while but I wanted the battery information to change colour dependent on the current charge level. A search gave me a number of similar methods of achieving this.
Conky battery status

All these methods had one thing in common, they used Conky’s battery_percent variable but I prefer and use the battery variable. The reason they use the battery_percent variable is simple, battery_percent has a numeric output and therefore the if_match command is easy to use to match the battery charge level. I prefer the battery variable as it outputs status as well as the charge level.
After some experimenting I found a simple solution. I use the battery_percent variable in the if_match statement but use the battery variable for the output. The line from my conkyrc is below. It is all one line in the conkyrc and the second line is the same with BAT) set to BAT1.

$font${color1}Battery 1: $color${if_match ${battery_percent BAT0}<=15}${color7}${blink ${battery BAT0}}${color}${else}${if_match ${battery_percent BAT0}<=25}${color8}${battery BAT0}${else}${if_match ${battery_percent BAT0}>25}${color}${battery BAT0}${endif}${endif}${endif}, ${battery_time BAT0}${color}

The if_match variable looks for 3 different battery charge levels. Above 25% it shows in the regular text colour I use through most of the display. When it reaches 25% the line changes to gold text. A third check looks for a charge of less than 15%. At that point the line of text turns red and starts to blink.

Author: Jim

A sixty something living in the Hawkesbury Valley on the edge of Sydney Australia.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.