Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: 'options:' doesn't work when I use 'template:' as filter #247

Closed
tj551955 opened this issue Jan 11, 2022 · 18 comments
Closed

FR: 'options:' doesn't work when I use 'template:' as filter #247

tj551955 opened this issue Jan 11, 2022 · 18 comments

Comments

@tj551955
Copy link

I'd like to have the possibility to add 'options:' to the entities list when I filtered them using a template.
For example:

type: custom:auto-entities
card:
  type: entities
  show_header_toggle: false
filter:
  template: |
    {% set temp_map =
     { 'Diesel':'diesel',
       'E10':'e10',
       'Super':'e5' } %}
    {% set state = states('input_select.fuel_type') %}
    {% set sprit = temp_map[state] if state in temp_map %}
    {% for s in states.sensor if 'tankerkoenig' in s.entity_id %}
      {% if (s.attributes.fuel_type == sprit) and not is_state(s.entity_id, 'False') %}
        {{ s.entity_id }}
      {% endif %}
    {% endfor %}
  options:
    name: Test
sort:
  method: state

When I use the syntax with filter: include: ... than options: work fine as expected. But I couldn't find a way to add 'options:' to a template section.

Would be great to have that.

@ASNNetworks
Copy link

@myhomeiot
Copy link

myhomeiot commented Jan 17, 2022

Check out this post, seems to work: https://community.home-assistant.io/t/auto-entities-automatically-fill-cards-with-entities/147801/511?u=asnnetworks

Unfortunately not for custom:multiple-entity-row :-(

Edit: I need it for something like this:

type: custom:auto-entities
show_empty: false
card:
  title: History
  type: entities
  show_header_toggle: false
filter:
  template: '{{ state_attr(''binary_sensor.history'', ''entity_ids'') }}'
  options:
    type: custom:multiple-entity-row
    secondary_info: last-changed
    state_color: false
sort:
  method: last_changed
  reverse: true
  count: 15

@ildar170975
Copy link

  1. The multiple-entity-row was not initially mentioned in the issue (1st post).

  2. The provided solution covers a case from the 1st post.

  3. The provided solution may be (or may be not) applied for multiple-entity-row as well. It depends on what you need. I advise you to ask your question in the Community thread before registering an issue on GitHub.

@myhomeiot
Copy link

  1. The provided solution may be (or may be not) applied for multiple-entity-row as well. It depends on what you need. I advise you to ask your question in the Community thread before registering an issue on GitHub.

As you can see I doesn't register this issue. I try suggested solution and report result with my config.

@beauwest
Copy link

It would be really nice to be able to use options: with template:. I think that the workaround above would work in my current scenario, but it'd just be simpler if the existing feature worked.

@ildar170975
Copy link

ildar170975 commented Jul 27, 2022

use options: with template:

I am not a creator of this great auto-entities card.
But I guess that:

  1. The auto-entities card creates a list of objects.
  2. Each object is not just an entity_id - it is a dictionary like ['xxx': '123', 'yyy': 'abc', 'zzz': '123 ghj 567'] (and some element of the dictionary may be a list or another dictionary).
  3. Then the generated list of objects is passed into some card as "entities", "cards" or whatever is specified as card_param.
  4. As a result - the Entities card gets not only a list of entity_id but a list of "entity_id: xxx, state_color: xxx, name: xxxx".

Now you are asking about "use options along with a template".
How do these things work together?
The template may already have different options specified.

Moreover, the template may create TWO or more absolutely different types of objects - suppose, the generated list contains 10 pairs "entity-row + history-graph". Then how options are supposed to be applied to this PAIR of DIFFERENT objects?

@beauwest
Copy link

Fair points.

I would expect that for template:, if options: was set, it would apply to all entity objects returned by the template.

That would behave consistently with how options: works with non-template includes, as well as matching the documentation.

options: Map of options to apply to entity when passed to card.

My understanding, is that the list result from a template, still gets passed to the entities property (by default) of the card specified by card:.

If you have multiple types of objects returned by a template, then absolutely you'd have to take manual control if you didn't want the options: applied to all of them.

@beauwest
Copy link

I actually may be misunderstanding a piece of how template works. I don't think that it passes to the entities property of the card:. I think that it just renders the objects returned by the template normally, and any include: stuff would be added to the card.

@ildar170975
Copy link

Imho using any options in any combinations should be at least harmless.
There are cases when applying some options for a template-generated objects may not be good.
Since using options along with template must be with some caution - imho we should not allow users to combine these options due to potential risks.

@ildar170975
Copy link

ildar170975 commented Jul 27, 2022

and any include: stuff would be added to the card.

I am not following you. There are 2 different ways to filter:

  • use include/exclude
  • use template

They cannot be combined. They may only be used to create a large list - part of it is created by template, other part is created by include/exclude.

@beauwest
Copy link

You're right, I did some more digging. I was thinking that template was handled differently when it was rendered, but I'm seeing that it's not.

Then yes, my vote would still be to make options: behave exactly like the documentation says it does. Even when using a template: to filter.

@beauwest
Copy link

beauwest commented Jul 27, 2022

This is my current

filter:
  template: >-
    {% set matcher = 'input_boolean.chore_[0-9]+_.*' ~ (now().strftime('%a') | lower) -%}
    {%- for bool in states.input_boolean -%}
      {%- if bool.entity_id is match(matcher) -%}
        {{
          {
            'entity': bool.entity_id,
            'tap_action': {
              'action': 'toggle'
            }
          }
        }},
      {%- endif -%}
    {%- endfor %}

This would be much nicer, in my opinion. But not necessary.

filter:
  options:
    tap_action:
      action: 'toggle'
  template: >-
    {% set matcher = 'input_boolean.chore_[0-9]+_.*' ~ (now().strftime('%a') | lower) -%}
    {%- for bool in states.input_boolean -%}
      {%- if bool.entity_id is match(matcher) -%}
        {{ bool.entity_id }},
      {%- endif -%}
    {%- endfor %}

@stefanschaedeli
Copy link

This is my current

filter:
  template: >-
    {% set matcher = 'input_boolean.chore_[0-9]+_.*' ~ (now().strftime('%a') | lower) -%}
    {%- for bool in states.input_boolean -%}
      {%- if bool.entity_id is match(matcher) -%}
        {{
          {
            'entity': bool.entity_id,
            'tap_action': {
              'action': 'toggle'
            }
          }
        }},
      {%- endif -%}
    {%- endfor %}

This would be much nicer, in my opinion. But not necessary.

filter:
  options:
    tap_action:
      action: 'toggle'
  template: >-
    {% set matcher = 'input_boolean.chore_[0-9]+_.*' ~ (now().strftime('%a') | lower) -%}
    {%- for bool in states.input_boolean -%}
      {%- if bool.entity_id is match(matcher) -%}
        {{ bool.entity_id }},
      {%- endif -%}
    {%- endfor %}

fully agree with this, I have exactly the same case...

@bcutter
Copy link

bcutter commented Sep 10, 2022

Copy that. I just want to show a secondary information for all my listed entities filtered by a template. Currently IMPOSSIBLE without going way deeper into templating which adds a lot of unneeded complexity. Would really prefer to see auto-entities allowing options for template filter too.

type: custom:auto-entities
card:
  type: entities
  title: Beleuchtung Test
filter:
  template: >-
    {{ states.light | selectattr('attributes.entity_id', 'defined') |
    selectattr('state','eq','off') | map(attribute='entity_id') | join(',') }}
  options:
    secondary_info: last-changed

Here the options is just ignored / not applied... 😞

@thomasloven
Copy link
Owner

If you're using the template filter you will have to add all options via the template.

@osnwt
Copy link

osnwt commented Jan 15, 2023

If you're using the template filter you will have to add all options via the template.

Could you please give an example of how to apply these options (working for include filter) to template filter?

It overrides the icon depending on entity_id and its color depending on the state.

      options:
        card_mod:
          style: |
            :host {
              {% set age = as_timestamp(now()) - as_timestamp(states('this.entity_id')) %}
              {% if age > 60*60 %}
                --paper-item-icon-color: red;
              {% elif age > 30*60 %}
                --paper-item-icon-color: #f29c38;
              {% elif age > 10*60 %}
                --paper-item-icon-color: #67ac5b;
              {% endif %}
              --card-mod-icon: {{ 'this.entity_id'.endswith('ble') | iif('mdi:bluetooth', 'mdi:zigbee') }};
            }

I guess it should be added to entity's attributes, but have no idea how.

@thomasloven
Copy link
Owner

@osnwt
Copy link

osnwt commented Jan 16, 2023

Excellent, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants