Is Jinja macro return type always a string?

Hello there,

How exiting is the new macro functionality introduced in 2023.4?
But what is the return type of them?

I’m trying to replace some stuff that I’m using in almost all my integration but I’m wondering what is the return type of the macro.

Let me explain:

I’ve plenty automation templates that start like so:

{% set is_weekend = now().isoweekday() in [6, 7] %}
{% set is_conge_scolaire = is_state('calendar.conges_scolaires', 'on') %}
{% set is_ferie = is_state('calendar.jours_feries_en_belgique', 'on') %}

All these (and other stuffs) are used later in conditions like:

{{ is_weekend or is_conge_scolaire or is_ferie }}

For non English speakers, it is my way to know that the kids are at home and change the house’s behaviour.

With HA 2023.4, I started to group all those repetitive set commands in a date.jinja file.
Here I am, proud of myself, with my macros:

{%- macro is_weekend() -%}
{{- now().isoweekday() in [6, 7] -}}
{%- endmacro -%}

{%- macro is_conge_scolaire() -%}
{{- is_state('calendar.conges_scolaires', 'on') -}}
{%- endmacro -%}

{%- macro is_ferie() -%}
{{- is_state('calendar.jours_feries_en_belgique', 'on') -}}
{%- endmacro -%}

And I started replacing all this in my automation templates:

- id: '12536874963'
  alias: Too much screen time
  description: ''
  trigger:
  - platform: template
    value_template: >
      {% import date.jinja as dt %}
      {{ dt.is_weekend() or dt.is_conge_scolaire() or dt.is_ferie() }}

And, crash!, bang!, nothing is working anymore.

dt.is_weekend() is returning True (string), not True (boolean).

So here is my question (longest intro ever, sorry): are the macro only about text returned values or did I miss something?

Because to make it work, I’ve to do this:

- id: '12536874963'
  alias: Too much screen time
  description: ''
  trigger:
  - platform: template
    value_template: >
      {% import date.jinja as dt %}
      {{ dt.is_weekend() == 'True' or dt.is_conge_scolaire() == 'True' or dt.is_ferie() == 'True' }}

Which makes my eyes bleed.

Thank you for your help.

1 Like

Yes.
See the answer from petro two posts below my question.

Ok, I was too enthusiast, too fast.
We are wiping the casts.

Slightly less eye-bleedy?

    value_template: >
      {% import date.jinja as dt %}
      {{ 'True' in [dt.is_weekend(), dt.is_conge_scolaire(), dt.is_ferie()] }}

For that one, yes, but now I have to go through the dozen of others and it is mostly and’s not or’s.
But good to know, thank you.

Maybe I’ll encapsulate some complex conditions in their own macro.

Work in progress :slight_smile:

Or:

{% import date.jinja as dt %}
{{ 'True' in [dt.is_weekend(), dt.is_conge_scolaire(), dt.is_ferie()] }}

And:

{% import date.jinja as dt %}
{{ 'False' not in [dt.is_weekend(), dt.is_conge_scolaire(), dt.is_ferie()] }}

You also have the option of the bool filter if you just prefer using “or”/“and”/“not”:

{{ dt.is_weekend()|bool or dt.is_conge_scolaire()|bool or dt.is_ferie()|bool }}
1 Like

Jinja is all python under the hood and like python empty string ('') is “falsy” and a string with something in it is “truthy”. So if you change your boolean-ish macros to return '' when they want to return False and 'true' (or any random non-empty string) when they mean to return True then you can use them in boolean expressions like this

{{ dt.is_weekend() or dt.is_conge_scolaire() or dt.is_ferie() }}

EDIT: To show what I mean with an example

Also you should have your boolean-ish macros return 'True' not some random string. Realized if you returned some random string other then 'True' then HA won’t like it in some places. Here’s why:

The output is actually the string you returned from the macro ('abc' in my example above). It’s not a boolean even though it acts like one in the expression. So you’ll be relying on HA to interpret it as a boolean in some places, like binary sensor templates. I believe HA is fine with '' as False and 'True' as True but not sure it will like 'abc' as the output.

1 Like

Not elegant but the default cast to boolean seems to work.
Waiting for the week-end to tell me if I can keep it like that.
Thx.