Help needed with intermittent template exception

Hi

I made an automation that monitors the changes of an iPhone GPS tracker to calculate the speed. That works, most of the times. Sporadically I get a template error that ‘max’ can’t compare str with float.

I’m staring blind I guess. Who understands how this error can be possible?!

I think I nailed down the error coming from `max’ in my automation (trigger by the iphone gps tracker):

  - variables:
     speed: |
           {% set dm = (distance(trigger.to_state, trigger.from_state) | float(0)) * 1000.0 %} 
           {% set dt = (as_datetime(trigger.to_state.last_updated) - as_datetime(trigger.from_state.last_updated)).total_seconds() | float(0) %}
           {{ (dm / <b>max(1.0,dt)</b>) | float(0) | round(1) }}

Please help

The full exception
Logger: homeassistant
Source: helpers/template.py:645
First occurred: 12:58:48 (1 occurrences)
Last logged: 12:58:48

Error doing job: Task exception was never retrieved (None)
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 643, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 2756, in _render_with_context
return template.render(**kwargs)
~~~~~~~~~~~~~~~^^^^^^^^^^
File “/usr/local/lib/python3.13/site-packages/jinja2/environment.py”, line 1295, in render
self.environment.handle_exception()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
File “/usr/local/lib/python3.13/site-packages/jinja2/environment.py”, line 942, in handle_exception
raise rewrite_traceback_stack(source=source)
File “”, line 2, in top-level template code
File “/usr/local/lib/python3.13/site-packages/jinja2/sandbox.py”, line 401, in call
return __context.call(__obj, *args, **kwargs)
~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 2268, in wrapper
return builtin_filter(environment, args, **kwargs)
File “/usr/local/lib/python3.13/site-packages/jinja2/filters.py”, line 543, in do_max
return _min_or_max(environment, value, max, case_sensitive, attribute)
File “/usr/local/lib/python3.13/site-packages/jinja2/filters.py”, line 503, in _min_or_max
return func(chain([first], it), key=key_func)
TypeError: ‘>’ not supported between instances of ‘str’ and ‘float’

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/trigger.py”, line 278, in async_with_vars
run_variables.update(trigger_variables.async_render(hass, run_variables))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/helpers/script_variables.py”, line 57, in async_render
rendered_variables[key] = template.render_complex(
~~~~~~~~~~~~~~~~~~~~~~~^
value, rendered_variables, limited
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 266, in render_complex
return value.async_render(variables, limited=limited, parse_result=parse_result)
~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 645, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: ‘>’ not supported between instances of ‘str’ and ‘float’

Update:
I moved the variables from the trigger, to the action part, and that probably seems to solve it, Need to monitor it for a while.

Probably because the sensor is unavailable.
You need to make sure the sensor is a valid state before doing this

Thanks for the suggestion, I don’t think that was the case. The trace at that time had valid content and this was the state at that time


What seems to be relevant is that it happens when the state changes form home to not_home or visa versa

In that case we can’t help much more since you have only provided this little of your automation or what it is.

Thanks for looking with me anyway :grinning:

Sharing the full automation will be too confusing. I just wonder how it is possible to get an input string, with all the defaults I added

@Hellis81
I moved the variable block from the trigger to the action part. Since then the errors are gone. Not sure what makes the difference:

  • your suggestion that it happened with triggers without a valid state object. Are those rejected for the remainder of the automation?
  • limited template support in the trigger part; but then why only intermittent
    Best Eric