Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Cannot read property 'states' of undefined. #337

Closed
dwainscheeren opened this issue Jun 1, 2020 · 15 comments
Closed

Cannot read property 'states' of undefined. #337

dwainscheeren opened this issue Jun 1, 2020 · 15 comments
Labels
bug Something isn't working

Comments

@dwainscheeren
Copy link

Checklist

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

Describe the bug
Sometimes when I enter a page I get the following error button-card.js:1638 Uncaught (in promise) TypeError: Cannot read property 'states' of undefined
It happens on this part: this._stateObj = this._config.entity ? this._hass.states[this._config.entity] : void 0;try { When I then keep that page open for some minutes some of the button cards suddenly starts working and show up (otherwise they are just empty/not visible).

Version of the card
Version: 3.3.5

To Reproduce
It happens in different ways I use button card but an example is:

- type: custom:auto-entities
            filter:
              exclude:
                - entity_id: '*battery_state*'
              include:
                - entity_id: '*battery*'
                  domain: sensor
                  options:
                    type: custom:button-card
                    template: house_data_item
                    entity: this.entity_id
                    icon: >
                      [[[ 
                        if (states[`this.entity_id`].state == 'unknown'){
                          return 'mdi:battery-alert';
                        } else {
                          return 'mdi:battery';
                        }
                      ]]]
                - type: custom:button-card
                  color_type: blank-card
                  aspect_ratio: 1/1

Desktop (please complete the following information):
Latest version chrome

@dwainscheeren dwainscheeren added the bug Something isn't working label Jun 1, 2020
@dwainscheeren
Copy link
Author

dwainscheeren commented Jun 1, 2020

This started happening from HA 0.110

@dwainscheeren
Copy link
Author

@RomRider
Copy link
Collaborator

RomRider commented Jun 2, 2020

Will fix that soon. They've changed something in core which make the hass object undefined while the card is created and it fucks everything up...

@thomasloven
Copy link

thomasloven commented Jun 2, 2020

You can never trust the order setConfig, set hass or attachment to DOM will happen in. Especially not when the GUI editor is involved. Also, setConfig may be called hundreds of times during the lifetime of the card - just a heads up.

@RomRider
Copy link
Collaborator

RomRider commented Jun 2, 2020

I understand, however it used to be always the case in the past and the latest release broke a lot of of cards because of that. So I see that as a breaking (big) change :)

@thomasloven
Copy link

Well, it used to be close enough to always... but i agree, it's an unfortunate change that could have been communicated more clearly.

@RomRider
Copy link
Collaborator

RomRider commented Jun 2, 2020

I'm just wondering something then... How do you tackle the attachement to DOM when you need hass to be available in the connectedCallback and it is not?
I really hate any solution involving setTimeout to retry later... It feels like this would require quite some Promise.then all over the code...

@thomasloven
Copy link

If it's really necessary and can't be fixed by restructuring, a flag could help.

initial_setup() {
  if(this.hass === undefined) return;
  if(this._config === undefined) return;
  if(!this.isConnected) return;
  this.initial_setup_complete = true;
  ... do setup stuff ...
}

setConfig(config) {
  ... do stuff ...
  this._config = config;
  if(this.initial_setup_complete === undefined) {
    this.initial_setup();
  }
}

set hass(hass) {
  ... do stuff ...
  if(this.initial_setup_complete === undefined) {
    this.initial_setup();
  }
}

connectedCallback() {
  ... you get the point ...
}

@dwainscheeren
Copy link
Author

Any news on this @RomRider :)

@RomRider
Copy link
Collaborator

RomRider commented Jun 5, 2020

Will work on it over the weekend, didn't have time lately, sorry :/

@parautenbach
Copy link

Here is another case to test or help reproduce this. I get the reported error using a timer:

          - type: horizontal-stack
            cards:
              - type: custom:button-card
                entity: timer.lhs_garage_door_timer

@RomRider
Copy link
Collaborator

RomRider commented Jun 7, 2020

@dwainscheeren, please give 3.3.6 a go an report back :)

@RomRider
Copy link
Collaborator

RomRider commented Jun 7, 2020

@parautenbach, the timer issue should also be fixed btw ;)

@dwainscheeren
Copy link
Author

Tested it for some minutes, until now no errors. So seems to be fixed. :) I report back here if not. Thanks!

@parautenbach
Copy link

Thanks a mil for the quick release! I downloaded and installed the update and haven't seen the issue. I'll let you know should it appear again.

@shauder shauder mentioned this issue Jun 9, 2020
2 tasks
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants