Sensor file how to extract some data value_template

Hello, this is my sensor and I want extract only false\true state.
How I need modify the value(value_template: ‘{{ value }}’) to achieve this?

  - platform: command_line
    name: vu4k powerstate
    command: "cat /home/homeassistant/.homeassistant/vu4k_powerstate"
    value_template: '{{ value }}'

file vu4k_powerstate

<?xml version="1.0" encoding="UTF-8"?>
false	</e2instandby>

Thank you guys.

Try this (not sure it will work because of the newline).

  - platform: command_line
    name: vu4k powerstate
    command: "cat /home/homeassistant/.homeassistant/vu4k_powerstate"
    value_template: "{{ value.split('<e2instandby>')[0] }}"

I received this :slight_smile:

<?xml version="1.0" encoding="UTF-8"?> <e2powerstate>

with this code

value_template: "{{ value.split('<e2instandby>')[1] }}"
false </e2instandby> </e2powerstate>

We are close :slight_smile:

It’s dirty but should work:

value_template: "{{ value.split('<e2instandby>')[1] | replace(' </e2instandby> </e2powerstate>','' ) }}"

received this:

false </e2instandby> </e2powerstate>
value_template: "{{ value.split('<e2instandby>')[1].split(' ')[0] }}"

Still the same.

I think that’s a tab not a space. Try copying this:

value_template: "{{ value.split('<e2instandby>')[1].split('	')[0] }}"
1 Like

Thank you!

1 Like

For future reference here’s another way to extract data from a string. It uses regex_findall_index which relies on the use of regular expressions (regex).

The regex pattern to select the data you want is:


I created it using an online regex tester:


It looks for <e2instandby> followed by a newline \n and then captures one or more characters using (\w+) where the parentheses () mean to capture the information and the \w+ means to match one or more characters.

I pasted your data into the Template Editor and successfully tested the template:


Therefore the sensor’s value_template can be this:

value_template: "{{ value | regex_findall_index('<e2instandby>\n(\w+)') }}"

I’'m going to have to learn regular expressions one of these days.

Error loading /home/homeassistant/.homeassistant/configuration.yaml: while scanning a double-quoted scalar
in “/home/homeassistant/.homeassistant/config/sensor.yaml”, line 38, column 21
found unknown escape character ‘w’
in “/home/homeassistant/.homeassistant/config/sensor.yaml”, line 38, column 71

Swap the existing single-quotes for double quotes and the existing double-quotes for single-quotes. The result will be this and it passes config check on my system (version 0.99):

    value_template: '{{ value | regex_findall_index("<e2instandby>\n(\w+)") }}'

For some reason, it gets confused when that regex pattern is delimited by single-quotes.