Splitting sensor output using value_template [solved]

Hi.

I’m trying to split the output from a sensor I’ve got.
The restful_api part is working and it outputs “Time until the next NLSS: 0 hours, 5 minutes, 4 seconds”.
I would like to only display the time, vut whenever I try to split the output, it either goes blank or says unknown.

It works when I write do it in the template developer tool.

 sensor:
 - platform: rest
    name: 'NLSS'
    resource: https://whenisnlss.com/when.php
  - platform: template
    sensors:
      nlss:
        friendly_name: 'NLSS'
        value_template: '{{ states.sensor.nlss.state.split(", ")[0]|replace("Time until the next NLSS:", " ") }} {{ states.sensor.nlss.state.split(", ")[1] }}'

Thanks in advance!

You can be super lazy and just format a time on your rest.

 sensor:
 - platform: rest
    name: 'NLSS'
    resource: https://whenisnlss.com/when.php
    value_template: >
      {{ as_timestamp(strptime(value, 'Time until the next NLSS: %H hours, %M minutes, %S seconds')) | timestamp_custom('%H:%M:%S') }}
2 Likes

Works great, thanks!

The countdown is 10 minutes less, than on the website, when I use the template. I can’t find a reason for this, do you know what it could be?

Not sure why. Try this instead:

{{ strptime(value, 'Time until the next NLSS: %H hours, %M minutes, %S seconds').strftime('%H:%M:%S') }}