WTH date/duration template is a mess

Working with datetime (and duration) is very hard on Home Assistant :

Even with sensor correctly created and flagged like :

  • sensor.start_variable_batterie :
state: 2024-12-06T21:01:00+00:00
device_class: timestamp
  • sensor.dure_variable_batterie :
state: 28680
state_class: measurement
unit_of_measurement: s
device_class: duration

It’s need to re-transformed each time it’s needed :

{{ states('sensor.start_variable_batterie') | as_datetime +
    states('sensor.dure_variable_batterie') | as_timedelta }}

Even worst, the sensor.dure_variable_batterie is created in template.yaml, if i change via the UI unit_of_measurement to hours, in dashboard, the sensor display is better but previous template is not working anymore ( datetime + timedelta )

generaly manipulate date and datetime in homeassistant is complex and take lot of time :
exemple 1:

{%- set time = ((state_attr('automation.lave_linge_fin','last_triggered')) - (state_attr('automation.lave_linge_start','last_triggered'))).seconds -%}
{%- set minutes = ((time % 3600) / 60) | round | int -%}
{%- set hours = ((time % 86400) / 3600) | int -%}

Begin : {{ ((state_attr('automation.lave_linge_start','last_triggered'))| as_local).strftime("%Hh%M") }} 
Duration {{ '{:01}h{:02}m'.format(hours,minutes) }}

example 2: my equipement (victron) work in number of second since midnigh so i need 2 automation to convert this number to datetime and it’s not easy to found how to do it :
1:

action: input_datetime.set_datetime
metadata: {}
data_template:
  time: >-
    {% set hours = ( states('number.victron_multiplus_battery_schedule_2_start') | int(default=0) / 3600 )| round(0,'ceil') -1 %}
    {% set minutes = ( states('number.victron_multiplus_battery_schedule_2_start') | int(default=0) /60 % 60 ) | round(0,'ceil') %}
    {{ '{:02}:{:02}:{:02}'.format(hours, minutes, 0) }}
target:
  entity_id: input_datetime.cerbo_schedule_2_start

2:

action: number.set_value
metadata: {}
data:
  value: >-
    {{ state_attr('input_datetime.cerbo_schedule_2_start','timestamp')  }}
target:
  entity_id: number.victron_multiplus_battery_schedule_2_start

And i don’t speak of sensor need to transform from timestamp to time to be able to used as trigger in automation

What do you propose to make it easier? Historically, we are at the simpliest form for using datetimes.

Also, your second example can be simplified and it will just do all the conversions for you.

time: >
  {% set hours = states('number.victron_multiplus_battery_schedule_2_start') | float %}
  {{ timedelta(seconds=hours*3600) }}

As for this:

it’s likely not needed.

As an observer to has a good handle on time, to me it seems like you’ve gotten outdated examples on the forums and ran with them in templates instead of using as_datetime, as_local, and as_timedelta. All things that were created to make date & time conversions easier in templates.

Lastly, many fields in triggers and conditions now accept datetime sensors, input_datetimes, and input_numbers and allow offsets, which typically negate the need for any templates in general. I suggest you post what this full system is doing as it likely can be simplified into 1 or 2 triggers/conditions (without templates) in your automation.