How to show the Raspberry Pi CPU temperature?

Dear community, I’ve started to get (cue the late Steve Erwin’s voice) ‘weally weally daingerwous’ with HA :slight_smile:

Yet I’m running into authentication problems getting remote ssh command based sensor results, following the directions for remote ssh, keygen and key upload above. Hoping somebody might be able to shed some light on what I’m missing

Setup: HA running in a virtual machine with MQTT broker, Zig and Zwave are radios running on a couple of Pi’s. All is working well, yet I’d like to monitor things like CPU temperature, pulling it over a remote ssh commandline.

However I’m unfortunately not seeing much success as I get no sensor updates and the HA server log has a ton of repeated entries like this:

2021-01-17 04:48:50 ERROR (SyncWorker_0) [homeassistant.components.command_line] Command failed: ssh [email protected] ‘cat /sys/class/thermal/thermal_zone0/temp’
2021-01-17 04:49:49 ERROR (SyncWorker_2) [homeassistant.components.command_line] Command failed: ssh [email protected] ‘cat /sys/class/thermal/thermal_zone0/temp’
2021-01-17 04:49:50 ERROR (SyncWorker_0) [homeassistant.components.command_line] Command failed: ssh [email protected] ‘cat /sys/class/thermal/thermal_zone0/temp’

Retracing my steps, I

  1. SSH’ed into the console of HA.
  2. Generated the keys with ssh-keygen and
  3. copied them up with ssh-copy-id.
  4. Running the command “by hand” yields the expected result:

~ $ ssh [email protected] ‘cat /sys/class/thermal/thermal_zone0/temp’
43312

However the wheels come off the cart when I implement the sensor in my configuration.yaml

sensor:

  • platform: command_line #<—forum is making the dash into a dot :-/
    name: ZigPi CPU
    command: “ssh [email protected] ‘cat /sys/class/thermal/thermal_zone0/temp’”
    unit_of_measurement: “°C”
    value_template: ‘{{ value | multiply(0.001) }}’

As soon as I validate the configuration and restart the HA server, the above logfile entries keep piling up and the sensor on the Overview appears but is empty as a result. If anyone has suggestions as how to troubleshoot this ,I’m all appreciative ears!

Thanks in advance.

Try adding the full path for the cat command, /bin/cat.

Thanks Raymond, good suggestion. I gave it a try, however it unfortunately doesn’t seem to make much difference:

2021-01-17 18:31:00 ERROR (SyncWorker_3) [homeassistant.components.command_line] Command failed: ssh [email protected] ‘/bin/cat /sys/class/thermal/thermal_zone0/temp’

The commandline still works being run manually, yet I wish there was some way of finding out why the command is failing when run by HA?

I had forgotten my earlier difficulty with this. When I setup up the ssh keys, I did so logged on as homeassistant. I could run my script manually, but not within an automation.
When homeassistant runs the script it does so as root, which makes the script fail because it looks for the credentials in /root/.ssh.

This is part of a script that is called via platform: command_line.

sudo -u homeassistant ssh volumio@kitchen “~/bin/remote-play $1”

'sudo -u homeassistant ’ was the solution.

That makes good sense, yet how would one go about doing that on HomeAssistant OS? I’m already SSH’ed in as root and besides there’s no sudo available on HassOS.

Thinking out loud, perhaps the keyfile is in the wrong place?
Currently it’s being generated by ssh-keygen as /root/.ssh/id_rsa - could this be it or is HA executing the call with another user than root perhaps?

My HA is running in a python virtual environment not HA OS.

I just found a thread that may apply to your situatuion:

[Solved] Run ssh command in Hass.io

Thank you sir! - I’ve been playing around (wrestling) with this for a couple of days now, and finally saw some success. I tried the command line options as referenced in the link you provided, yet I found out (mostly by google, trial and error) that the -o UserKnownHostFile=/dev/null does not seem to work). Second, I did not have the /config/.ssh/ folder, so I had to create it and copy in the ID file. Something does not like referencing the file in /root/.ssh… Anyway this is what I ended up with for my sensor. Thanks again!

- platform: command_line 
  name: ZigPi CPU
  command: ssh -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] 'cat /sys/class/thermal/thermal_zone0/temp'
  unit_of_measurement: "ºC"
  value_template: '{{ value | multiply(0.001) | round(2) }}'

At the time of writing I’m on Home Assistant OS 5.10 running in Docker

Hey @MaxVonEvil

I followed your last message and did what you did I.e.
In the bash I created a folder I.e. config/ssh/key but then when I try to copy the generated file to the folder it somehow doesn’t work.

Did you have the same issue?

Hi @bachoo - you’re creating the config/.ssh folders in the root path right? Either way, I’m running the above against 2 separate pi’s respectively running Raspian Stretch and Buster, both seems to work nicely. I built out the same mechanism to report RSSI (wifi signal strength) on the pi’s using this entry:

  - platform: command_line
    name: ZigPi RSSI
    command: ssh -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] '/home/pi/rssi.sh'
    unit_of_measurement: "%"

The referenced shellscript rssi.sh just contains the line below, but I kept it as a script so I could tinker some more (awk is afaik preinstalled, but you may want to check)

awk 'NR==3 {printf "%.0f\n",$3*10/7}' /proc/net/wireless

Hope this helps

1 Like

Also, you can enable glances addon and then do glances integration to get the sensors into ha.

The most easy thing if you have the glances add-on available!

image

2 Likes

complete noob here

i added this to my configuration.yaml

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

Now how do i add a card or show it graphically on the lovelace interface?

PS: Running hassio 7.0 on RPi 4

Hi

i’m new to this, can you please tell me how you got the temp to show on lovelace UI?

I do this with multiple machines, using the same command via ssh. I show them in a Horizontal Stack card with guage card children. Here’s my lovelace display configuration for that (my temps are fahrenheit):

type: horizontal-stack
title: CPU Temperature
cards:
  - type: gauge
    entity: sensor.pi_fire_cpu_temperature
    max: 180
    severity:
      green: 78
      yellow: 120
      red: 140
    min: 78
    unit: ' '
    name: pi-fire
  - type: gauge
    entity: sensor.pi_proxy_cpu_temperature
    max: 180
    min: 78
    severity:
      green: 78
      yellow: 120
      red: 140
    name: pi-proxy
    unit: ' '
  - type: gauge
    entity: sensor.pi_ha_cpu_temperature
    max: 180
    name: pi-ha
    unit: ' '
    min: 78
    severity:
      green: 78
      yellow: 120
      red: 140
  - type: gauge
    entity: sensor.pi_nas_cpu_temperature
    max: 180
    name: pi-nas
    unit: ' '
    min: 78
    severity:
      green: 78
      yellow: 120
      red: 140

thank oyu for the reply…

but this is what i see

i dont have that "entity:. how do i create it?

… how do I create it …

You don’t want to, that’s for one of my machines. You want to find and use the id of the sensor you created for your machine. Based on your first post my best guess it that it’s:

sensor.cpu_temperature

You should probably use Configuration - Devices & Services - Entities to find it though. And if you only have the one sensor you probably don’t want to put it in a horizontal stock card either, just add a guage card for it directly.

thanks

it worked, wis i could display it in Centigrade

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