REST sensor and split data

Hi! Guys could you help me please with these values.

I have an this REST sensor:

- platform: rest
  resource: !secret url
  name: container_uptime
  force_update: true
  value_template: '{{ value_json.status }}'

I see this value: Up 8 days - ok it’s good.

When container is down, I see the next value: Exited (137) 3 weeks ago

Okay, I wanted to remove the characters I didn’t need, and in the end I redesigned the sensor like this:

- platform: rest
  resource: !secret url
  name: container_uptime
  force_update: true
  value_template: "{{ value_json.status.split(')')[1] }}"

…and this looks like this: 7 weeks ago

But, if container is online, and sensor back the online value, it’s empty, 'coz I edited my template to this {{ value_json.status.split(')')[1] }} - how I can merge these two values from this sensor in one rest sensor? Can someone tell me?

if online - Up XX days or minutes
if down - XX minutes or days

Try this:

value_template: "{{ value_json.status if 'Up' in value_json.status else value_json.status.split(')')[1] }}"
1 Like

Thank u very much homie! Works flawlessly and as I needed it. :+1:

1 Like