Expressing alarm sensor as days / hours / minutes in future

I have a sensor from the HA mobile app that reflects the next alarm set on my phone. I’m trying to express it as something similar “The next alarm will happen in 1 day, 2 hours and 10 minutes”. I can work out the if/then logic to handle when I have or don’t have days/hours (alarm is less than a day or less than an hour).

I’m having trouble isolating the number of hours or minutes in the future that the alarm is set for.

{% set next_alarm = as_local(as_datetime(states('sensor.phone_next_alarm'))) %}
{% set alarm_diff = (next_alarm - now()) %}

next alarm: {{next_alarm}}
day of the week of next alarm: {{next_alarm.strftime('%A')}}
hour of next alarm: {{next_alarm.hour}}
minutes of next alarm: {{next_alarm.minute}}

Timedelta {{alarm_diff}}

how many days in the future: {{alarm_diff.days}}

which yields something like

next alarm: 2022-01-07 11:30:00-05:00
day of the week of next alarm: Friday
hour of next alarm: 11
minutes of next alarm: 30

Timedelta 2 days, 1:03:59.990890

how many days in the future: 2

But there doesn’t appear to be a “minutes” or “hours” attribute of the alarm_diff (like {{alarm_dif.minutes}} or {{alarm_diff.hours}}

Can someone point me in the right direction?

You have to do the math on alarm_diff.seconds

Timedelta only stores milliseconds, seconds, and days internally.

You can use a template to calculate the values in your desired output format:

{% set next_alarm = as_local(as_datetime(states('sensor.phone_next_alarm'))) %}
{% set alarm_diff = (next_alarm - now()) %}

{{ alarm_diff.days~' days ' if (alarm_diff.days > 0) }}
{%- set alarm_diff = alarm_diff.seconds | int %}
{{- alarm_diff // 3600~' hours ' if (alarm_diff > 3600) }}
{{- ((alarm_diff / 60) - (alarm_diff // 3600)*60) | round(0) | int~' minutes ' if (alarm_diff > 60) }}

Perfect. Thanks.

Thanks too for an example of the usage of ~
I hadn’t come across that yet.