Hello,
I have managed (I am a complete beginner) to obtain three values from a website (via Srape).
Gold price in euros, gold price in dollars and exchange rate.
Unfortunately, I get the values as a string. Now I have to convert the string (‘4,034.39’) into a number so that I can calculate with it. I was able to remove the decimal point, but when replacing the comma ‘,’ with a full stop ‘.’ I fail miserably.
There’s no point using the float here unless you are doing more operations in this template. The template is defining a state, and states are always strings.
In testing, it looks like your scrape sensor returns “4.034,39” for value, so you would use the following:
value_template: '{{ value | replace (".","") | replace (",",".") }}'
Even though it’s unlikely that it will ever need the first replace, you can use the same thing for the exchange rate.
to convert the string '‘4,034.39’ to a number, you only need to remove the ,. With it, the template parser will convert it into a list with the numbers 4 and 34.39 as items.
So, if you wan the sensor state to be a number, you only need to do:
value_template: "{{ value | replace (',','') }}"
which, like @Edwin_D mentioned, you can test in developer tools > template
Unless the German website returned “4.034,39” (European notation) and not 4,034.39 (English) as was mentioned in the post. Was that literal or a typo too?