Parse duration string: as_timedelta

The as_timedelta template function parses an ISO 8601 duration string and returns a Python timedelta object. Give it a string like “PT1H30M” (1 hour and 30 minutes) or “P2DT6H” (2 days and 6 hours), and it returns a timedelta you can use in time calculations.

Some integrationsIntegrations connect and integrate Home Assistant with your devices, services, and more. [Learn more] and external services provide durations in ISO 8601 format. To use these values in calculations, such as adding a duration to the current time or comparing how long something will take, you need to convert them into timedelta objects first. as_timedelta does this for you. If you need to create a timedelta from numeric values (days, hours, minutes) rather than parsing a string, use timedelta instead.

Usage

Here’s how to use this template function. Copy any example and adjust it to your setup.

TemplateA template is an automation definition that can include variables for the action or data from the trigger values. This allows automations to generate dynamic actions. [Learn more]
{{ as_timedelta("PT1H30M") }}
Result (timedelta)
1:30:00

Function signature

The signature is a technical summary of this template function. It shows the name of the function, the values (called parameters) it accepts, and what type of data each parameter expects (for example, a piece of text or a number).

Function parameters that have a = with a value after them are optional. If you leave them out, the default value shown is used automatically. Function parameters without a default are required.

as_timedelta(
    value: str,
) -> timedelta | None

Function parameters

The following parameters can be provided to this function.

value string Required

An ISO 8601 duration string to parse. Common formats include “PT1H” (1 hour), “PT30M” (30 minutes), “P1D” (1 day), and “P2DT6H30M” (2 days, 6 hours, 30 minutes). Returns None if the string cannot be parsed.

ISO 8601 duration format

The ISO 8601 duration format uses P as a prefix, followed by date components and T to separate time components:

  • P1D - 1 day
  • PT1H - 1 hour
  • PT30M - 30 minutes
  • PT45S - 45 seconds
  • P2DT6H30M - 2 days, 6 hours, 30 minutes
TemplateA template is an automation definition that can include variables for the action or data from the trigger values. This allows automations to generate dynamic actions. [Learn more]
{{ as_timedelta("P2DT6H30M") }}
Result (timedelta)
2 days, 6:30:00

Good to know

  • Returns None when the string cannot be parsed, so guard against missing data before arithmetic.
  • Only the ISO 8601 duration format is accepted. A plain string like "1h30m" will not work. For numeric inputs, use timedelta.
  • Year and month components (Y, M before T) are not supported because their length in seconds is not fixed.

Try it yourself

Ready to test this? Open Developer tools > Template, paste the example into the Template editor, and watch the result update on the right. Edit the values to see how the function adapts to your own entitiesAn entity represents a sensor, actor, or function in Home Assistant. Entities are used to monitor physical properties or to control other entities. An entity is usually part of a device or a service. [Learn more].

More examples

Real scenarios where this function comes up in automations and templates. Copy any example and adapt it to your setup.

Add a duration to the current time

Calculate when an event will end by adding a duration from a sensor to now.

TemplateA template is an automation definition that can include variables for the action or data from the trigger values. This allows automations to generate dynamic actions. [Learn more]
{{ now() + as_timedelta("PT2H30M") }}
Result (datetimeA value representing a specific moment in time, including the date, time, and time zone. For example, 2026-04-05 14:30:00+00:00. Used for timestamps, scheduling, and time-based calculations.)
2024-03-15 17:00:00.123456+01:00

Get total seconds from a duration

Convert an ISO 8601 duration string to a total number of seconds for use in numeric comparisons.

TemplateA template is an automation definition that can include variables for the action or data from the trigger values. This allows automations to generate dynamic actions. [Learn more]
{{ as_timedelta("PT1H30M").total_seconds() | int }}
Result (integerA whole number without decimal places, like 1, 42, or -5. Used for counts, indices, and whole values.)
5400

Check if a remaining duration is short

Determine whether a timer or countdown is about to finish by comparing the duration to a threshold.

TemplateA template is an automation definition that can include variables for the action or data from the trigger values. This allows automations to generate dynamic actions. [Learn more]
{{
  as_timedelta(states("sensor.washer_remaining"))
  < as_timedelta("PT10M")
}}
Result (booleanA value that is either true or false. Used for on/off states, yes/no conditions, and similar binary choices.)
true

Still stuck?

The Home Assistant community is quick to help: join Discord for real-time chat, post on the community forum with your template and expected result, or share on our subreddit /r/homeassistant.

Tip

AI assistants like ChatGPT or Claude can also explain or fix templates when you describe what you want in plain language.

Related template functions

These functions work well alongside this one: