custom templates may give an error if commented by "#"
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:
{% 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:
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 %}
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
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:
{{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):
i.e. commenting line does not harm.
So, there are issues with custom macros only.
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 closeCloses the issue. -
@home-assistant rename Awesome new titleRenames the issue. -
@home-assistant reopenReopen the issue. -
@home-assistant unassign templateRemoves 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)
I als ran into the # not working as comments in custom templates. This is working:
{#
your comments
#}
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 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
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
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
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.
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
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
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
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
up
Gonna close this issue with an advice:
do not use "#" for comments, use "{# #}".
Or use them together to make comments more vivid:
It is not documented in docs. Anyway, no reaction from Devs, it is meaningless to keep this issue open here.