Displaying Cpu Temperature Within Home Assistant

Ok, that sounds like a formatting error in the config file:

A similar example looks like this:

  - platform: command_line
    name: Server Temp
    command: /usr/bin/sensors | grep "Core 0" | cut -c16-19
    unit_of_measurement: "°c"

I have that in a serperate ‘sensors.yaml’ file that is referred to the main ‘configuration.yaml’ file as:

sensor: !include sensors.yaml

And don’t forget the best way to show people your code in the comments is to highlight the section and use the “< / >” tool on it.

People will be able to help you faster that way :slight_smile:

1 Like

Can you post with the same formatting as your config file?

Paste your text and then hit the < / > above your post.

Or post a screen shot if it’s easier.

Finally, is this all under ‘sensor’ as the component?

apologies, im still learning.

the below is literally all i have written so am i missing something ?

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

sorry but this is a little confusing so am i right in thinking i need to create a new file saving it as sensors.yaml and then put sensor: !include sensors.yaml in my configuration.yaml because when trying that i got many errors and home assistant would no longer start

Yes, you are correct. Put in you main configuration file the following line:

sensor: !include sensors.yaml

Then in sensors.yaml write the following code:

# 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) }}'

It works for me. I added round(1) at the end because I don’t need that many deciamal numbers, you can delete it if you want.

1 Like

ok but is there a specific area within my configuration.yaml that i should put that ?

No worries. Ok, we’ll ignore the separate file for now, so undo or comment-out those changes.

And we’ll get this right in you main ‘configuration.yaml’ file.

It should look like this I reckon:

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) }}'

I’d say you nearly have it. Try with & without those single quotes you have around the command.

The spaces in front of lines are specifically important and will always be multiples of two.

So in the above example there is no space before the word ‘sensor’, and then two spaces in front of all the lines below.

thanks, really appreciate the help but now i receive the error
17-01-01 20:21:41 homeassistant.loader: Unable to find component -sensor

home assistant will not work unless i put the - before sensor but then when it does run i get the above error ?

any ideas ?

Oops that one is on me!

try:

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) }}'

See the example here: https://home-assistant.io/components/sensor.command_line/

Also, are you re-running Home Assistant each time you want to check?

If you are there’s a quicker way to check if the config files will work, just type in a terminal:

hass --script check_config

yes i’m restarting the service each time and now i still have an error

duplicate key: “sensor”

That error is not so bad, it’s telling you that you can only use the header ‘sensor’ once in your configuration.yaml file.

So have a look through and find the others lines you’ve used ‘sensor’ with, and then group them together under the one ‘sensor’ line.

All the lines after will follow the same indented style, so when you’ve them together it should look like this:

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) }}'
    
  - platform: command_line
    name: Sensor Number 2
    command: 'cat /sys/class/thermal/thermal_zone0/temp'
    unit_of_measurement: '°C'
    value_template: '{{ value | multiply(0.001) }}'
3 Likes

you my friend are a star !!

it was conflicting with another sensor for the sun which i have deleted because i don’t want or need it anyway.

is there anyway to complicate my temperature script to make it refresh every 5 minutes ? or does this automatically update on a set interval ?

thanks again

Glad to help, a very helpful soul here gave me pretty much the same assistance when I started with hass in Nov.

Now I’ve dashboards, and automations all over :slight_smile:

The temp should update on its own every 2 minutes or so, iirc.

I really recommend breaking out your configuration.yaml file into smaller files.

It makes it easier to check for bugs, better readability, and you just experienced the ‘duplicate key error’ etc.

If you’re just starting, you’ll be amazed at the things you’ll have done very quickly - and that also means longer config files.

Lots of examples here that I used to get it done:

The formatting within the separate files is slightly different, so keep that in mind.

Enjoy, Hass is really an extraordinary thing!

I knew not a single bit of yaml or python 2 months ago. Now I can write APIs, didn’t think I had a bit of that in me!

isnt this function(get CPU temp) already covered by System Monitor Component?

typical isnt it lol i didnt see that and now i’ve looked then yes pretty much that covers what i was trying to do !

Thanks again for all your help. theres so much to learn and it feels like im going to need much more than just a few months to pick up this lot !

Ah, I totally forgot about them - the cpu sensor didn’t suit my chip type, I think that’s why I forgot.

Sorry for going around the houses with it jammin36 !

@Bit-River
I followed your instructions, and added the sensor as below, and changed ‘°C’ to ‘°F’, but the temp is still displaying in Celsius.

Any ideas?

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

This works for me to covert to Fahrenheit:

value_template: '{{ (value | multiply(0.001)) | multiply(1.8) + 32 | round(1) }}'

Thank you.

I tried that, and got the following.

“CPU Temperature
37 seconds ago
136.39280000000002 º”

# CPU temperature
- 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) + 32 | round(1) }}'

This should fix the rounding error:

value_template: '{{ (value | multiply(0.001) * 1.8 + 32) | round(1) }}'