Formatting question

I am scraping date from a website (using multiscrape) and I am trying to convert the text from:

“Data delayed at least 15 minutes, as of Dec 01 2022.” to 01/12/2022 (UK format)

In my entity, having captured the data as “sensor.fund1”, “fund_date”, I have started by using

value_template: “{{ value.replace(‘Data delayed at least 15 minutes, as of ‘, ‘’)|replace(’.’,’’) }}”

which leaves me with “Dec 01 2022” but I am struggling to convert it to 01/12/2022.

In Developer Tools, if I can use

{{ strptime(state_attr(“sensor.fund1”, “fund_date”), “%b %d %Y”) .strftime(’%d/%m/%y’) }}

it converts it to the format I want, but I cannot work out how to combine the two items

value_template: “{{ value.replace(‘Data delayed at least 15 minutes, as of ‘, ‘’)|replace(’.’,’’) }}”
and
{{ strptime(state_attr(“sensor.fund1”, “fund_date”), “%b %d %Y”) .strftime(’%d/%m/%y’) }}

into the value_template line

Any suggestions?

value_template: >
  {{ strptime(state_attr("sensor.fund1", "fund_date")[40:51], "%b %d %Y", default="")|as_timestamp|timestamp_custom("%d/%m/%Y") }}

This might be off by an hour depending on timezones. I’m not messing around with replace, but just pulling the character positions ([40:51]) to get Dec 01 2022, then strptime-ing it to a UNIX timestamp and timestamp_custom-ing it to the format you want.

Please format your code snippets correctly using the </> button.

Thank you.

I will try it out later

The problem is that I have a previous line in my code which is

value_template: "{{ value.replace('Data delayed at least 15 minutes, as of ', '')|replace('.','') }}"

and I cannot use a second value_template, so my question really is, how do I merge the two value templates into one

The full code I am using is:

- resource: https://markets.ft.com/data/funds/tearsheet/summary?s=GB0006061963:GBX
  scan_interval: 28800
  sensor:
    - unique_id: Fund1
      name: Fund 1
      select: "body > div.o-grid-container.mod-container > div:nth-child(2) > section:nth-child(1) > div > div > div.mod-tearsheet-overview__overview.clearfix > div.mod-tearsheet-overview__quote > ul > li:nth-child(1) > span.mod-ui-data-list__value"     
      attributes:
        - name: Fund Price
          select: "body > div.o-grid-container.mod-container > div:nth-child(2) > section:nth-child(1) > div > div > div.mod-tearsheet-overview__overview.clearfix > div.mod-tearsheet-overview__quote > ul > li:nth-child(1) > span.mod-ui-data-list__value"   

        - name: Fund Date
          select: "body > div.o-grid-container.mod-container > div:nth-child(2) > section:nth-child(1) > div > div > div.mod-tearsheet-overview__overview.clearfix > div.mod-tearsheet-overview__quote > div"
          value_template: "{{ value.replace('Data delayed at least 15 minutes, as of ', '')|replace('.','') }}"
          

Thanks

I don’t have multiscrape installed, so I can’t try it out. I’m not suggesting a second value_template. My code replaces your template. Here it is with value instead of the sensor:

- resource: https://markets.ft.com/data/funds/tearsheet/summary?s=GB0006061963:GBX
  scan_interval: 28800
  sensor:
    - unique_id: Fund1
      name: Fund 1
      select: "body > div.o-grid-container.mod-container > div:nth-child(2) > section:nth-child(1) > div > div > div.mod-tearsheet-overview__overview.clearfix > div.mod-tearsheet-overview__quote > ul > li:nth-child(1) > span.mod-ui-data-list__value"     
      attributes:
        - name: Fund Price
          select: "body > div.o-grid-container.mod-container > div:nth-child(2) > section:nth-child(1) > div > div > div.mod-tearsheet-overview__overview.clearfix > div.mod-tearsheet-overview__quote > ul > li:nth-child(1) > span.mod-ui-data-list__value"   

        - name: Fund Date
          select: "body > div.o-grid-container.mod-container > div:nth-child(2) > section:nth-child(1) > div > div > div.mod-tearsheet-overview__overview.clearfix > div.mod-tearsheet-overview__quote > div"
          value_template: >
            {{ strptime(value[40:51], "%b %d %Y", default="")|as_timestamp|timestamp_custom("%d/%m/%Y") }}

Hi Troon,

Still not quite sure how you did that (I will need to better understand the “value” aspect), but you have fixed it for me, so thank you very much indeed.

It’s quite simple: value is what the scrape sensor pulls out of the page, and value_template allows you to do something with it before it falls out of your sensor. Using your method rather than mine:

value_template: "{{ strptime(value|replace('Data delayed at least 15 minutes, as of ', '')|replace('.',''), '%b %d %Y').strftime('%d/%m/%Y') }}"

(note %Y for 2022 rather than %y for 22)

If you use multi-line templates, you can make it more readable:

value_template: >
  {{
     strptime(
        value|
          replace('Data delayed at least 15 minutes, as of ', '')|
          replace('.',''),
        '%b %d %Y'
     ).strftime('%d/%m/%Y')
  }}

That’s very kind of you to explain it so clearly. Very much appreciated.