custom-cards / decluttering-card

🧹 Declutter your lovelace configuration with the help of this card
MIT License
369 stars 30 forks source link

Help to pass input_select helper to decluttering template #54

Open fergalish opened 1 year ago

fergalish commented 1 year ago

Hi, I have decluttering templates working quite well, including nested ones. However, I've run into a block. I have several rooms, each with a binary_sensor.[[roomname]]_window entity. I pass the roomname as text when I call the template entity, and it all works great. However, I'd also like to have a separate page where the [[roomname]] is set by input_select.room_selector with the various room names listed. So I have this:

decluttering_templates:
  test_decluttering_template:
    card:
      type: entities
      title: '[[title]]'
      entities:
        - type: custom:template-entity-row
          state: '[[roomname]]'
          name: roomname_variable
        - type: custom:template-entity-row
          state: '{{ states("binary_sensor.[[roomname]]_window") }}'
          name: Window state

And the cards:

    cards:
      - type: custom:decluttering-card
        template: test_decluttering_template
        variables:
          - title: Passing the value of the input_select
          - roomname: '{{ states.input_select.room_selector.state }}'
      - type: custom:decluttering-card
        template: test_decluttering_template
        variables:
          - title: passing_direct_string (\"kitchen\")
          - roomname: kitchen

I've tried all sorts of variations, using custom:template-entity-row or directly using - entity: 'binary_sensor.[[roomname]]_window' and so on. I've tried using the '{{states("input_select.room_selector")}}' function instead of states.input_select.room_selector.state and so on.

Nothing is working for me. Some fail with "Unknown" in the UI, others don't render the card with various types of error. Happy to supply those errors if anyone thinks it might help.

Has anyone every seen this before and have an idea on how to fix please?

ghost commented 1 year ago

I had a similar issue and gave up. I don't think decluttering-card can handle templates either being passed as variables or being used in the template itself.

ildar170975 commented 1 year ago

Templates may be passed as variables:

  decl_test_input_name_2:
    card:
      type: entities
      title: '[[title]]'
      entities:
        - type: custom:template-entity-row
          state: '[[roomname]]'
          name: roomname_variable
        - type: custom:template-entity-row
          state: '[[room_light_state]]'
          name: light state
input_select:
  test_area:
    options:
      - kitchen
      - bathroom
      - balcony
type: vertical-stack
cards:
  - type: entities
    entities:
      - input_select.test_area
  - type: custom:decluttering-card
    template: decl_test_input_name_2
    variables:
      - roomname: >-
          {{ states('input_select.test_area') }}
      - room_light_state: >-
          {% set ROOM = states('input_select.test_area') -%}
          {{ states('light.' + ROOM + '_ceiling') }}
      - title: some_title_2
  - type: entities
    entities:
      - light.kitchen_ceiling
      - light.bathroom_ceiling
      - light.balcony_ceiling

image

But templates cannot be used "inside templates"...

RedNo7 commented 11 months ago

you can do it, but it's javascript, not jinja. So:

variables:
      - roomname: >-
          {{ states('input_select.test_area') }}

becomes:

variables:
  - roomname: "[[[ return states['input_select.test_area'].state ]]]"

...I popped in onto a single line, but you can still do it on multiple lines:

variables:
  - roomname: >-
      [[[ return states['input_select.test_area'].state ]]]
ildar170975 commented 11 months ago

you can do it, but it's javascript, not jinja. So:

The original question was "how to pass a template as a variable" - and a possible solution was provided here.

BTW, I was not quite right when said

templates cannot be used "inside templates"...

because it IS possible with using a custom ct_eval() function. Check here, part "jinjia template which is used INSIDE another jinjia template".


it's javascript, not jinja

The original question was about using template-entity-row which dies NOT accept JS.

RedNo7 commented 11 months ago

My attempts to pass templates as variables using jinja have failed in the past and someone on this forum showed me that javascript works.

Edit: Ugh, it’s 10 mins, 1 cup-of-coffee and some checking of my yaml later. I now remember why I am using javascript and not jinja; all my decluttering templates use custom:button-card which only accepts javascript templates.

…sorry for the bum info.