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

Improve the way the card checks whether it needs to be updated #307

Closed
akasma74 opened this issue Apr 5, 2020 · 9 comments
Closed

Improve the way the card checks whether it needs to be updated #307

akasma74 opened this issue Apr 5, 2020 · 9 comments
Labels
FR Feature Request

Comments

@akasma74
Copy link

akasma74 commented Apr 5, 2020

Is your feature request related to a problem? Please describe.
Please note that It only makes sense when the card has template(s)
not really, it's just a nice feature that would decrease the number of unnecessary updates if entity is not specified as in this case the card will be updated every time any HA entity changes its state.
the idea was born here

Describe the solution you'd like

I propose to introduce a new entities configuration variable that controls when the card is updated (instead of entity). It should accept a list of entity_ids or a string (one entity_id).

If it is specified, the card should be updated only when at least one of the entities changed its state.

This variable should be independent from entity so it's possible to have entities specified without specifying entity, which will allow the card to be updated regardless.

In case both entity and entities are specified, during setup the card should add entity's value to entities (if it's not already there). I presume that to represent entities a list/array is used internally and on statrup it's empty.

When the card needs to decide on updating (as it receives an updated hass object), it looks at internal list with entities and there are 2 cases:

  1. the list contains endity_id(s): check one-by-one (or any other way) if an entity changed its state and if so, trigger the card's update.
  2. the list is empty: update unconditionally (as it can only happen when no entity and no entities is specified)

There are several things that need to be addresses to avoid breakages.

a. Updating when using existing configs, i.e when there is no entities specified.
There are 2 cases possible:

  1. entity is specified (so the card should be updated only when that entity changes its state): add its content to the list representing entities.
  2. no entity is specified (so the card should updated on every change of any HA entity): do nothing.

b. Global templates are used.
In this case I propose to append content of template's entities to the card's entities list (internally, checking that all entities are unique before adding (just a nice touch)).

c. If all the above is not enough, it's possible to introduce a new config variable like update_mode that will be used during the card's update: if it's entity(default mode) and the card has entity (so it's an old-style config) - update immediately, otherwise perform checks as described above (entities mode). Consider issuing a warning if update_mode: entity but no entity specified and the same with update_mode: entities and entities.
I'm not a big fan of this idea as it makes things more complex but it might be useful in the long run if users are adamant they want the old update behaviour as it actually disables the new one.

Describe alternatives you've considered
do nothing, it works but consumes more resources as all button cards that do not have entity specified are updated much more frequently than they could have been.

@akasma74 akasma74 added the FR Feature Request label Apr 5, 2020
@RomRider
Copy link
Collaborator

RomRider commented Apr 9, 2020

Please test 3.3.0-0 and report back :)

@akasma74
Copy link
Author

akasma74 commented Apr 9, 2020

I can confirm that the beta works fine.

I presume you went that auto discovery way to avoid breaking changes and asking people to add triggers_update: -all.
Well, it makes sense.

So basically everythig is fine and you just need to update the docs about new mechanism and tweak the card so triggers_update supports individual entities as well ;)

👍

Here's the link too my post with more details just in case.

@akasma74
Copy link
Author

akasma74 commented Apr 9, 2020

btw, here's an interesting example from my config.
I use hierarchy of templates

button_card_templates:
  slave_row:
    template: base_row
    styles:
      card:
        - visibility: >-
            [[[
              let entity_id = variables.entity;
              ...
            ]]]


  unit_of_measurement_row:
    template: slave_row

and then a button itself

type: custom:button-card
template: unit_of_measurement_row
entity: {{ _input_entity_id() }}
variables:
  entity: {{ _master_entity_id() }}

and to get it updated as I want (i.e on variables.entity state change) I currently have to change the card's config

type: custom:button-card
template: unit_of_measurement_row
entity: {{ _input_entity_id() }}
triggers_update: [ &master_entity_id {{ _master_entity_id() }} ]
variables:
  entity: *master_entity_id

I'd love to do it on template level (slave_row) but can see no way of doint that.
something like this would do the job:

button_card_templates:
  slave_row:
    template: base_row
    triggers_update: >
      [[[ return variables.entity ]]]
    styles:
      card:
        - visibility: >-
            [[[
              let entity_id = variables.entity;
              ...
            ]]]

@RomRider
Copy link
Collaborator

RomRider commented Apr 9, 2020

I was thinking about adding templates to the triggers_update field... but this is going to introduce load again as it will have to be evaluated every time any entity in HA is updated as some people will do different things than just use variables, for eg:
return 'switch.' + entity.entity_id.split('.')[1] + '_switch'

@RomRider
Copy link
Collaborator

RomRider commented Apr 9, 2020

Single entry in triggers_update in bebe419

@akasma74
Copy link
Author

akasma74 commented Apr 9, 2020

I was thinking about adding templates to the triggers_update field... but this is going to introduce load again as it will have to be evaluated every time any entity in HA is updated as some people will do different things than just use variables, for eg:
return 'switch.' + entity.entity_id.split('.')[1] + '_switch'

yeah, you can set it aside until someone like Marius asks for it ;)

Single entry in triggers_update in bebe419

that's fab! I presume it'll be available in the next beta/release (cannot see a way to get an updated button-card.js)?

@RomRider
Copy link
Collaborator

RomRider commented Apr 9, 2020

You'd have to compile it yourself but the next beta will have some more things inside (like multiple template per card as an array that you've asked also)

@akasma74
Copy link
Author

akasma74 commented Apr 9, 2020

no worries at all, just checking as I have 0 experience in custom cards development ;)
I'm patiently wating.. ;) 👍

@RomRider
Copy link
Collaborator

Released in 3.3.0

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

No branches or pull requests

2 participants