core icon indicating copy to clipboard operation
core copied to clipboard

custom templates may give an error if commented by "#"

Open ildar170975 opened this issue 2 years ago • 15 comments

The problem

Related to reusable custom templates: https://www.home-assistant.io/docs/configuration/templating/#reusing-templates

Consider some jinja-file containing these macros:

{% macro test_macro(input_value) -%}
  {% set new_value = (input_value|int) * 2 -%}
  {{ new_value }}
{%- endmacro %}

{% macro test_macro_2(input_value_1,input_value_2) -%}
  {%- set new_value = input_value_1|int + input_value_2|int -%}
  {{ new_value }}
{%- endmacro %}

They work OK: image

{% from 'test_macro.jinja' import test_macro %} 
{{ test_macro(2) }} 
{% from 'test_macro.jinja' import test_macro_2 %} 
{{ test_macro_2(2,3) }} 

Let's add some formatting for the 2ns macro:

{% macro test_macro(input_value) -%}
  {% set new_value = (input_value|int) * 2 -%}
  {{ new_value }}
{%- endmacro %}

{% macro test_macro_2(input_value_1,
                      input_value_2) -%}
  {%- set new_value = input_value_1|int + input_value_2|int -%}
  {{ new_value }}
{%- endmacro %}

These macros still give same correct result in Dev Tools.

Now comment the 2nd macro:

{% macro test_macro(input_value) -%}
  {% set new_value = (input_value|int) * 2 -%}
  {{ new_value }}
{%- endmacro %}

# {% macro test_macro_2(input_value_1,
#                       input_value_2) -%}
#   {%- set new_value = input_value_1|int + input_value_2|int -%}
#   {{ new_value }}
# {%- endmacro %}

and then test it - it gives an error: image

This commented variant does not give an error:

{% macro test_macro(input_value) -%}
  {% set new_value = (input_value|int) * 2 -%}
  {{ new_value }}
{%- endmacro %}

# {% macro test_macro_2(input_value_1,input_value_2) -%}
#   {%- set new_value = input_value_1|int + input_value_2|int -%}
#   {{ new_value }}
# {%- endmacro %}

image

Tested with Markdown card - it stays empty with the faulty variant.

In other places of HA commented lines are ignored. Here with commented macros we see a different picture.

P.S. Same with a case when a broken line is inside a macro:

# {% macro test_macro_2(input_value_1,input_value_2) -%}
#   {%- set new_value = input_value_1|int
#                       + input_value_2|int -%}
#   {{ new_value }}
# {%- endmacro %}

Users often break long lines. Commenting them causes the described problem.

What version of Home Assistant Core has the issue?

2023.9.3

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

No response

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

ildar170975 avatar Oct 01 '23 20:10 ildar170975

Seems that custom macros does not like when some lines are divided ("broken") and then commented. As I said - in other places of HA this is not observed. Consider these sensors:

template:
  - sensor:
      - name: test_commented_jinja_1
        state: >-
          {% set STATE = states('sun.sun') -%}
          {%- set VALUE = STATE.split('_')[0] -%}
          {{ VALUE}}

      - name: test_commented_jinja_2
        state: >-
          {% set STATE = states('sun.sun') -%}
          {%- set VALUE = STATE.split('_')[1] -%}
          {{ VALUE}}

which give these states: image

{{states('sensor.test_commented_jinja_1')}}
{{states('sensor.test_commented_jinja_2')}}

Then format the 2nd sensor - break some line:

template:
  - sensor:
      - name: test_commented_jinja_1
        state: >-
          {% set STATE = states('sun.sun') -%}
          {%- set VALUE = STATE.split('_')[0] -%}
          {{ VALUE}}

      - name: test_commented_jinja_2
        state: >-
          {% set STATE = 
            states('sun.sun') -%}
          {%- set VALUE = STATE.split('_')[1] -%}
          {{ VALUE}}

which does not harm (as expected). Then comment the 2nd sensor:

template:
  - sensor:
      - name: test_commented_jinja_1
        state: >-
          {% set STATE = states('sun.sun') -%}
          {%- set VALUE = STATE.split('_')[0] -%}
          {{ VALUE}}

      # - name: test_commented_jinja_2
      #   state: >-
      #     {% set STATE = 
      #       states('sun.sun') -%}
      #     {%- set VALUE = STATE.split('_')[1] -%}
      #     {{ VALUE}}

which gives us (as expected): image i.e. commenting line does not harm. So, there are issues with custom macros only.

ildar170975 avatar Oct 01 '23 20:10 ildar170975

Hey there @phracturedblue, @tetienne, @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (template) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of template can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Renames the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign template Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


template documentation template source (message by IssueLinks)

home-assistant[bot] avatar Oct 01 '23 20:10 home-assistant[bot]

I als ran into the # not working as comments in custom templates. This is working:

{#
  your comments
#}

erkr avatar Oct 05 '23 11:10 erkr

I had to remove these comments in a macro file to prevent the output appearing as multiple lines or in some cases to make the output even render (in a mushroom template entity card)

Caused Issues:

{# --------------------------------------------------- #}
{# --------------------------------------------------- #}
{# --------------------------------------------------- #}
{%- macro get_domain(entity_id) -%}
    {{ entity_id.split('.')[0] }}
{%- endmacro -%}

No Issues:

{%- macro get_domain(entity_id) -%}
    {{ entity_id.split('.')[0] }}
{%- endmacro -%}

teskanoo avatar Oct 07 '23 22:10 teskanoo

@teskanoo That is new for me (and can't reproduce on dev tools). What happens if your add then like this?:

{#
   ----------
#}

Is it specific when using the minus characters?!

erkr avatar Oct 08 '23 20:10 erkr

@erkr Well, commenting with# INSIDE a template like

{% ... %}
{{ ... }}
{% ... %}
# {{ ... }}
{% ... %}

is wrong, we should use {# ... #} as you said. But here I was surprised that commenting with # OUTSIDE a template:

{% macro ... -%}
  {% ... -%}
  {{ ... }}
{%- endmacro %}

## ....

may cause errors as described above.

ildar170975 avatar Oct 14 '23 04:10 ildar170975

@ildar170975
Just a guess. When we import macro, that is inside the template. Maybe that is the reason the comments in the jinja file are interfering if # is used

erkr avatar Oct 14 '23 10:10 erkr

Here is how a noob user may see it: When using a macro, first we call an “import” command: take this macro XYZ from this ABC.jinja file. Then we call the macro itself. And looks like the first step “puts” the whole ABC.jinja content into a calling template - and presence of “#” may cause errors.

ildar170975 avatar Oct 14 '23 13:10 ildar170975

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

up

ildar170975 avatar Jan 16 '24 04:01 ildar170975

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

up

ildar170975 avatar Apr 17 '24 17:04 ildar170975

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

Up

ildar170975 avatar Jul 21 '24 23:07 ildar170975

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

Up

ildar170975 avatar Oct 20 '24 22:10 ildar170975

up

ildar170975 avatar Oct 28 '24 01:10 ildar170975

Gonna close this issue with an advice: do not use "#" for comments, use "{# #}". Or use them together to make comments more vivid: image

It is not documented in docs. Anyway, no reaction from Devs, it is meaningless to keep this issue open here.

ildar170975 avatar Dec 23 '24 22:12 ildar170975