Best way to import all variables from a Jinja template?

Hi team,

I was reading thru:

And the post I made here has some background / context:

But in short, let’s say I have the following Jinja template in custom_templates folder:

{# COMMON #}
{% set someone_home = is_state('binary_sensor.someone_is_home', 'on') %}
{% set auto = is_state('input_select.home_ambiance', 'Automatic') %}

{# SLEEP #}
{% set napping = is_state('input_boolean.napping', 'on') %}
{% set sleeping = is_state('input_boolean.sleeping', 'on') %}
{% set overnight = is_state('input_boolean.overnight', 'on') %}

{# MOVIE #}
{% set movie = is_state('input_boolean.watching_movie', 'on') %}
{% set tv_playing = is_state('media_player.living_room_apple_tv', 'playing') %}

{# WEATHER #}
{% set dark_outside = is_state('binary_sensor.is_dark_outside', 'on') %}

{# TIMES #}
{% set early = is_state('binary_sensor.early_morning', 'on') %}
{% set morning = is_state('binary_sensor.morning', 'on') %}

{# TIMES #}
{% set alarm_triggered = is_state('alarm_control_panel.security_system', 'triggered') %}

Basically it’s just some standard variables that I was copy pasting in a lot of my automations / templates etc prior to this feature being announced recently.

I’d like to be able to import them and use them in other templates or automations, is there a way to import *?

Right now I have for example:

{% from 'common.jinja' import someone_home, auto, napping, sleeping, overnight, movie, tv_playing, dark_outside, early, morning, alarm_triggered
 with context %}

{{ someone_home }}

But I’d like to import all variables without stating them, is that possible?

Thank you!!
Matt

If you’re using that custom_templates file, you’re misunderstanding how imports work.

When you import those vairables, you’re importing the result when they were executed. They will not continuously update. Nore will they re-execute when you import them. You’re simply just getting the value of is_state(...) when the sets were executed.

e.g. if you have an equation

y = 2
x = y + 4

and you import x, the x will always be 6, regardless if you change y to any other number.

Outside that, there is no way to import all macros in a file at this time.

@petro thank you kindly for that, I didn’t know that. I really appreciate your reply.

When I’m in developer tools, I note that if I add “with context” per the docs of Jinja2, Developer Tools states that it will auto update:

Additionally, the Jinja2 docs state that caching is disabled for you automatically when you add “with context”:

The combination of these 2 things is what led me to believe it would update rather than using the cached value. I just wanted to confirm with you that my understanding is not correct it and it’ll indeed not update.

Thanks so much for your reply, I am learning a lot, I really appreciate it!

Matt

Just wanted to update this for anyone else who wants to do it. I believe the following works (at least in my testing). It both updates automatically as well as doesn’t require you to specify everything in the import.

{% import 'common.jinja' as c with context %}
{{ c.auto }}
1 Like

Nice find, it’s a bit different than python syntax. You can probably skip the as c and use it as common.auto

1 Like

Thank you kindly Petro, I will give that a whirl! I appreciate the tip!

Matt