Then other solutions may be easier:
https://community.home-assistant.io/t/template-sensor-template-variable-warning-with-ha-2021-4/297237/9
https://community.home-assistant.io/t/the-epic-time-conversion-and-manipulation-thread/85786/238
https://community.home-assistant.io/t/hunanizing-history-stats/470978/2
{%- set closed = state_attr('input_datetime.gate_closed','timestamp') %}
{%- set open = state_attr('input_datetime.gate_open','timestamp') %}
{%- set diff = closed - open %}
{% macro phrase(name, plural_name, divisor, mod=None) %}
{%- set value = ((diff // divisor) % (mod if mod else divisor)) |int %}
{%- set name = plural_name if value > 1 else name %}
{{- '{} {}'.format(value, name) if value |int > 0 else '' }}
{%- endmacro %}
{%- set values = [
phrase('hour', 'hours', 60*60),
phrase('minute', 'minutes', 60),
phrase('second', 'seconds', 1, 60)
] | select('!=','') | list %}
{{ values[:-1] | join(', ') ~ ', ' ~ values[-1] if values |length > 1 else values |first |default }}
Or, if you want the value to be displayed as in your initial question:
{%- set closed = state_attr('input_datetime.gate_closed','timestamp') %}
{%- set open = state_attr('input_datetime.gate_open','timestamp') %}
{% set diff = closed - open %}
{%- set h = diff |timestamp_custom('%H:%M:%S', False) %}
{%- macro phrase(name, plural_name, divisor, mod=None) %}
{%- set value = ((diff // divisor) % (mod if mod else divisor)) |int %}
{%- set name = plural_name if value > 1 else name %}
{{- '{} {}'.format(value, name) if value |int >=1 else '' }}
{%- endmacro %}
{%- set values = [
phrase('minute', 'minutes', 60),
phrase('second', 'seconds', 1, 60),
] |select('!=', '') |list %}
{%- if diff < 3600 %}
{{ values[:-1] | join(', ') ~ ', ' ~ values[-1] if values |length > 1 else values |first |default }}
{%- else %} {{ h }}
{% endif %}