Why jinja macro didn´t give the right value back?

Hi everyone,

I created a jinja macro in custom_templates folder. When i test the jinja part in the developer tools I get the right response. When i the use the function i get weird responses.


I call the function two times.
The function gives false back, but I get the result off true.

Why is this so?
Can someone help me?

{% macro LichterInBereichAn(area_name) %}
  {% set ns = namespace(lights=[], groups=[]) %}
  {% for area in floor_areas(area_name) %}
    {% set entities = area_entities(area) | select('match', 'light.') | list %}
    
    {% for entity in entities %}
      {% if state_attr(entity, 'entity_id') is not none %}
        {% if entity not in ns.groups %}  
          {% set ns.groups = ns.groups + [entity] %}
        {% endif %}
      {% else %}
        {% set ns.is_in_group = false %}
        {% for group in ns.groups %}
          {% if entity in state_attr(group, 'entity_id') %}
            {% set ns.is_in_group = true %}
          {% endif %}
        {% endfor %}
        {% if not ns.is_in_group and entity not in ns.lights %} 
          {% set ns.lights = ns.lights + [entity] %}
        {% endif %}
      {% endif %}
    {% endfor %}
  {% endfor %}
  
  {% set used_entities = ns.groups + ns.lights %}
  {% set on_lights = used_entities | select('is_state', 'on') | list %}
  
  {{on_lights | count > 0}}
{% endmacro %}

Macros return strings, so it’s returning “False” the string, not false the boolean. Any non-empty strings are always true.

You can use the bool filter in your if statement:

1 Like

Thanks. I didn´t know that.

Hello Totenhose,

| bool
Is your friend at times like these…