Subtract one time from another - what is going wrong?

I have two Helper datetimes set up using the UI:

input_datetime.zappi_schedule_1_start_time
input_datetime.zappi_schedule_1_end_time

In the UI under “What do you want to input?” they are both set to “Time”. If I look at one in Developer Tools, with it set to 6AM, it looks like this:

has_date: false
has_time: true
editable: true
hour: 6
minute: 0
second: 0
timestamp: 21600
icon: mdi:ev-station
friendly_name: zappi Schedule 1 End Time

And the ‘State’ is reported as 06:00:00. All I want to do is subtract one from the other, giving me an answer in seconds or minutes or hours and minutes (I’m past caring now!). This is as a sensor in templates.yaml. I cannot seem to get any maths to work, regardless of whether I cast to an int, or use today_at() or as_timestamp(). The sensor just reports as unavailable, even when the two times are fine.

Some examples of what I’ve tried (after reading various threads on here)…

{% set ts_end = as_timestamp(states('input_datetime.zappi_schedule_1_end_time')) | int %}
{% set ts_start = as_timestamp(states('input_datetime.zappi_schedule_1_start_time')) | int %}
{{ timedelta(seconds=(ts_end - ts_start)) }}

and this…

{% set ts_end = as_timestamp(states('input_datetime.zappi_schedule_1_end_time')) %}
{% set ts_start = as_timestamp(states('input_datetime.zappi_schedule_1_start_time')) %}
{{ ts_end - ts_start }}

and this…

{% set ts_start = today_at(states('input_datetime.zappi_schedule_1_start_time')) | int %}
{% set ts_end = today_at(states('input_datetime.zappi_schedule_1_end_time')) | int %}
{% set total_min = (ts_end - ts_start).total_seconds() / 60 %}
{{ ts_start }}

If I strip it right back and try to do this just to prove that I can get something from it my mimicking one of the Time sensors:

{{ states('input_datetime.zappi_schedule_1_end_time') }}

then it correctly reports 06:00:00, but doing anything else causes it to become unavailable again.

I have been testing with the ‘end time’ set to 06:00:00 and the ‘start time’ set to 02:00:00, so there shouldn’t be any issues with crossing over midnight, negative numbers, etc.

I realise this should be really easy (at least, I think it should) so would be grateful for any guidance as to whether it really is that difficult, or I’ve done something silly (I suspect the latter!). Many thanks.

Try this:

{% set ts_end = state_attr('input_datetime.zappi_schedule_1_end_time','timestamp') %}
{% set ts_start = state_attr('input_datetime.zappi_schedule_1_start_time','timestamp') %}
{{ timedelta(seconds = ts_end - ts_start) }}

Also this would have worked if you did not convert your datetime objects to integers.

{% set ts_start = today_at(states('input_datetime.zappi_schedule_1_start_time')) | int %}
{% set ts_end = today_at(states('input_datetime.zappi_schedule_1_end_time')) | int %}
{% set total_min = (ts_end - ts_start).total_seconds() / 60 %}
{{ ts_start }}

So like this:

{% set ts_start = today_at(states('input_datetime.zappi_schedule_1_start_time')) %}
{% set ts_end = today_at(states('input_datetime.zappi_schedule_1_end_time')) %}
{{ ts_end - ts_start }}