Executing sensors command (lm-sensor) in Lua

334 Views Asked by At

I originally have this Lua script

function temp_watch()

    warn_value=60
    crit_value=80

    temperature=tonumber(conky_parse("${hwmon 1 temp 1}"))
    
    if cpu_tmp<warn_value then
        settings_table[1]['fg_colour']=normal
    elseif cpu_tmp<crit_value then
        settings_table[1]['fg_colour']=warn
    else
        settings_table[1]['fg_colour']=crit
    end
end

but for some reason, hwmon 1 temp 1 is just stuck reporting 25C. For this reason I switched to sensors. In conky I am executing it using this:

${exec sensors | grep 'Package id 0' | cut -d ' ' -f 5 | cut -c 2,3,4,5,6,7}

I tried using this solution: https://unix.stackexchange.com/questions/666250/how-to-use-conky-variable-with-external-command. Basically, replacing the temperature=tonumber... to

temperature=tonumber(conky_parse("${eval $${exec sensors | grep 'Package id 0' | cut -d ' ' -f 5 | cut -c 2,3,4,5,6,7}}"))

I also tried this: is it possible to pipe output from commandline to lua?. Replaced temperature=tonumber... to

local cpu_tmp = io.popen("exec sensors | grep 'Package id 0' | cut -d ' ' -f 5 | cut -c 2,3,4,5,6,7")
temperature=tonumber(cpu_tmp)

Both outputted this error: llua_do_call: function conky_main execution failed: /home/joe/conky/conky-grapes/rings-v2_gen.lua:530: attempt to compare nil with number

am I missing some variable conversion or is there any other syntax to execute bash in lua?

Thanks in advance :-)

1

There are 1 best solutions below

0
On

lua.org gave me an answer. The Complete I/O Model.

local file= io.popen("sensors -u | awk '/temp1_input:/ {print $2; exit}'")
local temperature = tonumber(file:read('*a'))
     <SOME CODE HERE>
file:close()
end

It is not elegant but it seems to work. I will be running the lua script for quite some time to ensure it wont have that too many open files error

Many thanks to @Fravadona for pointing me in the right direction :-)