TemplateSyntaxError after update from 2021.5.2 to 2021.5.4

This is a minimum example of a template, causing an TemplateSyntaxError in the template editor with HA 2021.5.4:

{% set z=1 %}
{% if z<02 %}
255
{% endif %}

This is the error caused: TemplateSyntaxError: expected token 'end of statement block', got 'integer'

This template doesn’t make much practical sense, but as said, it’s only a minimum example to trigger this error.

Using this template in an automation…

automation:
  - id: xxx
    trigger:
      platform: time_pattern
      minutes: "/5"
    action:
      - service: light.turn_on
        data:
          entity_id: light.bulb
          brightness: >-
            {% set z=1 %}
            {% if z<02 %}
            255
            {% endif %}

…passes the Configuration validation…

image

but it raises an invalid config notification…

image

…and an error in the log file:

2021-05-19 17:03:50 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token 'end of statement block', got 'integer') for dictionary value @ data['action'][0]['data']. Got None. (See ?, line ?).

When removing the leading zero from the if clause, the template is rendered without an error in the template editor as well as not causing an error in the log anymore:

{% set z=1 %}
{% if z<2 %}
255
{% endif %}

It seems, numbers with leading zeros aren’t interpreted as integers anymore. Is this expected behaviour?

Was there any (breaking) change between 2021.5.2 and 2021.5.4 which may have caused integers being interpreted differently than before? I read the release notes for 2021.5.3 and 2021.5.4, but couldn’t see any changes that may be related to this issue.

02 is fine as a zero-padded numeric string but not as an integer (and the concession made in the past, to handle it as an integer, has been rescinded).

What’s unusual is that it occurred in a patch release (per your report). One would expect this sort of behavioral change to occur exclusively in a major release.

FWIW, this was also reported as an Issue in GitHub. The person who reported it was in the habit of expressing time (hours, minutes, seconds) using zero-padded numbers. There’s no need to do that and any amount of zero-padding, such a 02 or 000000002, is never how the integer value 2 is expressed.

1 Like

No, of course it’s not necessary to zero-pad integers. I only used this padding for aesthetic reasons in a bigger if clause, coincidentally also for integers representing hours and minutes, similar to most of the comments in the GitHub issue you mentioned. Removing the leading zeros is no problem at all, apart from lacking a bit of readability in my code.

As you said, it’s fairly unexpected to introduce any breaking changes in a patch release and thus I wasn’t sure, if this change was intentional or not.

I think there’s still a minor issue, because the configuration validation doesn’t get triggered by this error and users may get into trouble when restarting HA, but I’ll mention this in the GitHub issue.

1 Like

Here because I just hit exactly the same issue in minor update I did yesterday. Took me a little while to work out what was going wrong as lots of my template sensors just disappeared and it was only when I did a check under Server Controls I started realising what had happened as the file editor didn’t show any error. I had these in purely for aesthetics as well, so I’m now working through my code removing them. Interestingly, 01 - 09 cause an error but 00 does not…

@derandiunddasbo @123 I’m getting the same exact error :

Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token 'end of statement block', got 'integer') for dictionary value @ data['condition'][0]['value_template']. Got None. (See ?, line ?).

My challenge is how to find the file/code as I have hundreds of automations in dozens of files - help would be appreciated

Use a text editor, like VS Code, which allows you to search for a text string across multiple files.

yes, which string? “<”/ “>” ?

According to the error message the problem exists in one of your automations so you will constrain your search to automations. It also indicates the problem exists in the value_template of the affected automation’s first condition. Start by searching for value_template and inspecting each one that is part of the the first condition that may contain a zero-padded value.

Alternately, if your text editor supports searching text based on a regular expression, you can use a regex pattern that looks for integer values with a leading zero. For example, this pattern will match integers with a leading zero:

[0][1-9]*

Screenshot from regex101.com
Screenshot from 2021-06-26 11-30-43

got 108 results in 11 files. Does visual studio code support regex?

Yes. I confirmed it does in my first reply to you (see above)

A quick Google search provides many references for it, here’s one:

Thanks @123, I couldn’t get regex to work (will keep trying) but did find the automation condition causing the issues and fixed it.

  - condition: template
    value_template: >
      {% set month=states("sensor.date").split('-')[1] | int %}   #check the [1] field of sensor.date [0,1,2] YYYY-MM-DD 
##      {%- if month == 06 or month == 07 or month == 08 -%}  ## was causing Invalid config for [automation]
      {%- if month == 6 or month == 7 or month == 8 -%}
        true
      {%- endif -%}
  action:
  - data:
      entity_id: input_boolean.season_summer
    service: input_boolean.turn_on
  - data:
      entity_id: input_boolean.season_fall
    service: input_boolean.turn_off
  - data:
      entity_id: input_boolean.season_winter
    service: input_boolean.turn_off
  - data:
      entity_id: input_boolean.season_spring
    service: input_boolean.turn_off

The automation doesn’t work properly now so I still have some work to do

I suggest you consider doing it like this:

  - condition: template
    value_template: "{{ now().month in [6, 7, 8] }}"
  action:
  - service: input_boolean.turn_on
    target:
      entity_id: input_boolean.season_summer
  - service: input_boolean.turn_off
    target:
      entity_id:
      - input_boolean.season_fall
      - input_boolean.season_winter
      - input_boolean.season_spring
1 Like