Scrape temperature

I had scrape configured to scrape the temperature from a website, but suddenly it is not working anymore. The data on the page looks like this:

<data>

<sea>

<depth>

<dval>

<![CDATA[ 1.0 ]]>

</dval>

<date>

<![CDATA[ 2023-06-12 ]]>

</date>

<time>

<![CDATA[ 17:03:40 ]]>

</time>

<measure>

<type>

<![CDATA[ 0 ]]>

</type>

<value>

<![CDATA[ 17.65 ]]>

</value>

</measure>

<measure>

<type>

<![CDATA[ 1 ]]>

</type>

<value>

<![CDATA[ 22.47 ]]>

</value>

</measure>

</depth>

This used to work:


But after the latest update it stopped working.

The logfile says:
ValueError: Sensor sensor.temp75m has device class ‘temperature’, state class ‘measurement’ unit ‘°C’ and suggested precision ‘None’ thus indicating it has a numeric value; however, it has the non-numeric value: ‘’ (<class ‘str’>)

I’m assuming value contains <![CDATA[ 22.47 ]]> as a string.

This is a relatively new change to HA where sensors with “unit_of_measurement” need to be numeric. You’re stripping out the parts around the number, but leaving the leading and trailing spaces. You can also parse it as a float with a default value to cater for this:

{{ value.lstrip('<![CDATA[').rstrip("]]>") | float(0) }}

Note that techinically “lstrip” and “rstrip” strip any matching character from the left/right and stop when a non-matching character is found, so “<![CDATA[” and “<![CDAT” are the same, although I would probably also do the same.

Note that HA can also convert XML to JSON, so there may be a simpler way to achieve what you’re doing.

Tried to add space and float(0) to the template:
{{ value.lstrip('<![CDATA[ ').rstrip(" ]]>") | float(0) }}

The sensor is now only displaying 0°C.