Retriving hours and minutes until next alarm

Hi,

I’m trying to show the hours and minutes until the next alarm in my dashboard.
The alarm is stored in a input datetime helper with just time.

I have this template:

{{ time_until(as_datetime(strptime(states(‘input_datetime.alarm’),“%H:%M”)),1) }}

But it fails with the following error:
ValueError: Template error: strptime got invalid input ‘09:00:00’ when rendering template ‘{{ time_until(as_datetime(strptime(states(‘input_datetime.alarm’),“%H:%M”)),1) }}’ but no default was specified

Any help would be greatly appriciated.

This isn’t going to be that easy. For a start you will need to determine if this alarm is today or tomorrow. If it is tomorrow you will need to add a day.

See if this works first, then we’ll worry about the output format.

{% if now() < today_at(states('input_datetime.alarm')) %}
  {{ time_until(today_at(states('input_datetime.alarm'))) }}
{% else %}
  {{ time_until(today_at(states('input_datetime.alarm')) + timedelta(days=1)) }}
{% endif %}

Thanks! It gives me “x hours”. I would like to get “x hours and x minutes” and then translate hours and minutes into my native language.

time_until() will return the precision you specify, just be aware that if you want “hours and minutes” you will also get “minutes and seconds” when the time is below an hour. You can use replace() to translate the units.

{% set alarm = today_at(states('input_datetime.alarm')) %}
{% set d = 0 if now() < alarm else 1 %}
{{ time_until(alarm + timedelta(days=d), precision = 2).replace('hour', 'ora')

If you want more specific control you might want to check out the Relative Time Plus or Easy Time custom macros.

1 Like

Thanks! That did the trick.