iantrich / config-template-card

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

entities parameter does not accept variable #93

Open wrieden opened 2 years ago

wrieden commented 2 years ago

Checklist:

Release with the issue: Master Branch Last working release (if known): unknown Browser and Operating System: chrome 96.0.4664.110, Win 11

Description of problem:

entity list based of variables is not working for me. For example:

type: custom:config-template-card
variables:
  STATE_KEYS: Object.keys(states).filter(k => k.startsWith('sensor'))
entities: ${STATE_KEYS}

or

type: custom:config-template-card
variables:
  STATE_KEYS: Object.keys(states).filter(k => k.startsWith('sensor'))
entities: 
  - ${STATE_KEYS}

Additional information:

This seems to be an error with the shouldUpdate function, as the evaluation of the Variables is carried out too late. I've made the following change locally, which solves the problem for me:

old:

 if (oldHass) {
    for (const entity of this._config.entities) {
      const evaluatedTemplate = this._evaluateTemplate(entity);
      if (Boolean(this.hass && oldHass.states[evaluatedTemplate] !== this.hass.states[evaluatedTemplate])) {
        return true;
      }
    }
    return false;
  }

new:

if (oldHass) {
    for (const entity of this._evaluateTemplate(this._config.entities)) {
        if (Boolean(this.hass && oldHass.states[entity] !== this.hass.states[entity])) {
            return true;
        }
    }
    return false;
}
ildar170975 commented 6 months ago

@wrieden

  1. What happens if "entities" are not templated here? I.e. in case of a traditional list of entities.
    for (const entity of this._evaluateTemplate(this._config.entities)) {
  2. Suggest you to create a PR for this proposal!
ildar170975 commented 6 months ago

Tested the proposed fix locally. It causes constant flickering - even when a monitored entity is not changed.

type: custom:config-template-card
entities:
  - sun.sun
card:
  type: history-graph
  entities:
    - sun.sun

This card erratically flickering every several seconds.