iantrich / config-template-card

📝 Templatable Lovelace Configurations
MIT License
443 stars 57 forks source link

Templated entities example is not working for me #139

Closed danielbrunt57 closed 9 months ago

danielbrunt57 commented 9 months ago

Checklist:

Release with the issue: 1.3.6

Last working release (if known): unknown

Browser and Operating System: Current versions of Windows 11 & Microsoft Edge

Description of problem: Example in README is not working:

type: 'custom:config-template-card'
variables:
  - states['sensor.light'].state
entities:
  - '${vars[0]}'
card:
  type: light
  entity: '${vars[0]}'
  name: "${states[vars[0]].state === 'on' ? 'Light On' : 'Light Off'}"

Additional information: I find I need to define a string variable to pass as the entity to the entities card. If I use the example as stated, the entities card complains that it can't find the entity 'closed':

image

The following variation of the example works but I would like to only have to specify the entity once for the entire code segment.

- type: custom:config-template-card
  variables:
    - '"cover.kitchen_blind_2"'
    - states['cover.kitchen_blind_2'].state
  entities:
    - '${vars[1]}'
  card: 
    type: entities
    entities:
      - entity: '${vars[0]}'
        type: custom:multiple-entity-row
        name: Kitchen 2
        show_state: false
        icon: "${vars[1] === 'closed' ? 'phu:blind-tilt-open' : 'phu:blind-tilt-closed'}"
        entities:
          - entity: sensor.kitchen_blind_2_battery_level
            name: Battery
          - entity: sensor.kitchen_blind_2_battery_level
            name: false
            icon: true

The above is part of a vertical stack of cards:

  - type: vertical-stack
    cards:
      - type: custom:mushroom-title-card
        title: ''
        subtitle: '2'
        alignment: center
      - type: custom:config-template-card
        variables:
          - '"cover.kitchen_blind_2"'
          - states['cover.kitchen_blind_2'].state
        entities:
          - '${vars[1]}'
        card: 
          type: entities
          entities:
            - entity: '${vars[0]}'
              type: custom:multiple-entity-row
              name: Kitchen 2
              show_state: false
              icon: "${vars[1] === 'closed' ? 'phu:blind-tilt-open' : 'phu:blind-tilt-closed'}"
              entities:
                - entity: sensor.kitchen_blind_2_battery_level
                  name: Battery
                - entity: sensor.kitchen_blind_2_battery_level
                  name: false
                  icon: true
          show_header_toggle: false
          state_color: false
          card_mod:
                class: inline-card
      - type: custom:slider-button-card
        slider:
          direction: bottom-top
          background: gradient
          attribute: tilt
          use_state_color: true
          use_percentage_bg_opacity: true
          toggle_on_click: false
          show_track: true
          force_square: false
          invert: true
        action_button:
          mode: toggle
          show: false
        icon:
          icon: ''
          show: false
          use_state_color: false
        entity: cover.kitchen_blind_2
        show_state: true
        compact: false
        name: Kitchen 2
        show_name: false
      - type: custom:slider-button-card
        slider:
          direction: left-right
          background: gradient
          use_state_color: false
          use_percentage_bg_opacity: false
          toggle_on_click: false
          show_track: false
          force_square: false
          invert: false
        icon:
          show: false
        entity: input_number.kitchen_blind_2
        show_state: true
        compact: true
        show_name: true
        name: 'Preset:'
        action_button:
          show: false
danielbrunt57 commented 9 months ago

This variation in my code also works but is NOT the same as the README example:

- type: custom:config-template-card
  variables:
    - '"cover.kitchen_blind_2"'
    - '"sensor.kitchen_blind_2_battery_level"'
  entities:
    - '${vars[0]}'
  card: 
    type: entities
    entities:
      - entity: '${vars[0]}'
        type: custom:multiple-entity-row
        name: Kitchen 2
        show_state: false
        icon: "${states[vars[0]].state === 'closed' ? 'phu:blind-tilt-open' : 'phu:blind-tilt-closed'}"
        entities:
          - entity: '${vars[1]}'
            name: Battery
          - entity: '${vars[1]}'
            name: false
            icon: true
ildar170975 commented 9 months ago

Typo in readme. Should be

type: 'custom:config-template-card'
variables:
  - states['sensor.light']
entities:
  - '${vars[0].entity_id}'
card:
  type: light
  entity: '${vars[0].entity_id}'
  name: "${vars[0].state === 'on' ? 'Light On' : 'Light Off'}"

Revise your code based on that & close the issue if resolved. ~The wrong code in Readme will be fixed.~ Done


This is not correct:

  variables:
    - '"cover.kitchen_blind_2"'
    - '"sensor.kitchen_blind_2_battery_level"'
  entities:
    - '${vars[0]}'

You got vars depending on TWO entities & going to monitor only one of them. No - you should monitor both of them, otherwise the card will not be updated if the missed entity is changed.

danielbrunt57 commented 9 months ago

Thank you! This is working:

type: custom:config-template-card
variables:
  - states['cover.kitchen_blind_1']
  - states['sensor.kitchen_blind_1_battery_level']
entities:
  - ${vars[0].entity_id}
  - ${vars[1].entity_id}
card:
  type: entities
  entities:
    - entity: ${vars[0].entity_id}
      type: custom:multiple-entity-row
      name: Kitchen 1
      show_state: false
      icon: >-
        ${vars[0].state === 'open' ? 'phu:blind-tilt-open' :
        'phu:blind-tilt-closed'}
      entities:
        - entity: ${vars[1].entity_id}
          name: Battery
        - entity: ${vars[1].entity_id}
          name: false
          icon: true
  show_header_toggle: false
  state_color: false
ildar170975 commented 9 months ago

Good.

But: CTC (config-tamplate-card) here is only used to make an icon dynamically set. In this particular case using CTC is a waste of resources. Consider other ways, like:

danielbrunt57 commented 9 months ago

I first tried setting '--card-mod-icon' with card-mod but the code I came up with changed the state badge for all entities within the multiple-entity-row whereas I only want to change the primary entity icon.

After quite a bit of digging, I came up with a solution:

type: entities
entities:
  - entity: cover.kitchen_blind_1
    type: custom:multiple-entity-row
    name: ' '
    show_state: false
    card_mod:
      style:
        hui-generic-entity-row:
          $: |
            state-badge {
              --card-mod-icon: {{ iif(states("cover.kitchen_blind_1") == "open", "phu:blind-tilt-open", "phu:blind-tilt-closed") }}
            }
    entities:
      - entity: sensor.kitchen_blind_1_battery_level
        name: Battery
      - entity: sensor.kitchen_blind_1_battery_level
        name: false
        icon: true
show_header_toggle: false
state_color: false

image

ildar170975 commented 9 months ago

The card-mod code seems to be OK.

danielbrunt57 commented 8 months ago

Yes, once I figured out how to target just the main entity! With just:

card_mod:
  style:
    --card-mod-icon: {{ iif(states("cover.kitchen_blind_1") == "open", "phu:blind-tilt-open", "phu:blind-tilt-closed") }}

the icon for the battery entity was changed as well!

I also implemented the decluttering card so now I only have one template I use for all 6 blinds.

ildar170975 commented 8 months ago

The last code you posted is absolutely wrong.