mirror of
https://github.com/home-assistant/home-assistant.io.git
synced 2025-07-17 22:36:51 +00:00
Add Relative_time and today_at updates (#25986)
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
parent
74a41104bf
commit
fd2dfb9bbc
@ -554,7 +554,7 @@ For example, if you wanted to select a field from `trigger` in an automation bas
|
||||
|
||||
### Time
|
||||
|
||||
`now()` and `utcnow()` are not supported in [limited templates](#limited-templates).
|
||||
`now()`, `relative_time()`, `today_at()`, and `utcnow()` are not supported in [limited templates](#limited-templates).
|
||||
|
||||
- `now()` returns a datetime object that represents the current time in your time zone.
|
||||
- You can also use: `now().second`, `now().minute`, `now().hour`, `now().day`, `now().month`, `now().year`, `now().weekday()` and `now().isoweekday()` and other [`datetime`](https://docs.python.org/3.8/library/datetime.html#datetime.datetime) attributes and functions.
|
||||
@ -564,9 +564,11 @@ For example, if you wanted to select a field from `trigger` in an automation bas
|
||||
- Using `utcnow()` will cause templates to be refreshed at the start of every new minute.
|
||||
- `today_at(value)` converts a string containing a military time format to a datetime object with today's date in your time zone.
|
||||
|
||||
- Using `today_at()` will cause templates to be refreshed at the start of every new minute.
|
||||
|
||||
{% raw %}
|
||||
|
||||
```yaml
|
||||
```text
|
||||
# Is the current time past 10:15?
|
||||
{{ now() > today_at("10:15") }}
|
||||
```
|
||||
@ -578,11 +580,12 @@ For example, if you wanted to select a field from `trigger` in an automation bas
|
||||
- `as_local()` converts 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.10/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 datetime object to its human-friendly "age" string. The age can be in second, minute, hour, day, month or year (but only the biggest unit is considered, e.g., 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.
|
||||
- `timedelta` returns a timedelta object and accepts the same arguments as the Python `datetime.timedelta` function -- days, seconds, microseconds, milliseconds, minutes, hours, weeks.
|
||||
|
||||
{% raw %}
|
||||
|
||||
```yaml
|
||||
```text
|
||||
# 77 minutes before current time.
|
||||
{{ now() - timedelta( hours = 1, minutes = 17 ) }}
|
||||
```
|
||||
@ -593,7 +596,7 @@ For example, if you wanted to select a field from `trigger` in an automation bas
|
||||
|
||||
{% raw %}
|
||||
|
||||
```yaml
|
||||
```text
|
||||
# Renders to "00:10:00"
|
||||
{{ as_timedelta("PT10M") }}
|
||||
```
|
||||
@ -632,7 +635,7 @@ To fix it, enforce the ISO conversion via `isoformat()`:
|
||||
|
||||
{% raw %}
|
||||
|
||||
```yaml
|
||||
```text
|
||||
{{ 120 | timestamp_local }}
|
||||
```
|
||||
|
||||
@ -846,10 +849,8 @@ Some examples:
|
||||
```
|
||||
This more complex example uses the `contains` filter to match the current month with a list. In this case, it's used to generate a list of light theme to give to the `Input select: Set options` service.
|
||||
|
||||
|
||||
{% endraw %}
|
||||
|
||||
|
||||
### Numeric functions and filters
|
||||
|
||||
Some of these functions can also be used in a [filter](https://jinja.palletsprojects.com/en/latest/templates/#id11). This means they can act as a normal function like this `sqrt(2)`, or as part of a filter like this `2|sqrt`.
|
||||
@ -951,7 +952,7 @@ The other part of templating is processing incoming data. It allows you to modif
|
||||
It depends per integration or platform, but it is common to be able to define a template using the `value_template` configuration key. When a new value arrives, your template will be rendered while having access to the following values on top of the usual Home Assistant extensions:
|
||||
|
||||
| Variable | Description |
|
||||
|--------------|------------------------------------|
|
||||
| ------------ | ---------------------------------- |
|
||||
| `value` | The incoming value. |
|
||||
| `value_json` | The incoming value parsed as JSON. |
|
||||
|
||||
@ -969,7 +970,7 @@ The template for `on` would be:
|
||||
{% raw %}
|
||||
|
||||
```yaml
|
||||
'{{value_json.on}}'
|
||||
"{{value_json.on}}"
|
||||
```
|
||||
|
||||
{% endraw %}
|
||||
@ -1068,7 +1069,7 @@ With given payload:
|
||||
{ "state": "ON", "temperature": 21.902 }
|
||||
```
|
||||
|
||||
Template {% raw %}```{{ value_json.temperature | round(1) }}```{% endraw %} renders to `21.9`.
|
||||
Template {% raw %}`{{ value_json.temperature | round(1) }}`{% endraw %} renders to `21.9`.
|
||||
|
||||
Additional the MQTT entity attributes `entity_id`, `name` and `this` can be used as variables in the template. The `this` attribute refers to the [entity state](/docs/configuration/state_object) of the MQTT item.
|
||||
|
||||
@ -1082,7 +1083,7 @@ For service calls command templates are defined to format the outgoing MQTT payl
|
||||
|
||||
Example command template:
|
||||
|
||||
With given value `21.9` template {% raw %}```{"temperature": {{ value }} }```{% endraw %} renders to:
|
||||
With given value `21.9` template {% raw %}`{"temperature": {{ value }} }`{% endraw %} renders to:
|
||||
|
||||
```json
|
||||
{
|
||||
@ -1106,7 +1107,7 @@ The default priority of operators is that the filter (`|`) has priority over eve
|
||||
|
||||
{% raw %}
|
||||
|
||||
```yaml
|
||||
```text
|
||||
{{ states('sensor.temperature') | float / 10 | round(2) }}
|
||||
```
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user