Skip to content

Commit

Permalink
Add template support
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasloven committed Jan 9, 2020
1 parent f4d1b54 commit 906869a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 13 deletions.
53 changes: 49 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ entities:
- <entity>
- <entity>
filter:
template: <template>
include:
- <filter>
- <filter>
Expand All @@ -42,14 +43,15 @@ sort: <sort_method>
- `card:` **Required.** The card to display. Specify this as you would specify any normal lovelace card, but ommit the `entities:` parameter.
- `entities:` Any entities added here will be added to the card before any filters are applied.
- `filter:`
- `include:` **Required.** A list of filters specifying which entities to add to the card
- `template:` A jinja2 template evaluating to a whitespace- or comma-separated list of entity ids to include
- `include:` A list of filters specifying which entities to add to the card
- `exclude:` A list of filters specifying which entities to remove from the card
- `show_empty:` Whether to display the card if it has no entities. Default: `true`.
- `unique:` Whether to remove duplicate values after filtering and sorting. Default: `false`.
- `sort:` How to sort the entities of the card. Default: `none`. See [Sorting entities for details](#sorting-entities)

### Filters
The two filter sections `include` and `exclude` each takes a list of filters.
The two main filter sections `include` and `exclude` each takes a list of filters.

Filters have the following options, and will match any entity fulfilling **ALL** options:

Expand All @@ -70,11 +72,17 @@ Special options:
- `not:` Specifies a filter that entities must *not* match.
- `sort:` Specifies a method to sort entities matched by *this filter only*.

### Template filter
The filter section `template` takes a jinja2 template which evaluates to a list of comma- or whitespace separated `entity_id`s which are included.

> Note: Due to how the templating engine of Home Assistant works, this may or may not be as useful as it sounds. See note at templating example below.

## How it works
`auto-entities` creates a list of entities by:
1. Including every entitiy given in `entities:` (this allow nesting of `auto-entities`if you'd want to do that for some reason...)
2. Include all entities that matches **ALL** options of **ANY** filter in the `filter.include` section. The same entity may be included several times by different filters.
3. Remove all entities that matches **ALL** options on **ANY** filter in the `filter.exclude` section.
2. Include every entity listed in a `filter.template` evaluation
3. Include all entities that matches **ALL** options of **ANY** filter in the `filter.include` section. The same entity may be included several times by different filters.
4. Remove all entities that matches **ALL** options on **ANY** filter in the `filter.exclude` section.

It then creates a card based on the configuration given in `card:`, and fills in `entities:` of that card with the entities from above.

Expand Down Expand Up @@ -296,5 +304,42 @@ filter:
entity_id: this.entity_id
```

Example using templates:
```yaml
type: custom:auto-entities
card:
type: entities
filter:
template: |
{% for light in states.light %}
{% if light.state == "on" %}
{{ light.entity_id}},
{% endif %}
{% endfor %}
```
> Note: templates won't update automatically on state changes unless they contain the literal entity id of the entity whose state changes.
> I.e. the example above will not update when a light is turned on or off, unless the view is reloaded.
>
> This is a limitation of the Home Assistant template engine, and nothing I can do anything about. There are, however two mitigations you could make.
>
> One is to redefine the template, e.g.:
> ```yaml
> template: |
> {%if is_state('light.bed_light','on')%}light.bed_light{%endif%}
> {%if is_state('light.kitchen_lights','on')%}light.kitchen_lights{%endif%}
> {%if is_state('light.ceiling_lights','on')%}light.ceiling_lights{%endif%}
> ```
>
> The other option is to add a list of entities to monitor:
> ```yaml
> filter:
> template: |
> ...etc...
> entity_ids:
> - light.bed_light
> - light.kitchen_lights
> - light.ceiling_lights
> ```

---
<a href="https://www.buymeacoffee.com/uqD6KHCdJ" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/white_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;" ></a>
Loading

0 comments on commit 906869a

Please sign in to comment.