Stringmanipulation with a Value

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.

# Example configuration.yaml entry
scrape:
  - resource: https://www.goldpreis.de/
    sensor:
      - name: Gold_Rubel
        unique_id: Gold_Rubel_Euro
        select: "body > div.container > div.row.mt20 > div:nth-child(1) > div.mainbox > div.row > div.col-md-9 > div:nth-child(2) > div > div.col-md-7.col-xs-8 > span"
        index: 0
        value_template: '{{ value | replace (".", "") }}'
        #value_template: '{{ value | replace (".", "") | replace(","."") | float }}'
        #value_templateA: '{{ value_template |replace(","."")|float }}'
        #unit_of_measurement: "€"

  - resource: https://www.goldpreis.de/
    sensor:
      - name: Gold_Wechselkurs
        unique_id: Gold_Wechselkurs
        select: "body > div.container > div.row.mt20 > div:nth-child(1) > div.mainbox > div.row > div.col-md-3 > div > div.au_wechselkurs"
        index: 0
        value_template: '{{ value | replace (",", ".")|float }}'
        #value_template: '{{ value | replace (".", "") | replace(","."") | float }}'
        #value_templateA: '{{ value_template |replace(","."")|float }}'
        #unit_of_measurement: "€"

Who can help?

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.

Replaced the wrong comma with a point several times :wink:
This would have come out easier if you had tried it in developer tools first.

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?

Sure, but I went with the value mentioned in the post, which was (‘4,034.39’)

With 4.034,39 I would do this

{{ value | replace('.', '') | replace(',', '.') }}

I’m still hoping someone will say: Hey Edwin_D, you make a good comma here… ehhh… point. :grinning:

1 Like

Missed opportunity indeed!

Good comma… ehhh… point!

1 Like