Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

button card templates fail to load randomly #718

Closed
andyblac opened this issue Jul 18, 2023 · 17 comments
Closed

button card templates fail to load randomly #718

andyblac opened this issue Jul 18, 2023 · 17 comments
Labels

Comments

@andyblac
Copy link

andyblac commented Jul 18, 2023

Checklist

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

Describe the bug
I get the following at random times, it takes several refreshes or HA restart to fix the error

It only shows on button templates that are not in use, in the above screen shot there is 5 sensor butttons, 2 in use, 3 not, the error only shows only show on ones not in use.

the browser console shows the following.

[Error] anonymous@
_evalTemplate@https://homeassistant.local:8123/hacsfiles/button-card/button-card.js:425:9393
@https://homeassistant.local:8123/hacsfiles/button-card/button-card.js:425:12392
forEach@[native code]
_buildStyleGeneric@https://homeassistant.local:8123/hacsfiles/button-card/button-card.js:425:12357
_getIconHtml@https://homeassistant.local:8123/hacsfiles/button-card/button-card.js:519:161
_gridHtml@https://homeassistant.local:8123/hacsfiles/button-card/button-card.js:499:544
_cardHtml@https://homeassistant.local:8123/hacsfiles/button-card/button-card.js:479:32
render@https://homeassistant.local:8123/hacsfiles/button-card/button-card.js:425:6933
update@https://homeassistant.local:8123/hacsfiles/button-card/button-card.js:1:19879
performUpdate@https://homeassistant.local:8123/hacsfiles/button-card/button-card.js:1:16767
@https://homeassistant.local:8123/hacsfiles/button-card/button-card.js:1:16426
	render (button-card.js:425:6967)
	update (button-card.js:1:19880)
	performUpdate (button-card.js:1:16768)
	(anonymous function) (button-card.js:1:16427)
[Error] Unhandled Promise Rejection: AbortError: The operation was aborted.
[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (light-entity-card.js.map, line 0)

light-entity-card v6.1.0 is installed

Version of the card
Version: 3.5.0

To Reproduce
This is the configuration I used:

  styles:
    custom_fields:
      s1: &card_andyblac_room_sensor_1_icon_styling
        - position: "absolute"
        - left: "4%"
        - bottom: "44%"
        - width: "14%"
        - height: "14%"

  custom_fields:
    s1:
      card:
        type: "custom:button-card"
        template: >
          [[[
            let templates = [ 'card_andyblac_room_sensor_1_icon' ];
            if (variables?.sensor_1?.templates?.length) {
              templates.push(...variables.sensor_1.templates);
            }
            return templates;
          ]]]
        variables: "[[[ return variables?.sensor_1; ]]]"
        state:
          - operator: "template"
            value: "[[[ return !variables.sensor_1; ]]]"
            styles:
              card:
                - display: "none"
        entity: "[[[ return variables?.sensor_1?.entity_id; ]]]"

card_andyblac_room_sensor_1_icon:
  tap_action:
    action: "more-info"
  size: "15px"
  show_icon: true
  show_name: false
  styles:
    icon:
      - width: "90%"
      - height: "90%"
      - line-height: "0"
      - color: "[[[ return variables.ulm_card_room_sensor_color ? 'rgba(var(--color-'+variables.ulm_card_room_sensor_color+'),0.4)' : 'rgba(var(--color-theme),0.2)'; ]]]"
      # - color: "rgba(var(--color-theme),0.2)"
    card:
      - background: "none"
      - box-shadow: "none"
      - padding: "0px"
  state:
    - styles:
        icon:
          - color: "[[[ return variables.ulm_card_room_sensor_color ? 'rgba(var(--color-'+variables.ulm_card_room_sensor_color+'),1)' : 'rgba(var(--color-theme),0.7)'; ]]]"
          # - color: "rgba(var(--color-theme),0.7)"
      id:  "on"
      value: "[[[ return (entity?.state == '0' || entity?.state == 'off' || entity?.state == 'closed' || entity?.state == 'disarmed') ? '' : entity?.state; ]]]"

Screenshots
Screenshot 2023-07-18 at 15 05 55
Screenshot 2023-07-18 at 15 23 53

Expected behavior
where the error are on scren it should just a empty space, when the sensor button is not in use, like the 2nd screenshot

Desktop (please complete the following information):

  • Browser safari]
  • Version 16.5.2
@andyblac andyblac added the bug Something isn't working label Jul 18, 2023
@andyblac
Copy link
Author

this still occurs in beta 4.0b11, if I can get any logs or debug info please just explain what you want me to do.

@andyblac
Copy link
Author

this is from safari java console
Screenshot 2023-07-26 at 10 19 50

@RomRider
Copy link
Collaborator

Some of your templates in your custom fields are incorrect it seems. The JS console will show something like "ButtonCardJSTemplateError" with the error.

@andyblac
Copy link
Author

if i refresh couple time the error goes away, or if i switch to a standard lovelace dashbaord and refresh then go back to this one again error is gone.
Screenshot 2023-07-26 at 10 27 53

can you see any issue in my code above ?

@andyblac
Copy link
Author

this is what i see in the console
Screenshot 2023-07-26 at 10 30 18

@RomRider
Copy link
Collaborator

can you see any issue in my code above ?

No, as I lack context.
But again, I need the js console output of the ButtonCardJSTemplateError.

Please try with chrome as it seems safari behaves strangely with the error thrown by your templates (it says anonymous where it should report the error as stated above with ButtonCardJSTemplateError).

@andyblac
Copy link
Author

is this what your after ?

Screenshot 2023-07-26 at 10 39 26

@andyblac
Copy link
Author

andyblac commented Jul 26, 2023

this is the line

      - color: "[[[ return variables.ulm_card_room_sensor_color ? 'rgba(var(--color-'+variables.ulm_card_room_sensor_color+'),0.4)' : 'rgba(var(--color-theme),0.2)'; ]]]"

i want to only use the color if one is defined, else use --color-theme

@RomRider
Copy link
Collaborator

Change any access to variables.xxxx to variables?.xxxx and it will be fixed.

@RomRider
Copy link
Collaborator

But i'll fix it so that variables exists even if there are none defined. It will just be an empty object, currently it's undefined. It's cleaner also.

@andyblac
Copy link
Author

thanks, i really appreciate the time you take to help people like me out. can I donate to you somehow ?

github-actions bot pushed a commit that referenced this issue Jul 26, 2023
## [4.0.0-dev.13](v4.0.0-dev.12...v4.0.0-dev.13) (2023-07-26)

### Bug Fixes

* **templates:** `variables` was `undefined` if none where provided. ([fad332b](fad332b)), closes [#718](#718)
@github-actions
Copy link

🎉 This issue has been resolved in version 4.0.0-dev.13 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@andyblac
Copy link
Author

both ways fix the issue, thanks again

@Mariusthvdb
Copy link
Contributor

Mariusthvdb commented Jul 26, 2023

Jerome, if I may, why do we explicitly have to use the keyword variables in our templates after a declaration? I mean, if we set a variable in core Jinja we only need to use the name of the variable, without anything further.

eg:

  variables:
    id: >
      [[[ return entity.entity_id.split('.')[1]; ]]]
  entity_picture: >
    [[[ return '/local/family/' + variables.id + '.jpg'; ]]]

why not simply do:

  variables:
    id: >
      [[[ return entity.entity_id.split('.')[1]; ]]]
  entity_picture: >
    [[[ return '/local/family/' + id + '.jpg'; ]]]

?

If I am being ignorant of common JS practice, sorry, I have been wondering about this. not an issue in any way so nevermind if this is a silly question.

@andyblac
Copy link
Author

andyblac commented Jul 26, 2023

i think variables is a simple list to store all your vars so you can use the same var name in each template, else if you only have 1 master list you could only use the var name once in the button. but could be wrong, I'm, still new to all this.

@RomRider
Copy link
Collaborator

why do we explicitly have to use the keyword variables

Because it's easier for me from a code perspective and avoid weird conflicts (and thus potential new unneeded questions/issues 😄 )

github-actions bot pushed a commit that referenced this issue Jul 29, 2023
## [4.0.0](v3.5.0...v4.0.0) (2023-07-29)

### ⚠ BREAKING CHANGES

* **helpers:** If you were using any of the beta before `4.0.0-dev14`. Please replace all the calls to helper functions with `helpers.xxx` for eg. `helpers.relativeTime(entity.state)` or `helpers.localize(entity)`
* **hacs:** Minimum required HA Version is now 2023.7
* **actions:** Requires HA 2023.4 minimum. Support for the new action format (`target` is also be supported), `service_data` should be renamed to `data` (but it still works with the old format)
* **icons:** This might break your card-mod setup
* this might break some of your color settings

### Features

* **action:** `repeat_limit` for `hold_action` ([73c216f](73c216f)), closes [#564](#564) [#555](#555)
* **actions:** Support for the new action (assist) and all the future ones ([d9c17a4](d9c17a4)), closes [#711](#711) [#685](#685)
* **custom_fields:** Add `do_not_eval` to stop evaluating js templates in an embedded card ([1638cf8](1638cf8))
* **helpers:** all template functions are now available through the `helpers` object ([f22ed69](f22ed69))
* **icons:** replace ha-icon with ha-state-icon to follow new HA's icons per domain automatically ([ab6a3f5](ab6a3f5))
* **templates:** new `relativeTime` function to display a relative time in a template and update it automatically ([965a3d7](965a3d7)), closes [#701](#701)
* **templates:** New date and time format helpers ([9b4fb05](9b4fb05))
* **variables:** A variable can depend on another variable based on their name's alphabetical order ([8cddccb](8cddccb)), closes [#656](#656)
* Force the `numeric_precision` for states which are numbers ([24d75c2](24d75c2))
* new helper functions for date/time in templates ([2b75993](2b75993)), closes [#701](#701)
* Support for localization in templates ([5de2dc9](5de2dc9))

### Bug Fixes

* *_action more-info entity as a template was not evaluated ([02441b2](02441b2)), closes [#734](#734)
* `group_expand` now works even if the entity is not a `group.xxx` ([f192ded](f192ded)), closes [#645](#645)
* Color are now aligned with HA > 2022.12 ([685d55e](685d55e)), closes [#635](#635)
* custom fields would sometime throw unsafeHTML errors ([c67e1d5](c67e1d5)), closes [#725](#725)
* ha-icon (if in custom_fields) size was weird ([a448c8e](a448c8e))
* ha-state-icon CSS selector was wrong ([a1bb39a](a1bb39a))
* icon would be cut with card height defined ([19f8393](19f8393)), closes [#731](#731)
* localization fix ([02dfab3](02dfab3)), closes [#685](#685) [#693](#693)
* lock icon was displaying over more-info dialog ([bf075b0](bf075b0)), closes [#694](#694)
* lock would go out of the button ([0b3e4d3](0b3e4d3))
* non string fiels would error with an unsafeHTML error ([d65c347](d65c347)), closes [#725](#725)
* numerical states would not follow HA's format ([72d7c41](72d7c41)), closes [#662](#662)
* optimize contrast color compute ([35109c3](35109c3))
* relativeTime didn't support to set the first letter uppercase ([f8b9b09](f8b9b09)), closes [#735](#735)
* Some cards with child cards wouldn't be clickable ([9f21c58](9f21c58))
* text/icon contrast when using label-card ([01e199b](01e199b))
* variable which were objects were only evaluated once ([e40bda9](e40bda9))
* **templates:** `variables` was `undefined` if none where provided. ([fad332b](fad332b)), closes [#718](#718)
* tooltip would show over everything ([1bc8f99](1bc8f99))
* **color:** main config `color` was broken ([b93c996](b93c996))
* **hacs:** minimum HA version 2023.7 ([db3b394](db3b394))
* **templates:** don't use the `numeric_precision` from the card config for `localize` in js templates by default ([2cc384f](2cc384f))
@github-actions
Copy link

🎉 This issue has been resolved in version 4.0.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants