Strip a comma from a sensor value

Hi,
I’m using the scrape sensor to bring in my current solar output and today’s peak from PVoutput.org.

When the Watts goes over 999, the number on the webpage gets a comma (eg 1,000 and over gets a comma). This is making HA interpret it as a string and not a number, and this is played out in my graphs which go to zero as soon as the value goes over 999.

Can I use a value template to strip out the comma? I’ve been doing some digging but haven’t found a way to do it.

Any ideas?

Here is my sensor code;

  - platform: scrape
    resource: "http://pvoutput.org/intraday.jsp?id=38497&sid=35231"
    name: Solar
    select: '#dashPowerOut'
    unit_of_measurement: 'W'    

  - platform: scrape
    resource: "http://pvoutput.org/intraday.jsp?id=38497&sid=35231"
    name: Solar Peak
    select: '#dashPowerPeak'
    unit_of_measurement: 'W'

ok, so I couldn’t wait so had a further dig into the jinja doco and found replace. (I didn’t realise jinja was a form of Python so figured python doco could help too).

Here’s the data_template I’ve used and it seems to work…

  - platform: scrape
    resource: "http://pvoutput.org/intraday.jsp?id=38497&sid=35231"
    name: Solar
    select: '#dashPowerOut'
    unit_of_measurement: 'W'
    value_template: '{{ value.replace(",", "") }}'    

If you have any other thoughts on a better way that would be appreciated…

Thx
JP

3 Likes

Thanks for following up with your solution, I will be using this in the future.

works for me too

I know it’s been a long time since you posted this, but its still helping people out in 2023.

The error message that I got (and eventually led me to your issue) was from
multiscrape in homeassistant. I got these errors:

numerical_value = float(value) # type:ignore[arg-type]
ValueError: could not convert string to float: ‘1,053.90’

It took me a while to figure out that it was the comma. Thanks so much for finding, and fixing your issue!
I hope anyone else getting those errors in Home Assistant will be able to find this and get help.

I really wish the developers who maintain the Convert String to Float would make their error message a bit more helpful. Instead of saying “could not convert”, they could have said "Could not convert because unexpected character ‘,’ found in string.