maxwroc / battery-state-card

Battery state card for Home Assistant
MIT License
875 stars 38 forks source link

Return blank if attribute cannot be evaluated #665

Closed andrew-codechimp closed 8 months ago

andrew-codechimp commented 9 months ago

Is your feature request related to a problem? Please describe. I would like to include numerous entities, some which have an attribute, some which don't but rendering secondary info on those that do not have the attribute just shows the template.

In the below example I want to include both battery_plus, which has an attribute of type & qty, but also standard batteries, which do not.

type: custom:battery-state-card
secondary_info: '{attributes.battery_type_and_quantity}'
round: 0
filter:
  include:
    - name: entity_id
      value: '*_battery_plus'
    - name: entity_id
      value: '*_battery'
  exclude:
    - name: attributes.battery_low
      value: false
bulk_rename:
  - from: "Battery+"      
  - from: "Battery"      
sort:
  - state

Describe the solution you'd like When evaluating the template within the secondary info, if it does not parse as a valid attribute then display blank.

Additional context Also linked to #658 battery_plus will optionally hide the original battery so I'd like those not displayed to avoid duplications.

CleanShot 2024-02-01 at 17 14 13@2x

maxwroc commented 9 months ago

I was thinking about the same thing for a while. But I couldn't decide whether to just display nothing in such cases by default or add a "KString function" which you could use to allow missing values

andrew-codechimp commented 9 months ago

A function with a default value if missing might be useful but either would work for me.

maxwroc commented 8 months ago

This will be a default behavior starting from 3.2.0