Template / Cleanup Temperate Sensor

Hi all.

Probably something simple that I just can’t figure out is how to regex/grep/cleanup this output to JUST get the temperature data out.

Have got /usr/bin/sensors from UnRAID providing the following data string to Home Assistant and just need to figure out what the value_template needs to look like to make it work properly.

k10temp-pci-00c3 Adapter: PCI adapter temp1: +47.4°C (high = +70.0°C) (crit = +100.0°C, hyst = +95.0°C) °C

Have this in HA already and that works fine but then I get the above of course.

  - platform: command_line
name: CPU Temperature
command: "/usr/bin/sensors"
# If errors occur, remove degree symbol below
unit_of_measurement: "°C"
#value_template:

I just want to get the current temperature out which is “47.4”.

Thats something to play with in Dev Tools/Templates.

Assign the string to a variable:

{% set string = 'k10temp-pci-00c3 Adapter: PCI adapter temp1: +47.4°C (high = +70.0°C) (crit = +100.0°C, hyst = +95.0°C) °C' %}

Now split it by spaces and get item 6 (python lists start with 0 so we use 5)

{{ string.split(' ')[5] }}

Now we have +47.4°C

Next we split it by the plus sign and get item 2 (1)

{{ string.split(' ')[5].split('+')[1] }}

Now we have 47.4°C

And at least we split it by the degree symbol and get item 1 (0)

{{ string.split(' ')[5].split('+')[1].split('°')[0] }}

Now we have 47.4

So the value_template:

value_template: "{{ value.split(' ')[5].split('+')[1].split('°')[0] }}"

should work.

1 Like

Bloody hell I lost my mind half way through that, but it worked perfectly first time.

The part I’m confused about is whether that variable is permanently required? Or is that purely a sandbox area for testing this kind of thing and you can remove it after you messed around as I did with the example that was there?

Doesn’t seem like you can save multiple templates or do anything permanently which is why I assume it’s just a sandbox.


Actually spoke too soon seems HA doesn’t like it based on the logs.

2018-11-26 21:01:37 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: list object has no element 1 (value: k10temp-pci-00c3

Adapter: PCI adapter
temp1: +49.6°C (high = +70.0°C)
(crit = +100.0°C, hyst = +95.0°C), template: {{ value.split(’ ‘)[5].split(’+')[1].split(‘°’)[0] }})

Am I maybe supposed to the the variable set string inside the yaml file as well?

Surely with the string changing all the time setting it statically wouldn’t do the job very well?

The first part is only to show how to play with these values in Dev Tools/Templates.
Auswahl_155

All you need is the value_template in the command_line sensor.

- platform: command_line
  name: CPU Temperature
  command: "/usr/bin/sensors"
  # If errors occur, remove degree symbol below
  unit_of_measurement: "°C"
  value_template: "{{ value.split(' ')[5].split('+')[1].split('°')[0] }}"

Strange I get that error output in HA.log when using it “as is” as per your post.

And a null value in the GUI of course.

Let me copy and paste above as is, just in case I made some mistake in syntax.

How would you split by line break? I have a suspicion that’s the actual problem here looking at the formatting on the log file.


Figured it out, just needed to use () instead of [].

Soldiering on!

Had some joy with the following in that it at least doesn’t give an error in HA logs…after changing the output from the sensors command.

- platform: command_line
59      name: "Microserver CPU Temperature"
60      command: "/usr/bin/sensors -u"
61      # If errors occur, remove degree symbol below
62      unit_of_measurement: "°C"
63      value_template: "{{ value.splitlines()[4].split(' ')[1] }}"

However it remains blank in the GUI interface.

You have however taught me an abundance of things in one small conversation @VDRainer so I think I’ll fight with it tomorrow.

It’s hard to not possible to work with multiple lines in the value_template.

You can strip lines not wanted if you use grep in the command.

Example:

server:$ /usr/bin/sensors
coretemp-isa-0000
Adapter: ISA adapter
Core 0:       +38.0°C  (high = +84.0°C, crit = +100.0°C)
Core 1:       +43.0°C  (high = +84.0°C, crit = +100.0°C)
Core 2:       +40.0°C  (high = +84.0°C, crit = +100.0°C)
Core 3:       +39.0°C  (high = +84.0°C, crit = +100.0°C)

If you want only one line (Core 0), you can filter it with grep.

server:~$ /usr/bin/sensors | grep 'Core 0'
Core 0:       +43.0°C  (high = +84.0°C, crit = +100.0°C)

So maybe edit your command to:

command: "/usr/bin/sensors | grep 'Core 0'"

or whatever line you want to read.

Yeah I have a strong suspicion that what I think HA is receiving isn’t actually what I’m running the substring or regex or whatever you want to call it against which is why I get blank output.

How would I see exactly what the sensor value is coming in as if HA would interpret it?

Going to try a grep now without any value_template as a start to try to clean it up.

You can see the states of the sensors in Dev Tools/States
Auswahl_156

And here one more example to extract the value in one line from the output above.

/usr/bin/sensors | grep 'Core 0' | cut -d'+' -f2 | cut -d'.' -f1

returns only ‘38’

I’ve realised my version of the /usr/bin/sensors for some reason adds a space at the beginning of the line but even accounting for that making the Template work perfectly with the string I still get a blank when adding the template.

So getting the value pre-formatted from bash seems like the most fool proof option.

As they say so many ways to skin a cat.

This is what finally worked and also matches the single decimal I have fo all other temperatures.

Thanks for all your help and leading me along the path @VDRainer

platform: command_line
    name: "Microserver CPU Temperature"
    scan_interval: 300
    command: "/usr/bin/sensors -u | grep 'temp1_input' | cut -c 16-19"
    unit_of_measurement: "°C"