Jinja macro and OR not working

Hello,

I’m implementing templates with the new macro functionality. However, it looks like or statements aren’t working.

Consider the following template:

{% from 'helpers.jinja' import is_last_changed_in_time_out %}
{% from 'helpers.jinja' import is_entity_currently_on %}
{% set last_changed = is_last_changed_in_time_out(area_name, states.binary_sensor, "presence", occupancy_time_out) %}
{% set is_on = is_entity_currently_on(area_name, states.binary_sensor, "presence")  %}
{{ last_changed or is_on}}
{% endmacro %}

If last changed is true and is_on is false the template is true. if last_changed is false and is_on is true it returns false. However, i’m using an or and i would expected the order doesn’t matter. Does someone know if this is a bug?

aka:

true or false = true
false or true = false

Looks like the template macro after the or isn’t rendered. Is this expected behaviour?

It’s because macros return strings. You need to convert them to booleans. Otherwise it’s resolved as a string. Populated string is true where an empty string is false.

That will also be true btw

1 Like
{{ last_changed|bool(false) or is_on|bool(false) }}

Thank you. Not the first time i’ve been burned by templates returning strings.

Yes, that’s why i was confused by the output. But apparently i was using strings.