From ed0bfaeb0534a5c257058bd59e0a8d3aa20abbf6 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Wed, 16 Apr 2025 08:14:36 +0200 Subject: [PATCH] Re-instate relative_time template method into docs (#38560) * Re-instate relative_time template method into docs * tiny tweaks --------- Co-authored-by: c0ffeeca7 <38767475+c0ffeeca7@users.noreply.github.com> --- source/_docs/configuration/templating.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/_docs/configuration/templating.markdown b/source/_docs/configuration/templating.markdown index 931a30d7008..180d5204dbe 100644 --- a/source/_docs/configuration/templating.markdown +++ b/source/_docs/configuration/templating.markdown @@ -744,6 +744,8 @@ For example, if you wanted to select a field from `trigger` in an automation bas - `as_timestamp(value, default)` converts a datetime object or string to UNIX timestamp. If that fails, returns the `default` value, or if omitted raises an error. This function can also be used as a filter. - `as_local()` converts a datetime object to local time. This function can also be used as a filter. - `strptime(string, format, default)` parses a string based on a [format](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior) and returns a datetime object. If that fails, it returns the `default` value or, if omitted, raises an error. +- `relative_time` converts a datetime object to its human-friendly "age" string. The age can be in seconds, minutes, hours, days, months, or years (but only the biggest unit is considered. For example, if it's 2 days and 3 hours, "2 days" will be returned). Note that it only works for dates _in the past_. + - Using `relative_time()` will cause templates to be refreshed at the start of every new minute. - `time_since(datetime, precision)` converts a datetime object into its human-readable time string. The time string can be in seconds, minutes, hours, days, months, and years. `precision` takes an integer (full number) and indicates the number of units returned. The last unit is rounded. For example: `precision = 1` could return "2 years" while `precision = 2` could return "1 year 11 months". This function can also be used as a filter. If the datetime is in the future, returns 0 seconds. A precision of 0 returns all available units, default is 1.