Multiple 'splits' in value_template in sensor

I’m trying to work out how to chain a ‘split’ in value template:

- platform: scrape
    resource: !secret council_url
    name: General Waste Collection
    select: "div.atPanelContent.atFirst.atAlt0"

I can get this to work, and it outputs:
General Waste Next Collection: Thu, Apr 5, 2018 View Calendar

Obviously this isn’t a perfect output, so I’ve been playing with the templates section to try and split it out. I used split like this:

{{ states.sensor.general_waste_collection.state.split()[4] }}
{{ states.sensor.general_waste_collection.state.split()[5] }}
{{ states.sensor.general_waste_collection.state.split()[6] }}
{{ states.sensor.general_waste_collection.state.split()[7] }}

In the templates section of Hass, this works, and shows me:

Thu,
Apr
5,
2018.

However I try I can’t seem to chain these together in value_template to string it together as a single line. I normally just “Unknown” as an output.

If anyone can shed any light on this I’d appreciate it!

You can specify a delimiter, other than the default space, to use in the split.
In your case, split(’:’)[2] will give you:
Thu, Apr 5, 2018 View Calendar

Also consider using replace(‘old’,‘new’), then split(’:’)[2] | replace(‘View Calendar’,’ ') will give you:
Thu, Apr 5, 2018

Thanks for the reply - that’s really helpful!
I shall try playing with that! In terms of syntax, would that be something like:

value_template: '{{ states.sensor.general_waste_collection.state.split(’:’)[2] | replace(‘View Calendar’,’ ’) }}'

Yes, you got it.

Thanks!
I’m getting some errors, so I must have some syntax error somewhere:

Testing configuration at /config
Failed config
  sensor.scrape: 
    - Invalid config for [sensor.scrape]: invalid template (TemplateSyntaxError: unexpected char '’' at 54) for dictionary value @ data['value_template']. Got '{{ states.sensor.general_waste_collection.state.split(’:’)[2] | replace(‘View Calendar’,’ ’) }}'.

Looks like one of the quote is not right, make sure you use the single quote from your keyboard, not copied from here.

Also needs to be [1] and not [2]:

value_template: "{{ states.sensor.general_waste_collection.state.split(':')[1] | replace('View Calendar',' ') }}"

Thanks for your help :slightly_smiling_face:

When I use:
value_template: "{{ states.sensor.general_waste_collection.state.split(':')[1] | replace('View Calendar',' ') }}"

The output is just blank. I figured this would be the double quotes, so when I change this to single quotes:

value_template: '{{ states.sensor.general_waste_collection.state.split(':')[1] | replace('View Calendar',' ') }}'

I verify my config and get:

Testing configuration at /config
ERROR:homeassistant.util.yaml:while parsing a block mapping
  in "/config/configuration.yaml", line 67, column 5
expected <block end>, but found '<scalar>'
  in "/config/configuration.yaml", line 71, column 77
Failed config
  General Errors: 
    - while parsing a block mapping
  in "/config/configuration.yaml", line 67, column 5
expected <block end>, but found '<scalar>'
  in "/config/configuration.yaml", line 71, column 77

Successful config (partial)

Can you show us the config of this sensor?

@JTPublic Thanks so much for your input on this!
@dale3h came to the rescue on this in Discord:

value_template: "{{ value.split(':')[1]|replace('View Calendar', ' ')|trim }}"

Glad that works out.