How to show the Raspberry Pi CPU temperature?

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?

all components listed under home-assistant.io/components will work with HassIO (unless there is something specifically noted as not compatible, but i dont think any exist like that, at least not that I have seen)

However after having a quick look at glances it appears you will need to SSH into HassIO and start the server which is a little more involved than most components. Shouldn’t be an issue, just give it a try.

Yes, that should be possible but I don’t want to mess with my nice hass.io installation too much… :pensive:

so run another RPi for the Glances server…

@sapnho, f you don’t want to mess with glances have a look at this component.

Nothing more than yaml configuration required.

3 Likes

Thank @tom_l, good idea!