Conky Wiki

Battery threshold example[]

This Lua script will execute the system 'beep' command when the battery value drops below a certain threshold. Use it like so (beeps when battery goes below 6%):

${lua_read_parse bat_warn 5 ${battery_percent BAT0}}

The Script[]

--
-- Lua scripting example to alert the user when battery percentage
-- goes below some threshold
--
-- usage in conky:
-- ${lua_read_parse bat_warn <alert_perc> ${battery_percent <battery>}}
--
-- Example: alert when BAT0 goes below 6 percent:
-- ${lua_read_parse bat_warn 5 ${battery_percent BAT0}}
--

local user_alerted = false
function conky_bat_warn(str)
	local alert_percent, percent = string.match(str, '^(%d+) (%d+)$')

	if percent <= alert_percent and not user_alerted then
		-- alert the user, put your command here
		os.execute('beep')

		user_alerted = true
	end

	-- reset alert if battery percentage goes above alert level
	if percent > alert_percent and user_alerted then
		user_alerted = false
	end

	-- display nothing in conky itself so returning empty string
	return ''
end