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

Question: Include variables from file? #49

Open
Aleborg opened this issue Apr 26, 2023 · 2 comments
Open

Question: Include variables from file? #49

Aleborg opened this issue Apr 26, 2023 · 2 comments

Comments

@Aleborg
Copy link

Aleborg commented Apr 26, 2023

Is it possible somehow to include variables from another file somehow?

I tried these two methods, but actually didn't expect it to work (and it didn't):

views: !include 
  - common/views.yaml
  - config: !include json-settings.yaml

and in json-settings.yaml:

'{ "settingA": true, "settingB": false }'

And this version as well:

views: !include 
  - common/views.yaml
  - !include json-settings.yaml

and in json-settings.yaml:

config: '{ "settingA": true, "settingB": false }'

Also tried using jinja2 include without success.
I'm trying to avoid using global variables (requires a restart of HA)

Does anyone have any ideas on how to solve this?

@Aleborg
Copy link
Author

Aleborg commented May 4, 2023

Solved it by using jinja2's import:

{% import '/config/includes/lovelace/my-tablet/settings/settings.json' as settings -%}

Just to bad that I have to use the full path :/

@skavan
Copy link

skavan commented Mar 28, 2024

Here's another way that has the benefit of being able to pluck sub objects (keys) which I couldn't get to work using your version...

a generic macro followed by a templated call to the macro.
has allowed me to replace 100's of lines of clunky yaml, with nice clean json...and pull it in all over the place.
If I could figure out how to do it with an import and a key, that would be even better. but I can't get something like:
{% from '/config/yaml/imports/footer-buttons.yaml' import porch as footer_data with context %} to work.

But the below does work.

{% macro import_json(source) %}
    {% include source %}
{% endmacro %}

{% set dpad_device = "firetv" %}
{% set dpad_file = '/config/yaml/imports/dpad_buttons.json' %}
{% set dpad_data = (import_json(dpad_file) | fromjson)[dpad_device] %}
{% set dpad_entity = "media_player.fire_tv_192_168_1_91" %}

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

No branches or pull requests

2 participants