Jinja2 out of scope variable

Hi!

I have the following problem in Jinja2 within a template node in Node-red and HASS:

  1. First, I create three objects that I then add to a list called “my_list”.

  2. I need to know if any of them have the “active” variable set to false, so I initialize a flag for this purpose.

  3. I iterate through the collection of objects (ignoring the first one in the list) and if any of them have the “active” variable set to false, then I also modify the flag.

  4. After finishing checking the entire list, the flag should indicate whether or not it found objects with the “active” variable set to false.

The issue is that the flag does NOT change its value. I suspect that the modifications I make to the flag within the loop are not being retained once we exit it.

Here is the code:

{# set up the objects #}
{% set resultObj = { "allActive": true } %}
{% set obj1 = {"id": 1423, "active": false} %}
{% set obj2 = {"id": 4656, "active": true} %}

{# create a list based on those objects #}
{% set my_list = [resultObj, obj1, obj2] %}

{# set an optimistic flag #}
{% set are_all_active = true %}

{# iterate my_list looking for inactive ids (skip first object in list) #}
{% for item in my_list[1:] %}
    {% if not item.active %}
        {% set are_all_active = false %}
    {% endif %}
{% endfor %}

are_all_active?: {{ are_all_active }}

This will give us:

are_all_active?: True

Here is the Node-RED flow if you want to try it:

image

[{"id":"e1a91c60268c703a","type":"api-render-template","z":"d093d58a7f75472a","name":"All Active?","server":"cfaa527b.040d7","version":0,"template":"{# set up the objects #}\n{% set resultObj = { \"allActive\": true } %}\n{% set obj1 = {\"id\": 1423, \"active\": false} %}\n{% set obj2 = {\"id\": 4656, \"active\": true} %}\n\n{# create a list based on those objects #}\n{% set my_list = [resultObj, obj1, obj2] %}\n\n{# set an optimistic flag #}\n{% set are_all_active = true %}\n\n{# iterate my_list looking for inactive ids (skip first object in list) #}\n{% for item in my_list[1:] %}\n    {% if not item.active %}\n        {% set are_all_active = false %}\n    {% endif %}\n{% endfor %}\n\nare_all_active?: {{ are_all_active }}\n","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":270,"y":280,"wires":[["c04a175a5073aae4"]]},{"id":"71a542b692fa1bb6","type":"inject","z":"d093d58a7f75472a","name":"Run it!","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":90,"y":280,"wires":[["e1a91c60268c703a"]]},{"id":"c04a175a5073aae4","type":"debug","z":"d093d58a7f75472a","name":"Show result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":450,"y":280,"wires":[]},{"id":"cfaa527b.040d7","type":"server","name":"Home Assistant","addon":true}]

What can I do to solve this? Any help would be really appreciated.

Thank you all.

Just answered this the other day, try namespace.
See discussion here

Thank you, that solved the issue!