iantrich / config-template-card

📝 Templatable Lovelace Configurations
MIT License
425 stars 55 forks source link

Can't get values from attributes #127

Closed luixal closed 1 year ago

luixal commented 1 year ago

Checklist:

Release with the issue: 1.3.6

Last working release (if known):

Browser and Operating System: Chrome 112.0.5615.165 running on EndevourOS (Arch Linux)

Description of problem: I'm trying to use config-template-card with mushroom cards and everythings works fine as long as I use values in the state object at a root level, when I try to get values from attributes it seems that a null value is passed.

This example works and happily renders sensor's state:

  - type: custom:config-template-card
    variables:
      VALUE: states['binary_sensor.irrigation_unlimited_c1_m'].state
    entities:
      - binary_sensor.irrigation_unlimited_c1_m
    card:
      type: custom:mushroom-template-card
      entity: binary_sensor.irrigation_unlimited_c1_m
      primary: "${ VALUE }"
      icon: mdi:water
      icon_color: blue

If I try to show the value from an attribute, like this:

  - type: custom:config-template-card
    variables:
      VALUE: states['binary_sensor.irrigation_unlimited_c1_m'].attributes.enabled
    entities:
      - binary_sensor.irrigation_unlimited_c1_m
    card:
      type: custom:mushroom-template-card
      entity: binary_sensor.irrigation_unlimited_c1_m
      primary: "${ VALUE }"
      icon: mdi:water
      icon_color: blue

It fails and nothing is rendered.

Javascript errors shown in the web inspector (if applicable): The error I get in browers console is related to the mushroom card:

image

and I guess it is produced because the mushroom card expects that value to not be null.

ildar170975 commented 1 year ago

Replace the mushroom with a conventional card, your custom sensor with a standard "sun.sun" - and see if it works:

изображение

изображение

So, probably not related to CTC card. Check your mushroom (what values may be accepted by that option), check your sensor's attribute.

luixal commented 1 year ago

Got it!

I posted this issue here because following the errors and making tests with the mushroom card it felt like the mushroom card wasn't getting a valid value. And THAT was the problem, but not really a bug in config-template-card.

The entity's attribute has a boolean type (true/false) and the mushroom card expected a string in the primary field. Just passing the boolean as string did the trick:

type: custom:config-template-card
variables:
  VALUE: states['binary_sensor.irrigation_unlimited_c1_m'].attributes.enabled
entities:
  - binary_sensor.irrigation_unlimited_c1_m
card:
  type: custom:mushroom-template-card
  entity: binary_sensor.irrigation_unlimited_c1_m
  primary: "${ VALUE.toString() }" // <--- NOTICE THIS
  icon: mdi:water
  icon_color: blue

if anyone gets here and wants to make it a little bit nice, you can use this:

primary: "${ VALUE ? 'Enabled': 'Disabled' }" // <--- NOTICE THIS

Cheers!