1

So I'm getting to get the CPU core temperature using sensors command.

Inside conky, I wrote

$Core 0 Temp:$alignr${execi 1 sensors | grep 'Core 0' | awk {'print $3'}} $alignr${execibar 1 sensors | grep 'Core 0' | awk {'print $3'}}

Each second I'm running the exact same command sensors | grep 'Core 0' | awk {'print $3'} in two places for exact same output. Is there is a way to hold the output inside a variable and use that variable in place of the commands.

coder_86
  • 95
  • 2
  • 13

1 Answers1

1

conky does not have user variables. What you can do instead is call lua from conky to do this for you. The lua language is usually built-in to conky by default, so you need only put some code in a file, include the file in the conky setup file, and call the function. For example, these shell commands will create a test:

cat >/tmp/.conkyrc <<\!
conky.config = {
    lua_load = '/tmp/myfunction.lua',
    minimum_height = 400,
    minimum_width = 600,
    use_xft = true,
    font = 'Times:size=20',
};
conky.text = [[
 set ${lua myfunction t ${execi 1 sensors | awk '/^Core 0/{print 0+$3}'}}°C
 get ${lua myfunction t}°C ${lua_bar myfunction t}
]]
!
cat >/tmp/myfunction.lua <<\!
vars = {}
function conky_myfunction(varname, arg)
 if(arg~=nil)then vars[varname] = conky_parse(arg) end
 return vars[varname]
end
!
conky -c /tmp/.conkyrc -o 

In the myfunction.lua file, we declare a function myfunction() (which needs to be prefixed conky_ so we can call it from conky). It takes 2 parameters, the name of a variable, and a conky expression. It calls conky_parse() to evaluate the expression, and saves the value in a table vars, under the name provided by the caller. It then returns the resulting value to the caller. If no expression was given, it will return the previous value.

In the conky.text the line beginning set calls myfunction in lua with the arbitrary name of a variable, t, and the execi sensors expression to evaluate, save, and return. The line beginning get calls myfunction to just get the value.

lua_bar is similar to exec_bar, but calls a lua function, see man conky. However, it expects a number without the leading + that exec_bar accepts, so I've changed the awk to return this, and have added the °C to the conky text instead.

meuh
  • 11,500
  • 2
  • 29
  • 45
  • Looks good, but it'd probably be better to name the function something like `conky_var` or `conky_variable` rather than `conky_myfunction` to make it's purpose more easily distinguishable in the conky config file. – David Yockey May 19 '22 at 22:53
  • Sure. I just used *myfunction* as a place holder to make it stand out in the code, and would expect the user to replace this with a name more meaningful to them. – meuh May 20 '22 at 08:09