Template Editor Not Behaving

I’m trying to test this code in the template editor and getting no results:

{%- macro formatValue(name, value, sep='') %}
  {%- set name = '{}s ago'.format(name) if value > 1 else name %}
  {%- set name = '{}s'.format(name) if value < 1 else name %}
  {%- set value2 = 'In {}'.format(value) if value < 1 else value %}
  {{ '{} {}{}'.format(value2, name, sep) if value != 0 else '' }}
{%- endmacro %}

{%- set time = (as_timestamp(now()) - as_timestamp(states.sensor.washing_machine_remaining_program_time.state)) | int %}

{%- set seconds = (time if time < 60 and time > -60 else 0) %}
{%- set minutes = ((time % 3600) // 60) %}
{%- set hours = ((time % 86400) // 3600) %}

{{ formatValue('second', seconds) }}
{{ formatValue('minute', minutes) }}
{{ formatValue('hour', hours) }}

I’ve also noticed that the boilerplate template code isn’t working correctly either, the temperature is not output in the right output results panel. Is it something I’m doing wrong?

I’m running version 0.112.0 docker.

Upgrade to 0.112.1

1 Like

Your template generates for me (in my test case) : -

22 minutes ago

3 hours ago

Or are you refering to (what Phil was talking about) Where 0.112.0 misses off the first 4 lines of output ? (actually starts output 4 lines above display space)

Regardless your template is quite long, I tend to standardise such “ago” with a common format calculated from seconds, so : -

          {% set secs = as_timestamp(states('input_datetime.id_cool_last_off')) - as_timestamp(states('input_datetime.id_cool_last_on')) %}
          {% set days = (secs / (24 * 60 * 60)) | int %}
          {% set plural = ' days ' if days > 1 else ' day ' %}
          {% set hrmn = secs | timestamp_custom('%H:%M', false)  ~ ' ago'  %}
          {{ days ~ plural ~ hrmn if days > 0 else hrmn }}

Note I added the ’ ago’ just for you :rofl:

Note: Seeing as petro just liked this post, I’m obliged to point out that ‘some’ of the above template was stolen from inspired by petro’s efforts on another post :rofl:

2 Likes