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
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.
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.
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] }}'
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"].
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.