button-card icon indicating copy to clipboard operation
button-card copied to clipboard

Handling of undefined entities

Open MEKadan opened this issue 3 years ago • 4 comments

Checklist

  • [ x] I updated the card to the latest version available
  • [ x] I cleared the cache of my browser

Describe the bug In the following strucure (from samples, entity name modified):

image

...the "default" operator is never launched in case of persistent notifications. A persistent notification is created when conditions are met and as an entity it displays the state "notifying". If needed to e.g. draw a button with different color/ text when it is NOT launched, it does not take the default route, probably, because it does not exist and thus not having any 'state'

Version of the card Version: 3.4.2

To Reproduce This is the configuration I used:


Screenshots If applicable, add screenshots to help explain your problem.

Expected behavior The default route would be run also when the entity is not defined/existing.

Desktop (please complete the following information):

  • Browser [various (Firefox 96.0.3, Firefox Developer 98.0b5, Vivaldi 5.1.2567.49]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context Add any other context about the problem here.

MEKadan avatar Feb 24 '22 22:02 MEKadan

Instead of using default, make it a template and check if the entity exists. But the issues is that your card won't update automatically because the entity will not exist. Not much I can do about that without changing the whole logic of the card

RomRider avatar Mar 15 '22 19:03 RomRider

I would add a rationale for this issue (or FR) A non-existing entity is right now a valid situation in HA. It's because HA provides an option to temporarily disable integration, causing its entities to disappear. For example, one might disable Christmas tree strings (Twinkly integration). Probably in this case disconnecting the device and leaving its entity unavailable is the better choice (for the custom button card). But it doesn't solve other potential use-cases.

Recently I'm facing the same situation: the "default" decision branch is not chosen if no entity exists at all. I would expect the opposite. But the fair point is (which I didn't know), that reappearing the of the entity would not refresh the card. In this context relying on this feature makes no sense.

michalk-k avatar May 25 '22 11:05 michalk-k

recently we've been experimenting with non existing entities, and things like:

    - type: custom:button-card
      name: Test unavailable color
      entity: input_boolean.test
      styles:
        card:
          - color: |
              [[[
                return (!entity) ? 'red' : 'auto';
              ]]]

are possible, even

    - type: custom:button-card
      name: Test unavailable color
      entity: input_boolean.test
      color: |
        [[[ return (!entity) ? 'red' : 'auto'; ]]]

taking it 1 step further, using an 'exist' template:

exist:
  styles:
    card:
      - display: >
          [[[ if (!entity) return 'none'; ]]]

not sure if this helps, just thought id share.

test by changing the entity to anything non-existing ;-) put_boolean.test

Mariusthvdb avatar May 25 '22 12:05 Mariusthvdb

It helps unless the card of interest is not embedded into another custom button card. Otherwise, the entity object refers to most top button card, and thus cannot be used here.

There is also a serious drawback of using JS templates to return values for CSS styles. There is no way to 'DO NOTHING' if no condition is met. In the case of your example (rewritten below) one would expect to render the color green. But no - color will be reset to value inherited from CSS styles which renders it impractical when using card templates and inheriting CSS values from them. Using state: is free of this limitation.

button_card_templates:
  tpl_test:
    styles:
      card:
        - color: green
views:
  - title: Home
    cards:
      - type: custom:button-card
        template: tpl_test
        entity: light.pc_room
        styles:
          card:
            - color: |
                [[[
                  return (false) ? 'red' : 'auto';
                ]]]

michalk-k avatar May 25 '22 12:05 michalk-k