0

I'm trying to compare two timestamps but getting a type error:

"TypeError: '<' not supported between instances of 'float' and 'str'

The template code is using 'as_timestamp' and the documentation recommended using float() when comparing.

I'm trying to get the name and date of the sensor that has a date value that is closest to now() (in Home Assistant). So I'm trying to do the following:

    {%- set list = ['sensor.1','sensor.2','sensor.3','sensor.4'] -%}
    {%- set data = namespace(min_date=float(as_timestamp(now() + timedelta(days = 360))), min_name = '') -%}
    {%- for i in list %}
    {%- set value = states(i) %}
    {%- if float(as_timestamp(value)) < float(data.min_date) %}
    {%- set data.min_date = value -%}
    {%- set data.min_name = state_attr(i, 'friendly_name') -%}
    {% endif %}
    {%- endfor %}
    {{ data.min_name + ' (' + data.min_date | timestamp_custom('%Y-%m-%dT%H:%M:%S%z') + ')' }}

Any ideas why data.min_date is a str in the comparison, despite the value being defined with both float() and as_timestamp() ?

Chrisvdberge
  • 1,824
  • 6
  • 24
  • 46

1 Answers1

0

had to remove the 'as_timestamp' from the initial value assignment and add it to the if statement. that solved it.

Chrisvdberge
  • 1,824
  • 6
  • 24
  • 46