Calculation of time and duration in templates

Hello everyone,

I have a problem with the calculation of times and durations in templates and have been trying to find a solution for hours without success.

Starting position:
I have a sensor for public transport that outputs the arrival time at the destination as a string in the format HH:MM. It also gives me the current delay at the destination as an integer in minutes.

In a template, I would now like to add both values to a time in order to create a message in the sense of “the expected arrival time is HH:MM”.

{{ state_attr('sensor.sbahn_to_home', 'arrival')  }}
{{ state_attr('sensor.sbahn_to_home', 'delay_arrival')  }}
{{ (state_attr('sensor.sbahn_to_home', 'delay_arrival') *60) | timestamp_custom("%H:%M", 0) }} 


{% set arrival_datetime = strptime(state_attr('sensor.sbahn_to_home', 'arrival'), "%H:%M", today_at ('00:00:00')) %} 
{{ arrival_datetime }}

{% set delayminutes = (state_attr('sensor.sbahn_to_home', 'delay_arrival') *60) | timestamp_custom("%H:%M", 0) %}
{{ delayminutes }}

{% set delaytime_datetime = strptime(delayminutes, '%H:%M', today_at ('00:00:00')) %}
{{ delaytime_datetime }}


{{ arrival_datetime + delaytime_datetime }}

Results to the Error message:

"TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'datetime.datetime'" 

I have now reached the end of my ideas. What am I doing wrong?

Many thanks in advance for your help


{% set arrival_datetime = today_at(state_attr('sensor.sbahn_to_home', 'arrival')) %} 
{{ arrival_datetime }}

{% set delayminutes = timedelta(minutes = state_attr('sensor.sbahn_to_home', 'delay_arrival')) %}
{{ delayminutes }}

{{ arrival_datetime + delayminutes }}
{{ (arrival_datetime + delayminutes).strftime('%H:%M') }}

EDIT

Correction. Added missing closing parenthesis to first line (error reported in message below).

1 Like

That worked, thanks for the quick help! Now I just have to find out why it works.

By the way, the first line in your example code is missing a “)” at the end of the line. Just in case someone has the same problem in the future and looks at your solution.

1 Like

Thanks for bringing the mistake to my attention. I have corrected it in the example posted above.

The template for arrival_datetime uses today_at to convert the arrival time, which is a string value, to a datetime object where the date is set to today and the time is the arrival time.

The template for delayminutes uses timedelta to convert the delay time, which is a numeric string value representing minutes, to a timedelta object.

Adding the datetime object with a timedelta object produces a datetime object (that’s been incremented by the amount of the timedelta object). It displays the computed time and date (but in the format of a datetime object).

The final template simply displays the computed datetime object as a time string only.

1 Like

Thank you very much for your explanation!
My mistake was to assume that datetime objects can be added together. This is obviously not the case and you can only add datetime objects with timedelta objects. Did I understand that correctly?

Correct. That’s what the error message indicates:

unsupported operand type(s) for +: ‘datetime.datetime’ and ‘datetime.datetime’