How to modifiy the displayed "time left"?

Hi there,

I’ve created a template sensor, which should show the time remaining between now and a specific time-stamp.

This is my sensor:

    oilfox_kachelofen_updatetimeleft:
      value_template: >-
        {% set currentTime = as_timestamp(states('sensor.date_time_iso')) %}
        {% set nextUpdateTimestamp = as_timestamp(state_attr('sensor.oilfox_of7090002028_filllevelpercent', 'Next Measurement')) %}
        {% set timeLeft = nextUpdateTimestamp - currentTime  %}

        {% set minutes = (timeLeft / 60)|int % 60 %}
        {% set hours = (timeLeft / 3600)|int %}
        {{ [hours, minutes]|join(':') }}
      unit_of_measurement: hrs

The Sensor is pretty much working as I do expect…
There are just a few issues:

1: It is displaying only one digit instead of two:
– When the time left is for example:
10:36 hrs
5:12 hrs
0:5 hrs → in this case, I would like to display: 0:05 hrs
grafik

Also, another issue I’ve observed is:

  • When time now - has past the “next Measurement” timestamp - then, the counter is increasing (as long as the integration did not update the sensor information)

How could I prevent that?

thanks and with best regards,
Christoph

I’m pretty sure you can do this.

        {% set currentTime = as_timestamp(states('sensor.date_time_iso')) %}
        {% set nextUpdateTimestamp = as_timestamp(state_attr('sensor.oilfox_of7090002028_filllevelpercent', 'Next Measurement')) %}
        {% set timeLeft = nextUpdateTimestamp - currentTime  %}
        {% if timeLeft > 0 %}
          {% set minutes = (timeLeft / 60)|int % 60 %}
          {% set hours = (timeLeft / 3600)|int %}
          {{ '{:02}:{:02}'.format(hours, minutes) }}
        {% else %}
          00:00
        {% endif %}

I haven’t tested the syntax. Might have errors but you get the idea.

1 Like

Thanks - that’s working fine now :slight_smile: