Python script on template sensor: ValueError: could not convert string to float: 'unavailable'

using a etc_offset template sensor:

      - unique_id: utc_offset
        name: Utc offset
        state: >
          {{now().utcoffset().total_seconds()/3600}}
        icon: >
          {% set offset = now().utcoffset().total_seconds()/3600 %}
          mdi:numeric-{{offset|int|abs}}

in my python scripts:

utc_offset = hass.states.get('sensor.utc_offset').state
timeDifference = float(utc_offset) # <-- line 22

every now and then I see this error in the homeassistant.log

Logger: homeassistant.components.python_script.summary_active.py
Source: components/python_script/__init__.py:222 
Integration: Python Scripts (documentation, issues) 
First occurred: 10:19:10 (1 occurrences) 
Last logged: 10:19:10

Error executing script: could not convert string to float: 'unavailable'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 222, in execute
    exec(compiled.code, restricted_globals)
  File "summary_active.py", line 22, in <module>
ValueError: could not convert string to float: 'unavailable'

it is used in the python script like:

for entity_id in hass.states.get(group).attributes['entity_id']:
    state = hass.states.get(entity_id)

    if (state.state == filter or debug):
        dt = state.last_changed + datetime.timedelta(hours= timeDifference)
        time = '%02d:%02d' % (dt.hour, dt.minute)

I guess it has to do with the new template default values, but am not sure how to fix this in the python script.

maybe like:

for entity_id in hass.states.get(group).attributes['entity_id']:
    state = hass.states.get(entity_id)

    if (state.state == filter and timeDifference is not None or debug):
        dt = state.last_changed + datetime.timedelta(hours= timeDifference)
        time = '%02d:%02d' % (dt.hour, dt.minute)

cant imagine it is the icon template throwing the issue?
Please have a look with me?

thanks.

Tempalte updates do not affect python_script, they are unrelated. The problem is the same, so check for invalid states before converting them to numbers.

yeah, but the only conversion is in the icon template?
so mdi:numeric-{{offset|int|abs}} should become mdi:numeric-{{offset|int(default=0)|abs}} ?

You’re converting a string to a float right here in python

right, I thought you said the template sensor needed checking., sorry for that.

if (utc_offset is not None):
    timeDifference = float(utc_offset)

but what does this do if it is actually None.?

odd thing is I had this for ages, in all of my python scripts using the timeDifference, and only this complains now.

Just use a try: except ValueError and it’ll catch everything that’s not a number

ok, like this? Might seem odd, but it is in none of my scripts. Using the is not None all over

try:
    timeDifference = float(utc_offset)
except:
    ValueError
#timeDifference = float(utc_offset)

https://www.programiz.com/python-programming/exception-handling

1 Like