Template variable warning

Hey there!

I have problem with this code and do not know what to add and where:

  - platform: template
    sensors:
      hassio_drifttid:
        friendly_name: 'Hassio Drifttid'
        value_template: >-
          {%- set word_for_and = 'och' %}
          {%- set up_time = as_timestamp(now())-as_timestamp(states('sensor.uptime')) %}
          
          {%- macro phrase(name, plural_name, divisor, mod=None) %}
            {%- set value = ((up_time // divisor) % (mod if mod else divisor)) | int %}
            {%- set name = plural_name if value > 1 else name %}
            {{- '{} {}'.format(value, name) if value | int > 0 else '' }}
          {%- endmacro %}
          
          {%- set values = [ 
                     phrase('dag', 'dagar', 60*60*24, 7),
                     phrase('timme', 'timmar', 60*60, 24),
                     phrase('min', 'min', 60)
                 ] | select('!=','') | list %}
                        
          {{ values[:-1] | join(', ') ~ ' ' ~ word_for_and ~ ' ' ~ values[-1] if values | length > 1 else values | first }}

Warning:

2021-04-21 19:26:15 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: No first item, sequence was empty. when rendering '{%- set word_for_and = 'och' %} {%- set up_time = as_timestamp(now())-as_timestamp(states('sensor.uptime')) %}
{%- macro phrase(name, plural_name, divisor, mod=None) %}
{%- set value = ((up_time // divisor) % (mod if mod else divisor)) | int %}
{%- set name = plural_name if value > 1 else name %}
{{- '{} {}'.format(value, name) if value | int > 0 else '' }}
{%- endmacro %}
{%- set values = [
phrase('dag', 'dagar', 60*60*24, 7),
phrase('timme', 'timmar', 60*60, 24),
phrase('min', 'min', 60)
] | select('!=','') | list %}
{{ values[:-1] | join(', ') ~ ' ' ~ word_for_and ~ ' ' ~ values[-1] if values | length > 1 else values | first }}'

What do I ADD and where?

Thanks!

Anyone that can help me?

+1 from me!!

I think the original template came from @petro - maybe he could help you out.

  - platform: template
    sensors:
      hassio_drifttid:
        friendly_name: 'Hassio Drifttid'
        value_template: >-
          {%- set word_for_and = 'och' %}
          {%- set up_time = as_timestamp(now())-as_timestamp(states('sensor.uptime')) %}
          
          {%- macro phrase(name, plural_name, divisor, mod=None) %}
            {%- set value = ((up_time // divisor) % (mod if mod else divisor)) | int %}
            {%- set name = plural_name if value > 1 else name %}
            {{- '{} {}'.format(value, name) if value | int > 0 else '' }}
          {%- endmacro %}
          
          {%- set values = [ 
                     phrase('dag', 'dagar', 60*60*24, 7),
                     phrase('timme', 'timmar', 60*60, 24),
                     phrase('min', 'min', 60)
                 ] | select('!=','') | list %}
                        
          {{ values[:-1] | join(', ') ~ ' ' ~ word_for_and ~ ' ' ~ values[-1] if values | length > 1 else values | first | default }}
2 Likes

Thanks @petro