Template replace - how to cut some words from the sensor?

Hi,
I’m trying to set up a sensor for my waste collection.
I’ve managed to scrape my data, with this code:

  - platform: scrape
    resource: !secret wasteneturl
    name: Recycling
    select: "div.wasteSearchResults.recycling > div:nth-child(4)"

my sensor.recycling gets the follownig state:
Next Collection Day Friday, 16 August 2019
I’d like to strip all the text left of the comma, so that the final result will be just 16 August 2019

Using the Template editor I was able to get rid of Next Collection Day with this template
{{ states.sensor.recycling.state|replace('Next Collection Day','')}}, but I’d like to get rid of the comma and the weekday as well.
Could anyone help me out with this?
Thanks

You can try to split it.
Play with it in the Template Dev tool.

{% set text = 'Next Collection Day Friday, 16 August 2019' %}
{{ text.split(', ')[1] }}
1 Like

Awesome. Thanks.
I ended up with this, and it works.

{% set text = states.sensor.recycling.state %}
{{ text.split(', ')[1] }}
1 Like

It doesn’t hurt, but you don’t need the {% set text = ... %}, it’s only for playing with values in the Template tool.

{{ states.sensor.recycling.state.split(', ')[1] }}

should do it.

2 Likes

Thanks for the update.

Now I have another problem.
As soon as there’s only one day until the thrash is being collected, the date changes to “Tomorrow” the day before collection, respectively “Today”.

{% set text = states('sensor.recycling') %}
{{ text.split(', ')[-1] if ',' in text else text }}

I just solved the problem with the help of @skalavala and @Vasiley (I hope I’m tagging the right people) - thanks guys again.
There was a small issue because my scraper scraped some invisible spaces and newline characters as well.
The unshaped result is this

{% if 'Tomorrow' in states('sensor.rubbish') %}
     Rubbish Tomorrow
  {% else %}
     Rubbish Not Tomorrow
  {% endif %}

Now I just have to tune it a little bit.

{% set text = states('sensor.recycling') %}
{{ text.split(', ')[-1].strip() if ',' in text else text.strip() }}

should return date, tomorrow, or today

Kind of works, but because of the extra space and new line it returns this:


Right now, I had to adapt the sensor name, because the rubbish is due tomorrow, not the recycling.

Then the result isn’t just

That’s a large whitespace in the center of the string. And assuming it’s in Tomorrow and Today but not the date…

{% set text = states('sensor.recycling') %}
{{ text.split(', ')[-1].strip() if ',' in text else text.split('\n').strip()[-1] }}

This last code snippet returns 16 August 2019, because recycling is due on the 16th.
With sensor.rubbish returns an error:

Error rendering template: UndefinedError: 'list object' has no attribute 'strip'

sensor.rubbish state looks like this:

oops, typo

{% set text = states('sensor.recycling') %}
{{ text.split(', ')[-1].strip() if ',' in text else text.split('\n')[-1].strip() }}
1 Like

Return nothing.


The state today is Next Collection Day Today
image

sorry to resurrect and old thread
in the template editor I have:

{% set text = states.sensor.blackbin.state %}
{{ text.split(', ')[1] }}

which gives me
image
But if I put this template in my configuration.yaml its just empty?

    value_template: >-
      {% set text = states.sensor.blackbin.state %}
      {{ text.split(', ')[1] }}

Check for errors.

I have it now:

    value_template: >-
      {{ value.split(', ')[-1].strip() if ',' in value else value.strip() }}