Reading Raspberry Pi CPU temp with Exec binding

I am trying to get the CPU temperature from my Raspberry Pi into an item using the Exec binding.

This can be achieved with this item definition:

Number System_Temperature_CPU "CPU temp [%.3f]" <temperature> (System) { exec="<[cat /sys/class/thermal/thermal_zone0/temp:60000:REGEX((.*?))]" }

This reads the temperature in millidegrees Celsius once a minute (every 60000 milliseconds).

However, I would like to get the temperature in full degrees, so divided by 1000.

This can be done in several ways. Here are some solutions:

echo $(cat /sys/class/thermal/thermal_zone0/temp) 1000 | awk '{ printf("%f\n", $1/$2) }'
echo "scale=3; $(cat /sys/class/thermal/thermal_zone0/temp)/1000" | bc -l

However, I cannot get these commands working into the item definition.

Can anyone help me with a solution to get this sorted out?

The easiest thing to do would probably be to use the JS transform rather than the REGEX transform. See @watou’s example here.

That being said, your commands may be having issues with binding because sometimes it doesn’t handle spaces properly. In that case replace your spaces with “@@”. For exmaple:

echo@@$(cat@@/sys/class/thermal/thermal_zone0/temp)@@1000@@|@@awk@@'{@@printf("%f\n",@@$1/$1)

You also many need to escape some of the special characters above (e.g. ', (, ), etc). Shunting the command off to a script would also be a good solution so openHAB just has to call your script.

I think though by far the simplest solution will be the JS transform.

I use cron job on the Pi side, format the output then send the data with curl to the openhab server which is on another machine

Thanks.
I have successfully used the JavaScript transformation

items file:

Number System_Temperature_CPU "CPU temperatuur [%.1f °C]" <temperature> (System) { exec="<[cat /sys/class/thermal/thermal_zone0/temp:30000:JS(milli.js)]" }

transform file: milli.js

(function(i){ return i / 1000; })(input)

sitemap

Text item=System_Temperature_CPU

Hi digineut!

I would like to ask you to be a bit more precise, because I’ve followed your commands, but I have no value shown on my sitemap. Alltrough, I’m a really beginner, so it could be, that I’ve made a mistake…
For example, where should I put the .js file? I’ve made a new JS file in the transforms folder…

Thanks!

robertito

I don’t now exactly what information you are looking for, so pls find a small addition:

  • I added the Number line to my system.items file in the
    items folder
  • I created a new milli.js file in the transform folder and added the
    function line to that
  • I added the Text line to my default.sitemap file.

I suggest you paste some of the contents of your items, js and sitemap
files here, so we can have a look.

Dear digineut,

thank you for your help, now it works! I’m getting more familiar with it. Now, I have to figure out, how to start it on boot.

I have created a page in the wiki to document this use case example: Raspberry Pi System Temperature.

Does these instructions work with OpenHab2 also? I followed the wiki instructions but temperature is not read. Only get “-” in the sitemap.

It should work. I have not tested it yet myself. However, the exec commands depend on your RPi so those should still work. You can test these from a regular command line first. Both transformations should work as well.

If you need help, please post your confit in the forums and I will see what I can do.

For your information. The new Systeminfo binding also supports CPU temperature. I have only tested this binding with my Mac and not yet with the RPi. But this could be a more sustainable implementation.

I tested the new systeminfo binding, it displays 0° as well. I think is related to the RPi & maybe access rights? I’m using a RPi 3 with Jessie.

Both Linux commands $ cat /sys/class/thermal/thermal_zone0/temp and $ /opt/vc/bin/vcgencmd measure_temp are working and I also added the openhab user to the video group.

I am no Linux expert so I’m not sure if I can solve this issue for you.

Have you already tried to run it from OH1 on the same RPi? What were the results?

And. Have you tried warching the event log? I am aware that the new basic UI has issues with refreshing. Refreshing the browser might help. Or try the classic UI.

Could you please copy your item and sitemap definitions in the thread?

@rtvb here are my configs …

items:
Group System_Temperature_Chart (System, Charts) Number System_Temperature_Chart_Period "Periode" (System) Number System_Temperature_CPU "Temperature CPU [%.1f C]" <temperature> (System_Temperature_Chart) { exec="<[cat /sys/class/thermal/thermal_zone0/temp:10000]" }

sitemap:
Text item=System_Temperature_CPU

I removed all the chart and GPU stuff, only display the CPU temperature. For testing I also removed the millis.js transformation.

The first thing I noticed was the missing transformation. As far as I
know, you should Always add one; at least a REGEX that matches the
complete contents.

Please try

Number System_Temperature_CPU "Temperature CPU [%.1f   C]" <temperature> (System_Temperature_Chart) { exec="<[cat /sys/class/thermal/thermal_zone0/temp:10000:REGEX((.*?))]"

I got it working. I switched on debug log for the exec binding and noticed it does not log anything. I uninstalled the binding, restarted OH and installed the binding again. Fixed.

1 Like

Hey, rtvb. I see you got that working. I followed your steps but no success on my rpi temperature displaying. Could you give a clue? I did the exect same thing as you mentioned (using the .js file on tranformation). I also tried the first item config you posted (the one with REGEX). I tried the vcgencmd measure_temp command from Raspian. Don t I have to configure a .thing file? Could you help me?

Any one with hints on this?

Regards!

You don’t need a thing if you use the exec1 binding.

Are you sure the vcgencmd command is working on the command line?
If so, and it is not working in openHAB, it might be a permissions issue.
The openHAB user (depending on your installation, but probably is openhab.
That user should be a member of the video group (as per my wiki item).
Can you confirm this?

My test:

$ getent group video
video:x:44:openhabian

Tks for the reply. VCGENCMD command is working on command line - when I type vcgencmd measure_temp I get the following return temp=48.3 C. About the permission, when i type getent group video as you suggested I get the following return video:x:44:openhabian.

$ vcgencmd measure_temp
temp=49.4'C
getent group video
video:x:44:openhabian

My sitemap file looks like this:

        Frame {
                Text item=CPU_Temp

So far, I`ve tried the following item lines under the CPU_Temp.items file:

Number CPU_Temp "Temperature CPU [%.1f °C]" <temperature> (System_Temperature_Chart) { exec="<[cat /sys/class/thermal/thermal_zone0/temp:10000]" }

String CPU_Temp "Temperature CPU [%.1f °C]" <temperature> (System_Temperature_Chart) { exec="<[cat /sys/class/thermal/thermal_zone0/temp:10000]" }

String CPU_Temp "Temperature CPU [%.1f °C]" <temperature> (System_Temperature_Chart) { exec="<[cat vcgencmd measure_temp" }

Number CPU_Temp "Temperature CPU [%.1f °C]" <temperature> (System_Temperature_Chart) { exec="<[cat vcgencmd measure_temp" }

I`d appreciate if you could give me a hand here. Thanks in advance for your help!

Neither of your item definitions has a transformation included. I don’t think you followed my example. Please try to add a transformation for example.

Number CPU_Temp "Temperature CPU [%.1f °C]" <temperature> (System_Temperature_Chart) { exec="<[cat /sys/class/thermal/thermal_zone0/temp:10000:REGEX((.*?))]" }