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>
This commit is contained in:
Franck Nijhof 2025-04-16 08:14:36 +02:00 committed by GitHub
parent a6f946c923
commit ed0bfaeb05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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.