Sensor template remaining time until

Hi!

Guys need help, I’m racking my brains with a template - I need to show the time from the current one, how much is left from now until 00:45.

For example, right now is 10:07 PM, so I need output sensor like this: 2h 38m - it’s a time 'till 12:45 AM from 10:07 PM.

I hope I explained it transparently enough. I would be grateful for the help, I myself will not master it. :smiley:

The tricky part here is:

  • The target time 00:45 is tomorrow if the current time is before midnight.
  • The target time 00:45 is today if the current time is between midnight and 00:45.
{% set t = now().replace(hour=0, minute=45, second=0, microsecond=0) %}
{{ ((t + timedelta(days=1) if now() > t else t) - now()).total_seconds()
   | timestamp_custom('%-Hh %-Mm', false) }}
3 Likes

That was tricky, love it !

(Had a solution too, but when seeing this, do not want to post ! I’m out lol)

Wow! @123 so fast! :slight_smile: Awesome! Thanks homie :+1:

@123 could u help with one thing also, if hours is 0 we can show only minutes? like only %-M without %-H in this template? Thanks.

If the remaining time has no hours portion that means it’s less than 60 minutes (3600 seconds).

{% set t = now().replace(hour=0, minute=45, second=0, microsecond=0) %}
{% set r = ((t + timedelta(days=1) if now() > t else t) - now()).total_seconds() %}
{{ r | timestamp_custom('%-Hh %-Mm' if r > 3600 else '%-Mm', false) }}
2 Likes

Oh damn it! You’re kidding me :upside_down_face: I just went to make a template and your answer immediately arrives, thanks buddy. I was almost on the right way… :sweat_smile: Thanks again! :+1:

1 Like