Skip to content

Dashboard variables#109

Merged
iantrich merged 3 commits intoiantrich:masterfrom
swiergot:master
Jan 20, 2023
Merged

Dashboard variables#109
iantrich merged 3 commits intoiantrich:masterfrom
swiergot:master

Conversation

@swiergot
Copy link
Copy Markdown
Contributor

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.

@ildar170975
Copy link
Copy Markdown
Collaborator

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:
  ...

@swiergot
Copy link
Copy Markdown
Contributor Author

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

@ildar170975
Copy link
Copy Markdown
Collaborator

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

@ildar170975
Copy link
Copy Markdown
Collaborator

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

@swiergot
Copy link
Copy Markdown
Contributor Author

swiergot commented Sep 2, 2022

@ildar170975 Here it is:

config-template-card.js.gz

@ildar170975
Copy link
Copy Markdown
Collaborator

ildar170975 commented Sep 5, 2022

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?

@swiergot
Copy link
Copy Markdown
Contributor Author

swiergot commented Sep 7, 2022

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

@swiergot
Copy link
Copy Markdown
Contributor Author

swiergot commented Sep 7, 2022

@ildar170975
Copy link
Copy Markdown
Collaborator

Great thanks! Started testing.

@ildar170975
Copy link
Copy Markdown
Collaborator

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.

@swiergot
Copy link
Copy Markdown
Contributor Author

swiergot commented Sep 7, 2022

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.

@ildar170975
Copy link
Copy Markdown
Collaborator

ildar170975 commented Sep 7, 2022

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.

@swiergot
Copy link
Copy Markdown
Contributor Author

swiergot commented Sep 7, 2022

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

@iantrich
Copy link
Copy Markdown
Owner

Very cool! Looks great! Sorry for the delay, haven't been keeping up on my repos as of late.

@iantrich iantrich merged commit d616b6c into iantrich:master Jan 20, 2023
@artwhitehead
Copy link
Copy Markdown

Thanks. Updating both the .js and .js.gz files did the trick! Awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants