How to use reusable template in if statement

Hi,

I got following reusable template:

{% macro in_car() %}
    {% set device = "XX:XX:XX:XX:XX:XX (MY_CAR)" %}
    {{ device in (state_attr('sensor.phone_bluetooth_connection', 'connected_paired_devices') | list) }}
{% endmacro %}

Which works great in the development tools section:

          {% from 'bt_auto.jinja' import in_car %}
          {{ in_car() }} 

This returns:

Resulttype: boolean
false

However, when I try to use this in an if-else statement, it always executes the ‘True’ branch

          {% from 'bt_auto.jinja' import in_car %}
          {% if in_car() %}        
          Connected
          {% endif %}

Resultaattype: string
Connected

How should I make the reusable templates work in if-else statements ?

Thx in advance !

          {% from 'bt_auto.jinja' import in_car %}
          {% if in_car() | bool %}        
          Connected
          {% endif %}

Macros return string values.

1 Like

Thanks @123, I did not find that in the documentation (or I missed it).
Applied your changes, thx again for the help !

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

1 Like