Filter out subset of results

If I had a value of

OK: uptime: 9w 5d 08:32h, boot: 2020-02-16 16:41:24 (UTC)"

being returned by

“{{ value_json.lines[0].message }}”

In developer > template how would I use it to filter just “9w 5d”? I would even be OK with having the 08:32h in there.

Regular expressions.

"{{ value_json.lines[0].message | regex_findall_index('OK: uptime: (\d*.\s\d*.)') }}"

I’m a bit rubbish at them though. So this probably won’t work if zero weeks is not shown as 0w but is just not shown instead.

What does it display if there are no weeks or days of uptime?

You da man Tom. Thanks a lot. I was getting close filtering it out thru the original json call but couldn’t get rid of the OK at the front so this works without bothering.

Using my poor regex skill though there could still be a problem:

I’ll have to reboot to get that or set this up also on my other computer. Was just rebooted. So I put in my yaml:

value_template: "{{ value_json.lines[0].message | regex_findall_index('OK: uptime: (\d*.\s\d*.)') }}"

Getting:

Error loading /config/configuration.yaml: while parsing a flow mapping
  in "/config/entities/sensor/sensor_rest.yaml", line 144, column 20
expected ',' or '}', but got '['
  in "/config/entities/sensor/sensor_rest.yaml", line 144, column 38

So as it worked in the template screen wondering if it’s the slashes need to be escaped or something.

EDIT: put an extra slash in front of each slash and it passed. See if it works after reboot.

Yeah that worked.

Sometimes the single quotes round the expression can be an issue too. The solution is to use a multi-line template and double quotes. See Template Sensor with regex_findall_index not working

I found there are a couple other scenarios that basically make the sensor not created apparently if the value_template doesn’t match. So apparently if the server hasn’t been up for at least a day it’s flagged as CRITICAL and if it’s a day but not sure yet how far out, it’s labeled as WARNING so:

CRITICAL: uptime: 21:40h
WARNING: uptime: 1d 01:35h
OK: uptime: 9w 5d 08:32h

Any way to handle all these scenarios?

Otherwise I get
image

Until it gets to an OK state.

Not sure if this is possible, but the above will fail if it’s less than a day but also if the reported status is different. I can see three different value_template regex calls I would need to accommodate.

So run this if it’s OK: regex_findall_index(‘OK: uptime: (\d*.\s\d*.)’)
Run this if it’s WARNING: regex_findall_index(‘WARNING: uptime: (\d*.\s\S*)’)
Run this if it’s Critical: CRITICAL: uptime: (\d*\S.*)

Is that even possible?

Note I came up with this regex that seems to do it, but can’t get it into the value_template without it erroring:

(?:.*?:){1}(.*)

Have:

value_template: "{{ value_json.lines[0].message | regex_findall_index((?:.*?:)(.*)) }}"

but returning:

Invalid config for [sensor.rest]: invalid template (TemplateSyntaxError: unexpected char '?' at 54) for dictionary value @ data['value_template']. Got '{{ value_json.lines[0].message | regex_findall_index((?:.*?:)(.*)) }}'. (See ?, line ?).

EDIT EDIT EDIT: This might work:

value_template: "{{ value_json.lines[0].message | regex_findall_index('(?:.*?:){2}(.*h)') }}"

In the original output there is also a comma and then a bunch of garbage after that. Tried to terminate at the comma but kept getting it included so took a shot at the h but that may not be a static value.

EDIT EDIT EDIT EDIT EDIT EDIT: Think I got it: (?:.*?:){2}(.*)(?=,)