thomasloven / lovelace-auto-entities

🔹Automatically populate the entities-list of lovelace cards
MIT License
1.2k stars 111 forks source link

Can't use custom card-type when using filter template? #315

Closed j-loewen closed 1 year ago

j-loewen commented 1 year ago

Hello,

I need to use a filter template and a custom card type. Is this possible? Everything I try doesn't work.

type: custom:auto-entities
card:
  type: grid
  columns: 1
  square: false
card_param: cards
filter:
  template: |-
    {% for calendar in states.calendar %}
      {% if ((as_timestamp(state_attr(calendar.entity_id, "start_time")) - as_timestamp(now())) / 60 / 60 / 24) | round(0, "ceil") <= 7 %}
        {{ calendar.entity_id }},
      {% endif %}
    {% endfor %}
  options:
    type: custom:mushroom-template-card
    icon: '{{ state_attr(entity, "icon") }}'
    primary: |-
      {% if state_attr(entity, "next_holiday") is none -%}
        {{ state_attr(entity, "friendly_name") }}
      {%- else -%}
        {{ state_attr(entity, "next_holiday") }}
      {%- endif %}
    secondary: >-
      {% if as_timestamp(state_attr(entity, "start_time")) |
      timestamp_custom("%Y-%m-%d") == as_timestamp(now()) |
      timestamp_custom("%Y-%m-%d") -%}
        Today
      {% else -%}
        In {{ ((as_timestamp(state_attr(entity, "start_time")) - as_timestamp(now())) / 60 / 60 / 24) | round(0, "ceil") }} 
        {% if ((as_timestamp(state_attr(entity, "start_time")) - as_timestamp(now())) / 60 / 60 / 24) | round(0, "ceil") != 1 -%}Days{% else -%}Day{%- endif %}
      {%- endif %}
    tap_action:
      action: none
    hold_action:
      action: none
    double_tap_action:
      action: none
  exclude: []
sort:
  method: attribute
  reverse: false
  attribute: start_time
show_empty: false

What am I doing wrong?

ScottG489 commented 1 year ago

So I'm looking to do exactly this and I was about to point out how it wasn't possible, but then I noticed this in the docs:

The filter section template takes a jinja2 template which evaluates to a list of entities or entity objects.

"entity objects" being the key word here.

And I think I have a proof of concept to show how this works. Here's a relevant excerpt:

...
filter:
  template: |
    {{
      [
        {
          "type": "custom:mushroom-light-card",
          "entity": "light.kitchen_lights"
        }
      ]
    }}
...

So you'd want to make a template that generates not a list of entities, but entity objects that have all the properties you want. Which in your case is quite complicated, but should be doable.

Also for future reference, I wanted to point out you've put the options map in the wrong location. You've nested it under filters, but it's meant to be a map nested under a filter definition:

...
filter:
  include:
    - domain: light
      options:
        type: custom:mushroom-light-card
...

Hope this helps!