How to show the Raspberry Pi CPU temperature?

Here are a couple of quick and dirty sensors if you’d like to convert CPU / GPU temps from Celsius to Fahrenheit:

- platform: command_line
  name: cpu_temperature
  command: "cat /sys/class/thermal/thermal_zone0/temp | awk '{print ((($1*.001) * 9 / 5))+32}'"
- platform: command_line
  name: gpu_temperature
  command: "/opt/vc/bin/vcgencmd measure_temp | cut -c 6- | head -z --bytes -3 | awk '{print (($1 * 9 / 5))+32}'"

Might be a better way to do this, but seems to be working so far!

3 Likes

That is how I do it. And if you are worried you can add a little fan pretty easily to the CPU. (I did for my HA rPi)

1 Like

Hi,

I tried your solutions, because I wanna monitoring the temperature of another raspberry.
The problem is my other raspberry has a password…
How can I integrate the password in the command ?

Thank you !!

i came across this challenge a while back and dont think it is possible. it violates the key security principle anyways putting password in plain text. you can however exchange certificates somehow (too complicated for me to remember) between the two raspberry pi’s to avoid specifying password.

i would rather just post MQTT status updates from the other raspberry pi and pick them up within home assistant.

i suppose the vcgencmd cannot work in hassio (it doesnt for me). any alternative there?

cat /sys/class/thermal/thermal_zone0/temp
1 Like

For the GPU temperature this works:

- platform: command_line
  name: GPU Temperature
  command: "/opt/vc/bin/vcgencmd measure_temp"
  unit_of_measurement: "°C"
  value_template: '{{ value | multiply(0.001) | round(1) }}'

but gives the following badly formatted output: temp=32.7'C °C

I tried this:

- platform: command_line
  name: GPU Temperature
  command: "/opt/vc/bin/vcgencmd measure_temp | cut -c 6- | head -z --bytes -3"
  unit_of_measurement: "°C"
  value_template: '{{ value | multiply(0.001) | round(1) }}'

but it gives unknown as the output.

Any idea how I can format this correctly?

Argh! So close:

- platform: command_line
  name: GPU Temperature
  command: "/opt/vc/bin/vcgencmd measure_temp | tr -d '[:alpha:]' | sed 's/[<>]//g'"
  unit_of_measurement: "°C"
  value_template: '{{ value | multiply(0.001) | round(1) }}'

gives =33.8' °C

# CPU Temperature
  - platform: command_line
    name: CPU Temperature
    command: "cat /sys/class/thermal/thermal_zone0/temp"
    unit_of_measurement: "°C"
    value_template: '{{ value | multiply(0.001) | round(1) }}'

I use this for teh CPU

maybe add a | replace("’", “”)

value_template: ‘{{ value | multiply(0.001) | round(1) | replace("’", “”) }}’

2 Likes

I’ve got the CPU temp fine using this:

- platform: command_line
  name: CPU Temperature
  command: "cat /sys/class/thermal/thermal_zone0/temp"
  unit_of_measurement: "°C"
  value_template: '{{ value | multiply(0.001) | round(1) }}'

I just cant work out how to remove the extra string bits this GPU temp command returns:

/opt/vc/bin/vcgencmd measure_temp

it outputs a string of the form:

temp=30.4'C

I just need to extract the floating point number from that.

value_template: ‘{{ value | multiply(0.001) | round(1) | replace(“’”, “”) | replace(“temp=", “”) }}’

Invalid config for [sensor.command_line]: invalid template (TemplateSyntaxError: unexpected char ‘“’ at 54) for dictionary value @ data[‘value_template’]. Got ‘‘{{ value | multiply(0.001) | round(1) | replace("’", “”) |replace(“temp=”, “”) }}’’.

See my edit. I had a “ before replace(Temp-=

Weird. I’m getting fancy double and single quotes when I copy and paste your example.

So I typed this out:

value_template: '{{ value | multiply(0.001) | round(1) | replace("'", "") | replace("temp=", "") }}'

But HA is still not happy.

Invalid config for [sensor.command_line]: invalid template (TemplateSyntaxError: unexpected char ‘“’ at 54) for dictionary value @ data[‘value_template’]. Got ‘‘{{ value | multiply(0.001) | round(1) | replace("’", “”) | replace(“temp=", “”) }}’’.

Sometimes the forum does funny things with single inverted commas… you see the logic of the replace command though. I’m using similar command line sensors and value templates without any issue…

I do see your logic. I’ve been stripping it down to test one thing at a time and it seems HA has a problem with this:

replace("'", "")

it throws this error right at the single quote:

expected <block end>, but found '<scalar>' in "/config/sensors.yaml", line 64, column 41

Oh wow. You have to escape a single quote with a single quote in YAML!

http://yaml.org/spec/current.html#id2534365 (look at Example 4.57. Single Quoted Quotes)

This works:

- platform: command_line
  name: GPU Temperature
  command: "/opt/vc/bin/vcgencmd measure_temp"
  unit_of_measurement: "°C"
  value_template: '{{ value | replace("temp=", "") | replace("''C", "") }}' 

Thanks for putting me on the right track David.

2 Likes

Good Grief!!!

You’re Welcome.

This is how I rounded F to an integer and added an “F”

  - platform: command_line
    name: cpu_temperature
    command: "cat /sys/class/thermal/thermal_zone0/temp | awk '{printf \"%.0fF\", ((($1*.001) * 9 / 5))+32}'"

Does Glances (https://www.home-assistant.io/components/sensor.glances/) already work for Hass.io?