How to use value_template to extract data from the sensors command

I need to extract 2 values from the below command using value_template which I underline below. what is the correct command of value_template to extract just that 2 values of 38.0(CPU temp) & 36.9(NVME SSD temp) respectively as there is too many text there? I need display this value on HA.

I try to seach through the forum but non of the topic match my question.

Thanks in advances.

root@pve:~# sensors -A
iwlwifi_1-virtual-0
temp1: N/A

acpitz-acpi-0
temp1: +39.0°C (crit = +119.0°C)

coretemp-isa-0000
Package id 0: +38.0°C (high = +100.0°C, crit = +100.0°C)
Core 0: +36.0°C (high = +100.0°C, crit = +100.0°C)
Core 1: +36.0°C (high = +100.0°C, crit = +100.0°C)
Core 2: +35.0°C (high = +100.0°C, crit = +100.0°C)
Core 3: +37.0°C (high = +100.0°C, crit = +100.0°C)

nvme-pci-0100
Composite: +36.9°C (low = -273.1°C, high = +81.8°C)
(crit = +84.8°C)
Sensor 1: +36.9°C (low = -273.1°C, high = +65261.8°C)
Sensor 2: +37.9°C (low = -273.1°C, high = +65261.8°C)

Assuming your system is set up such that you can call sensors from the HA core:

(edit: updated in line with information that became available later in the topic)

sensor:
  - platform: command_line
    name: "Coretemp ISA temperature"
    command: sensors -A
    value_template: '{{ (value|regex_findall("Package id 0:\s*\+?(\-?[\d\.]*)°"))[0] }}'
    unit_of_measurement: "°C"
    device_class: temperature

  - platform: command_line
    name: "NVMe PCI temperature"
    command: sensors -A
    value_template: '{{ (value|regex_findall("Composite:\s*\+?(\-?[\d\.]*)°"))[0] }}'
    unit_of_measurement: "°C"
    device_class: temperature

Both templates are looking for the identifier (e.g. “Composite:”), an optional plus sign that is ignored, then returning an optional minus sign and any digits and decimal points before the degrees sign. regex_findall (docs) always returns a list, and we want the first and only value, hence the [0] on the end.

Alternatively, you could use a combination of command-line options to sensors and use of grep and cut to get just the value on the command line, and not need a value_template.

EDIT: having just tested this, it’s important that the regex_findall query string is surrounded by double quotes (or you’ll get an error about escape codes), so the template is surrounded by outer single quotes.

Hi, thanks for fast reply, seem like come out an error when using file editor from HA

bad indentation of a mapping entry (249:47)
249 | … te: "{{ (value|regex_findall(“Composite: +?(-?[\d.]*)°”))[ …
------------------------------------------^

i try to add space in between also same error
value | regex_findall

A bad indentation error means you’ve got the wrong number of spaces at the start of one of your lines, and is nothing to do with the content of that line. Everything should be lined up exactly as per my example — but make sure you only have one top-level sensor: statement in your config.

Paste in your config as it stands — format it correctly using three backticks (```) either side or by using the </> button.

is my fault :sweat_smile:, i accidently add " at the back, now no error but entity value come out 0, the other sensor also come out 0 as well.

here is the actual command

  • platform: command_line
    name: CPU Temp on Proxmox
    command: “ssh -i /config/id_rsa -o StrictHostKeyChecking=no [email protected] -t ‘sensors -A’”
    unit_of_measurement: “°C”
    unique_id: 9587aa30-ec05-4507-b040-3d7859d07443
    value_template: ‘{{ (value|regex_findall(“Package id 0: +?(-?[\d.]*)°”))[0] }}’

I can’t debug your code because you haven’t formatted it as requested. You have pasted it in like text, so the quotes are turned to invalid “smart” quotes, and you appear to have lost a load of backslashes from my template.

However, as a first step, it looks from that screenshot that there is more than one space before the number you want, unlike in your initial post (again, if you’d formatted that as code, that would have been visible to me). Try these templates:

'{{ (value|regex_findall("Package id 0:\s*\+?(\-?[\d\.]*)°"))[0] }}'
'{{ (value|regex_findall("Composite:\s*\+?(\-?[\d\.]*)°"))[0] }}'

Here’s a breakdown of what that regex pattern is looking for:

Composite:\s*    Find "Composite:" followed by any amount of whitespace
\+?              There might be a plus sign which we do not need
(                Start the "match" section: the bit we want
\-?              There might be a minus sign which we do need
[\d\.]*          Keep going over any combination of digits and decimal points
)                End of the "match" section
°                and there should be a degrees sign to finish

It’ll return a list of matches, which should contain just one e.g. ["36.9"].

2 Likes

it is working as intended now, Thanks again for your help for solved this matter that seem to be kind of difficult to me as I not so understand how the value template filter the text.

image

1 Like

now I know how to put actual code here. Below is the actual text

root@pve:~# sensors -A
iwlwifi_1-virtual-0
temp1:            N/A  

acpitz-acpi-0
temp1:        +40.0°C  (crit = +119.0°C)

coretemp-isa-0000
Package id 0:  +38.0°C  (high = +100.0°C, crit = +100.0°C)
Core 0:        +36.0°C  (high = +100.0°C, crit = +100.0°C)
Core 1:        +38.0°C  (high = +100.0°C, crit = +100.0°C)
Core 2:        +35.0°C  (high = +100.0°C, crit = +100.0°C)
Core 3:        +37.0°C  (high = +100.0°C, crit = +100.0°C)

nvme-pci-0100
Composite:    +37.9°C  (low  = -273.1°C, high = +81.8°C)
                       (crit = +84.8°C)
Sensor 1:     +37.9°C  (low  = -273.1°C, high = +65261.8°C)
Sensor 2:     +37.9°C  (low  = -273.1°C, high = +65261.8°C)
1 Like

:+1: Perfect. Quite a range of values on those last two sensors! (16-bit Kelvin value converted to Celsius).

I believe they allocate the temperature range wrongly for the sensor 1 & 2.

I was trying the new configuration for command line from this update:

but cannot make it work:

command_line:
  sensor:
    name: "NVMe PCI temperature"
    command: sensors -A
    value_template: '{{ (value|regex_findall("Composite:\s*\+?(\-?[\d\.]*)°"))[0] }}'
    unit_of_measurement: "°C"

the NVME drive is connected to Proxmox or HAOS? you need test the command “sensors” on terminal for verify whether it is working.

The command works fine:

but I get this error:
image

can you try the format below, new command format change abit, add “-” before the sensor & inverted command for the “sensor -A”

command_line:
  - sensor:
      name: NVMe PCI temperature
      command: "sensors -A"
      value_template: '{{ (value|regex_findall("Composite:\s*\+?(\-?[\d\.]*)°"))[0] }}'
      unit_of_measurement: "°C"

@Troon , how are you? need to trouble you again. I have change my proxmos CPU and now the “sensors -A” command generate different result(refer below) of NVME drive temperature, how do I use value template to filter it to get the nvme-pci-0100, nvme-pci-0200 & nvme-pci-0300 respective composite temperature which without the +sign?

root@pve:~# sensors -A
coretemp-isa-0000
Package id 0:  +51.0°C  (high = +105.0°C, crit = +105.0°C)
Core 0:        +48.0°C  (high = +105.0°C, crit = +105.0°C)
Core 1:        +48.0°C  (high = +105.0°C, crit = +105.0°C)
Core 2:        +48.0°C  (high = +105.0°C, crit = +105.0°C)
Core 3:        +48.0°C  (high = +105.0°C, crit = +105.0°C)
Core 4:        +51.0°C  (high = +105.0°C, crit = +105.0°C)
Core 5:        +51.0°C  (high = +105.0°C, crit = +105.0°C)
Core 6:        +51.0°C  (high = +105.0°C, crit = +105.0°C)
Core 7:        +51.0°C  (high = +105.0°C, crit = +105.0°C)

nvme-pci-0300
Composite:    +29.9°C  (low  = -20.1°C, high = +83.8°C)
                       (crit = +88.8°C)
Sensor 2:     +55.9°C  

acpitz-acpi-0
temp1:        +27.8°C  

nvme-pci-0200
Composite:    +29.9°C  (low  = -20.1°C, high = +83.8°C)
                       (crit = +88.8°C)
Sensor 2:     +55.9°C  

nvme-pci-0100
Composite:    +40.9°C  (low  = -20.1°C, high = +83.8°C)
                       (crit = +88.8°C)
Sensor 2:     +53.9°C  

If you know they’re always in that order, my original solution will still work:

{{ (value|regex_findall("Composite:\s*\+?(\-?[\d\.]*)°"))[0] }}

for nvme-pci-0300; replace the [0] with [1] for acpitz-acpi-0 and use [2] for nvme-pci-0200.

If you can’t guarantee the order, you can pull them out specifically with e.g.:

{{ (value|regex_findall("nvme-pci-0100\nComposite:\s*\+?(\-?[\d\.]*)°"))[0] }}
1 Like

Thanks again for the reply, will try it & let u know the result.

There is something wrong when I use the value template below

  - sensor:
      name: NVME 2280 SSD Temp
      command: "ssh -q -i /config/id_ed25519 -o StrictHostKeyChecking=no [email protected] -tt 'sensors -A'"
      unit_of_measurement: "°C"
      unique_id: eb7eb84c-b558-6e2d-bcdf-da374e4bf463
      value_template: '{{ (value|regex_findall("nvme-pci-0100\nComposite:\s*\+?(\-?[\d\.]*)°"))[0] }}'

it will show below
“This entity is no longer being provided by the command_line integration. If the entity is no longer in use, delete it in settings.”

If I change the value template to below
value_template: ‘{{ (value|regex_findall(“Composite:\s*+?(-?[\d.]*)°”))[0] }}’

the sensor will available again once restart HA.

That template works in the template editor against the copy-paste output from sensors you provided.

Perhaps the real output isn’t using straightforward \n characters, or there is some terminal control codes buried in there.

Try:

{% set cut = value.split('nvme-pci-0100')[1] %}
{{ (cut|regex_findall("Composite:\s*\+?(\-?[\d\.]*)°"))[0] }}