custom-cards / decluttering-card

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

Use of this card with the config-template-card #11

Closed SeLLeRoNe closed 4 years ago

SeLLeRoNe commented 5 years ago

I am trying to use this card to cleanup my code, but I am facing an issue with the "variables" configuration inside the config-template-card

This is my code:

---
temperature:
  card:
    type: custom:config-template-card
    entities:
      - [[entity]]
      - input_number.night_time
    variables:
      - states['sensor.[[entity]]'].state
    card:
      type: custom:mini-graph-card
      name: Temperature
      icon: "${vars[0] > 30 ? 'fas:thermometer-full' : vars[0] > 25 ? 'fas:thermometer-three-quarters' : vars[0] > 20 ? 'fas:thermometer-half' : vars[0] > 10 ? 'fas:thermometer-quarter' : 'fas:thermometer-empty' }"
      more_info: false
      height: 150
      line_width: 4
      hours_to_show: 168
      points_per_hour: 0.25
      aggregate_func: max
      group_by: hour
      group: false
      hour24: true
      animate: false
      decimals: 1
      font_size: 100
      font_size_header: 14
      align_header: default
      align_icon: right
      align_state: center
      lower_bound: -5
      upper_bound: 40
      show:
        name: true
        icon: true
        state: true
        graph: line
        fill: true
        points: false
        legend: false
        extrema: false
        labels: false
        labels_secondary: false
        name_adaptive_color: true
        icon_adaptive_color: true
      color_thresholds:
        - value: 5
          color: "#0055FF"
        - value: 15
          color: "#FF6600"
        - value: 20
          color: "#FFAA00"
        - value: 22.5
          color: "#FF7700"
        - value: 25
          color: "#FF3700"
      style: |
        ha-card {
          padding: 16px !important;
        }
      entities:
        - entity: '[[entity]]'
          name: Temperature
          color: '#FF5000'
          show_fill: false
        - entity: input_number.night_time
          color: '#B3B3B3'
          name: Night Time
          show_fill: true
          show_line: false
          show_points: false
          show_legend: false
          y_axis: secondary

The problem seems to be caused by this:

    variables:
      - states['sensor.[[entity]]'].state

The decluttering card doesn't seems to replace the [[entity]] because wrapped around other []

The error I receive in the console is:

VM577:1 Uncaught (in promise) TypeError: Cannot read property 'state' of undefined
    at eval (eval at _evaluateTemplate (config-template-card.js:3305), <anonymous>:1:29)
    at HTMLElement._evaluateTemplate (config-template-card.js:3305)
    at config-template-card.js:3276
    at Array.forEach (<anonymous>)
    at HTMLElement._evaluateConfig (config-template-card.js:3265)
    at HTMLElement.render (config-template-card.js:3256)
    at HTMLElement.update (config-template-card.js:2827)
    at HTMLElement.performUpdate (config-template-card.js:2285)
    at HTMLElement._enqueueUpdate (config-template-card.js:2237)

Please note, if I use actual entities values instead of [[entity]] (ex. sensor.living_room_temperature) it does work as expected.

Thanks

RomRider commented 4 years ago

I think the problem is here:

temperature:
  card:
    type: custom:config-template-card
    entities:
      - [[entity]]  # <<<<---- 

You're missing quotes.

temperature:
  card:
    type: custom:config-template-card
    entities:
      - '[[entity]]'
SeLLeRoNe commented 4 years ago

No, that's the entity definition and it is working fine, the problem is here:

    variables:
      - states['sensor.[[entity]]'].state

That part generates a vars[0] that I then use here:

      icon: "${vars[0] > 30 ? 'fas:thermometer-full' : vars[0] > 25 ? 'fas:thermometer-three-quarters' : vars[0] > 20 ? 'fas:thermometer-half' : vars[0] > 10 ? 'fas:thermometer-quarter' : 'fas:thermometer-empty' }"

Surely there is something wrong with the "[" and "]", I tried using "(" and ")" but doesn't help states['sensor.[[entity]]'].state

RomRider commented 4 years ago

Still, what I pointed out is valid. This - [[xxx]] without the quotes means, in yaml:

-
  -
    - xxx

I'm going to check the other part.

SeLLeRoNe commented 4 years ago

Yes I know but that part it is actually working because even if I set an entity there without quotes works ;)

RomRider commented 4 years ago

How do you instantiate the card? Can I see the config please?

SeLLeRoNe commented 4 years ago

Sure, here it is:

      - type: custom:decluttering-card
        template: temperature
        variables:
          - entity: sensor.studio_andrea_temperature
RomRider commented 4 years ago

Well the problem is that: states['sensor.[[entity]]'].state results in states['sensor.sensor.stufio_andrea_temperature'].state

Note the double sensor.sensor

SeLLeRoNe commented 4 years ago

Oh wow.. and I am sure I did test that....

Thanks for spotting it!