thomasloven / hass-lovelace_gen

🔹 Improve the lovelace yaml parser for Home Assistant
MIT License
218 stars 22 forks source link

Question: Include variables from file? #49

Open Aleborg opened 1 year ago

Aleborg commented 1 year ago

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 commented 1 year ago

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 commented 6 months ago

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" %}