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.
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?
Olivier1974
(Olivier Toussaint)
November 16, 2023, 9:12am
2
By adding this in the template
{{ value.split(" ")[0] | float(0) }}
Olivier1974
(Olivier Toussaint)
November 16, 2023, 10:24am
4
Is there a space between 372,69 and €?
Maybe you need to replace “,” with “.”, you know, America
{{ value.split(" ")[0].replace(",",".") | float(0) }}
Krivatri
(Krivatri)
November 16, 2023, 11:14am
6
I use a template similar to what you need as follows:
{{states('sensor.pelletpreis') | replace(',', '.') | replace('€', '') }}
mekaneck
(Rick Auch)
November 16, 2023, 12:55pm
7
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
Thanks for the awesome help!!
Olivier1974
(Olivier Toussaint)
November 17, 2023, 3:31pm
10
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) }}
But anyway …
mekaneck
(Rick Auch)
November 17, 2023, 5:21pm
11
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) }}
Olivier1974
(Olivier Toussaint)
November 17, 2023, 6:21pm
12
I’m not assuming anything, I’ve checked on the link provided in post #1 with devtools.
And, yes, I saw the additional space at the end of the first <div>
but [0]
should still work.
mekaneck
(Rick Auch)
November 18, 2023, 2:32pm
13
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:
In a text editor you can see there are many spaces and some carriage returns and line feeds in that data:
Regardless, the point of the code from troon is to make all of that detail irrelevant and just pass the numbers through.