Scrape Number only

Hello, got a small problem with the scraping Integration
I want to fetch the price of wood pellets daily so i can monitor buying them.

image

I put in:

#ctl00_MainContent_PriceTrendView_TrendContent > div.pricetrend-prices > div:nth-child(1)
on Scrape.

How do i remove the “€” Symbol so Hass recognized it as a number?

By adding this in the template

{{ value.split(" ")[0] | float(0) }}

Now its 0…

image

Is there a space between 372,69 and €?
Maybe you need to replace “,” with “.”, you know, America :wink:

{{ value.split(" ")[0].replace(",",".") | float(0) }}

Sadly its still 0…

I use a template similar to what you need as follows:

{{states('sensor.pelletpreis') | replace(',', '.') | replace('€', '') }}

Here’s a trick from troon:

Rather than splitting the value up and trying to replace bad values, instead only allow the characters you want:

{{ value | select('in', ',.0123456789') | join | float(0) }}

I haven’t dealt with different region settings, you may or may not still need to replace the comma with a period.

{{ value | select('in', ',.0123456789') | join | replace(',', '.') | float(0) }}

So if i use the top one it says “0”

If i use to bottom one it says the price but with a “.” instead of a “,” “,” would be right so like 300,50

So i guess the first one is good but i still cant get any value spitting out…

Ok i think it works now, changed device class to energy :slight_smile:
Thanks for the awesome help!!

You did something wrong with my solution, because it was giving the exact same result

{{ "300,67 €".split(" ")[0].replace(",",".") | float(0) }}
{{ "300,67 €" | select('in', ',.0123456789') | join | replace(',', '.') | float(0) }}

image

But anyway …

You are assuming the space is actually a space. Try it with a different character that only looks like a space:


{{ "300,67\xA0€" }}
{{ "300,67\xA0€".split(" ")[0].replace(",",".") | float(0) }}
{{ "300,67\xA0€" | select('in', ',.0123456789') | join | replace(',', '.') | float(0) }}

I’m not assuming anything, I’ve checked on the link provided in post #1 with devtools.
image
And, yes, I saw the additional space at the end of the first <div> but [0] should still work.

I’d be interested to see what that shows when you copy the element to a text editor which shows non-printing characters. And it looks like different region settings affect what is being displayed as well.

I’m seeing the Euro sign on the opposite side, no space, and period instead of comma:
image

In a text editor you can see there are many spaces and some carriage returns and line feeds in that data:
image

Regardless, the point of the code from troon is to make all of that detail irrelevant and just pass the numbers through.