How do I use value_template?

I got this script to scrape data from a website. I want to shorten the output, but I don’t know how.

This is my script:

sensor:
  - platform: scrape
    index: "24"
    name: 00-01
    resource: https://www.epexspot.com/en/market-data?market_area=NL&trading_date=2020-04-21&delivery_date=2020-04-22&underlying_year=&modality=Auction&sub_modality=DayAhead&product=60&data_mode=table&period=
    select: '.child'
    scan_interval: 60

This is my current output:
1,438.3 1,934.3 1,934.3 300.01

My desired output would by only the last number:
300.01

Can anyone help me?

value_template: "{{ value.replace(',','').split()[-1] }}"

take the value (value), remove the commas (.replace(',','')) because commas in code do not go inside numbers. Then split the string (.split(' ')) based on spaces and choose the last ([-1]) item.

2 Likes

It still doesnt work. But I geuss my output was different than I showed earlier.

My former output was:
image

My current output is:
image

So I geuss it splits by different lines instead of a spacing.

try

value_template: "{{ value.replace(',','').split()[-1] }}"
1 Like

Thanks, it works!

Now I’m trying to create multiple “sensors” from one website.
Only my index number and name changes.

Could you help me with that?

So my current code is

sensor:
  - platform: scrape
    index: "24"
    name: 00-01
    resource: https://www.epexspot.com/en/market-data?market_area=NL&trading_date=2020-04-21&delivery_date=2020-04-22&underlying_year=&modality=Auction&sub_modality=DayAhead&product=60&data_mode=table&period=
    select: '.child'
    scan_interval: 60
    value_template: "{{ value.replace(',','').split()[-1] }}"

Could I maybe use something like this:

sensor:
  - platform: scrape
    resource: https://www.epexspot.com/en/market-data?market_area=NL&trading_date=2020-04-21&delivery_date=2020-04-22&underlying_year=&modality=Auction&sub_modality=DayAhead&product=60&data_mode=table&period=
    select: '.child'
    scan_interval: 60
    value_template: "{{ value.replace(',','').split()[-1] }}"
    sensors:
       sensor1:
            Index:"24"
            name:00-01
       sensor2:
            index:"25"
            name:01-02

So this code isn’t working but is something like this even possible?

You have to follow the documents. You can’t just make up yaml. Home assistants configuration is rigid and the documents spell out how to configure each platform or integration. In your case, you’ll have to copy/paste the whole item over and over again.

However, yaml has built in functionality called anchors and you can leverage them in this instance.

sensor:
  - index: "24"
    name: 00-01
    <<: &epexmarket
      platform: scrape
      resource: https://www.epexspot.com/en/market-data?market_area=NL&trading_date=2020-04-21&delivery_date=2020-04-22&underlying_year=&modality=Auction&sub_modality=DayAhead&product=60&data_mode=table&period=
      select: '.child'
      scan_interval: 60
      value_template: "{{ value.replace(',','').split()[-1] }}"

  - index: "25"
    name: 01-02
    <<: *epexmarket

  - index: "26"
    name: 02-03
    <<: *epexmarket
1 Like

I see you work with epexspot power prices.
Wouldn’t it be better to have one sensor showing all the values of the incoming day to show a graph like on the epexspot website?

How do you use the sensors in an automation?
I assume you will trigger charging your vehicle or something when prices are low (will be low perspectively)?

Thank you!