How to show the Raspberry Pi CPU temperature?

I’m in the US, so I know F, but all chip spec’s are in C, and I would actually prefer that also. However no matter what I tried I never could get a sensible value in C with my primary HA unit of measure set to F, I gave up on it.

It took some tinkering but here is the CPU temp in °F.

sensor:
  - platform: command_line
    name: CPU Temperature
    command: "cat /sys/class/thermal/thermal_zone0/temp"
    unit_of_measurement: "°F"
    value_template: "{{ value | multiply(0.001) | multiply(1.8) | round(1) | float + 32}}"
1 Like

I’ve gotten the Raspberry Pi temperature into HA using the old template format, but I can’t work out how to do it in the modern format. I’d like to try to stay with the modern format. Can anyone point out what I’m doing wrong?

Working

sensor:
  - platform: systemmonitor
    resources:
      - type: processor_temperature

Not working
First Attempt

  - sensor:
      - name: "Pi.4 Temperature"
        state: "{{ systemmonitor.processor_temperature }}"
        unit_of_measurement: "°C"

Second Attempt

  - sensor:
      - name: "Pi.4 Temperature"
        state: "{{ states('systemmonitor.processor_temperature') }}"
        unit_of_measurement: "°C"

Third Attempt

  - sensor:
      - name: "Pi.4 Temperature"
        state: "{{ state_attr('systemmonitor', 'processor_temperature') }}"
        unit_of_measurement: "°C"
1 Like

Check in developers tools that you have the correct sensors called out.

Mine in developers tools is sensor.processor_temperature. I can get the correct temperature just using this sensor. No state or attributes needed for me.

@bschatzow thanks for the reply. If I remove the “working” code from my configuration.yaml the processor_temperature is no longer available. None of the other System Monitor states are available either, unless I explicitly add them.

It works fine with the old sensor format, I mostly want to learn about how HA works better. I can’t find any documentation that helps convert between legacy and modern format, and the System Monitor page still uses the old format.

Maybe I am not following your question. How did you create your sensor? From there look at the sensor in developers tools and you should be able to see what is necessary to get the temperature value. Do you have a sensor.processor_temperature?

I followed this guide to get a lot more showing from my Raspberry Pi 4:

1 Like

Interesting approach @wwwescape, running a daemon that sends the data to HA is interesting, and provides additional data over the system monitor package.

@bschatzow unfortunately my post where I said I worked it out was incorrect. I had forgotten to remove the working old format sensor

sensor:
  - platform: systemmonitor
    resources:
      - type: processor_temperature

That creates this sensor in developer tools.

Unfortunately none of these modern formats below seem to work. I wonder if somehow need to reference “state_class” or “device_class”? Once I remove the old format sensor that all disappears. I’ve tried all sorts of variations but I don’t understand HA / the syntax well enough, I’m just guessing.

First Attempt

  - sensor:
      - name: "Pi.4 Temperature"
        state: "{{ sensor.processor_temperature }}"
        unit_of_measurement: "°C"

Second Attempt

  - sensor:
      - name: "Pi.4 Temperature"
        state: "{{ states('sensor.processor_temperature') }}"
        unit_of_measurement: "°C"

Third Attempt

  - sensor:
      - name: "Pi.4 Temperature"
        state: "{{ state_attr('sensor', 'processor_temperature') }}"
        unit_of_measurement: "°C"

The “new” format for the template sensors has nothing to do with the systemmonitor sensor.
You don’t need a template sensor at all for this.

Just keep the configuration for the systemmonitor as it is.

If you really need a additional template sensor, for example to convert to Fahrenheit:

template:
  - sensor:
      - name: "Temperature in Fahrenheit"
        unique_id: temperature_fahrenheit
        device_class: temperature
        unit_of_measurement: "°F"
        state: "{{ ((states('sensor.processor_temperature') | float(0)) * 9/5) + 32 }}"

Thanks @dennis84de. There’s a bit of a learning curve with HA. I declared a sensor for system monitor, thought it might be nice to have it match the newer format, but some sensors require the old format? Or they’re a different kind of sensor? I’ll leave things as they are, working, but I’d like to understand more about this if you have time to explain it :slight_smile:

The new / old format only refers to template sensors.

In the old way the template sensors were (and still can be) defined under the sensor domain:

sensor:
  - platform: template
    sensors:
      temperature_fahrenheit:
        friendly_name: "Temperature in Fahrenheit"
        unit_of_measurement: "°F"
        value_template: "{{ ((states('sensor.processor_temperature') | float(0)) * 9/5) + 32 }}"    

For some time now there is the template domain for templated sensors or other entities:

template:
  - sensor:
      - name: "Temperature in Fahrenheit"
        unique_id: temperature_fahrenheit
        device_class: temperature
        unit_of_measurement: "°F"
        state: "{{ ((states('sensor.processor_temperature') | float(0)) * 9/5) + 32 }}" 

Other integrations can still be configured under the sensor domain:

sensor:
  - platform: systemmonitor

And other integrations can be configured under their own domain and still create a sensor entity as a result:

zodiac:
2 Likes

Thanks Dennis, appreciate you taking the time to explain that :slight_smile:

using Docker instead of SSH removes the protocol overhead, by adding the host file to the docker volumes. In your docker-compose.yaml you just need to add a read-only symlink:

volumes:
  - /sys/class/thermal/thermal_zone0/temp:/config/temp:ro

Before you Recreate the docker container, add this to your configuration.yaml:

sensor:       #remove if a sensor is already added in your config; add following to existing sensor config
  - platform: command_line 
    name: Raspberry CPU Temp
    command: cat /config/temp
    unit_of_measurement: "ºC"
    value_template: '{{ value | multiply(0.001) | round(2) }}'

I’ve added the custom mini-graph-card from HACS ( kalkih/mini-graph-card: Minimalistic graph card for Home Assistant Lovelace UI (github.com) with this config for my card on the dashboard:

entities:
  - entity: sensor.raspberry_cpu_temp
name: Raspberry Temperatur (1 Hour)
icon: mdi:thermometer
unit: °C
hour24: true
show:
  labels: true
  legend: false
hours_to_show: 1
points_per_hour: 3600
color_thresholds:
  - value: 43
    color: '#16acfc'
  - value: 46.5
    color: '#fca816'
  - value: 50
    color: '#fc1616'

Example:

Edit: spelling, formating

I have had the same problem but you can discover the path of the vcgencmd
using this command : “which vcgencmd”
in my case the path was : “/usr/bin/vcgencmd measure_temp”

bye

Hello, I am trying to do the steps as you wrote. But it doesn’t. Error message is coming.

permission denied

That was over 2 years ago! I’m no longer using the pi I had that lashed together for. It was running a RazBerry2 Z-Wave hat. I replaced it with a newer Z-Wave USB dongle directly on my HA host…

1 Like