config-template-card icon indicating copy to clipboard operation
config-template-card copied to clipboard

Dashboard variables

Open swiergot opened this issue 1 year ago • 13 comments

If you need to use the same variable in multiple cards, then instead of defining it in each card's variables you can do that once for the entire dashboard.

title: My dashboard
config_template_card:
  variables:
    - states['sensor.light'].state
views:

Both arrays and objects are supported, just like in card's local variables. It is allowed to mix the two types, i.e. use an array in dashboard variables and an object in card variables, or the other way around. If both definitions are arrays, then dashboard variables are put first in vars. In the mixed mode, vars have array indices and as well as variable names.

swiergot avatar Aug 26 '22 07:08 swiergot

Seems a very promising feature! What we will be able to do is:

  1. Define all global vars in a dedicated file - vars.yaml:
variables:
  GLOB_VAR_1: xxxxxxxxxxxx
  GLOB_VAR_2: xxxxxxxxxxxx
  1. Use these vars in every dashboard:
title: My dashboard 1
config_template_card: !include vars.yaml
views:
  ...
title: My dashboard 2
config_template_card: !include vars.yaml
views:
  ...

ildar170975 avatar Aug 26 '22 22:08 ildar170975

@ildar170975 Just checked, it's working as you said.

swiergot avatar Aug 27 '22 16:08 swiergot

@swiergot Very great. Hope this PR will be merged! Thanks a lot for your addition, will start testing it.

ildar170975 avatar Aug 30 '22 19:08 ildar170975

@swiergot Could you provide a "js" file so I will replace it in my "community/config-template-card" folder? Wanna test your changes.

ildar170975 avatar Sep 01 '22 13:09 ildar170975

@ildar170975 Here it is:

config-template-card.js.gz

swiergot avatar Sep 02 '22 10:09 swiergot

The current usage for global var is:

title: My dashboard
config_template_card:
  variables:
    varSUN: states['sun.sun'].state
    varSUN_ELEVATION: states['sun.sun'].attributes['elevation']
views:
...

Assume we have multiple dashboards in yaml mode. Then we may need to place the same "config_template_card" section in every dashboard file. Alternatively - me may define it like this for every dashboard:

title: My dashboard
config_template_card: !include my_vars.yaml
views:
...

where the my_vars.yaml stores our vars:

  variables:
    varSUN: states['sun.sun'].state
    varSUN_ELEVATION: states['sun.sun'].attributes['elevation']

But still we have to store ALL our vars in ONE file. Alternatively - we may keep all vars in a special subfolder in separate files:

title: My dashboard
config_template_card:
  variables: !include_dir_merge_named config-template-card-vars/
views:
...

where config-template-card-vars contains yaml files like this (kind of "config splitting"):

varSUN: states['sun.sun'].state
varSUN_ELEVATION: states['sun.sun'].attributes['elevation']

So far it works OK, thank you very much!

And we came to my proposal. Next, assume we are using custom:decluttering-card & custom:button-card too - then our dashboard file looks like this:

title: My dashboard
decluttering_templates: !include_dir_merge_named templates-decluttering/
button_card_templates: !include_dir_merge_named templates-button-card/
config_template_card:
  variables: !include_dir_merge_named config-template-card-vars/
views:
...

That is why I propose to omit the "variables" level; and we may rename the section to "config_template_card_vars". Then it will look like this:

title: My dashboard
decluttering_templates: !include_dir_merge_named templates-decluttering/
button_card_templates: !include_dir_merge_named templates-button-card/
config_template_card_vars: !include_dir_merge_named config-template-card-vars/
views:
...

What do you think?

ildar170975 avatar Sep 05 '22 22:09 ildar170975

Makes sense. I don't expect any other configuration than variables to end up there.

swiergot avatar Sep 07 '22 07:09 swiergot

Great thanks! Started testing.

ildar170975 avatar Sep 07 '22 14:09 ildar170975

Strange, it does not work. A simple test in Lovelace storage mode:

  1. Open a dashboard in "raw" mode.
  2. Add this:
config_template_card_vars:
  varSUN: states['sun.sun'].state
  1. Add this card:
type: custom:config-template-card
entities:
  - sun.sun
card:
  type: entities
  title: ${varSUN}
  entities:
    - entity: sun.sun
  1. The card is not displayed.

ildar170975 avatar Sep 07 '22 15:09 ildar170975

This may simply be a cache issue.

First, if you have config-template-card.js.gz in /config/www/local/config-template-card/, make sure both .js and .js.gz are new versions.

Then try to open this in the web browser:

http://HA-HOST:8123/hacsfiles/config-template-card/config-template-card.js

I suspect it will be the old version. Press F5 to refresh. Then reload the dashboard.

swiergot avatar Sep 07 '22 16:09 swiergot

This may simply be a cache issue.

Shame on me. You are absolutely right. Forgot to clear a cache. Cleared - works FINE.

make sure both .js and .js.gz are new versions.

Have just "js" file. I copied "gz" file, then ran "gunzip xx.gz". It works anyway. AFAIK gz is needed as a backup.

I suspect it will be the old version

Checked it's content right after unzipping, found a desired "config_template_card_vars" keyword ))).

Thanks a lot once more! I am testing the plugin.

ildar170975 avatar Sep 07 '22 16:09 ildar170975

You're welcome. Thank you for testing and feedback.

swiergot avatar Sep 07 '22 18:09 swiergot