Templating changes in 2021.10.0

That’s how it has worked in previous versions. For example, if float can’t convert the supplied value it reports 0. Zero is its implicit default value (a.k.a “fallback” value). If you wanted it to report -1 instead of 0 all you needed to do is use explicitly indicate the desired default value with float(-1).

The difference in the 2021.12.0 version will be that there will no implicit default value and you must explicitly specify it. So if you want the existing behavior, where it reports zero, you must indicate it with float(0).

The current version, 2021.10.0, just gives you a warning that float is missing an explicit default value. It’s part of a two month grace period for everyone to update their templates in preparation for the December release when it becomes mandatory to supply a default value.

Basically, this makes everyone pay closer attention to what their template does when it encounters an unexpected value (like unknown or unavailable or isn’t numeric in some other way).

FWIW, several of the templates I have helped users fix revealed fundamental flaws that were masked by the use of implicit defaults.

1 Like

thx for your swift reply - will change with float(0), BR JJ

Thanks for the answer and I agree that this is making us fix our code. What I was trying to ask if HA is now not using the "standard "Jinja code? The way I read the docs there is a default for int (0) and float (0.0) I know that HA uses other filters (i.e. multiply) that are not in the standard Jinja code. Are they now not using the standard jinja2 filters?
Thanks.

what about this template in a template-entity-row complaining about not setting a default, while I explicitly have set it…:

[homeassistant.helpers.template] Template warning: 'round' got invalid input 'None' when rendering template '{% if states(config.entity) not in ['unknown','unavailable'] and
      states[config.entity] is not none %}
  {% set temp = states('sensor.pond_buiten_sensor_calibrated_temperature')|float(none)|round(2,'Not ready') %}
  {% set dark = 'Licht' if is_state('binary_sensor.dark_outside','off') else 'Donker'%}
  {{relative_time(states[config.entity].last_changed)}} ago, {{dark}} en {{temp}} °C
{% else %} Not yet set {% endif %}' but no default was specified. Currently 'round' will return 'None', however this template will fail to render in Home Assistant core 2021.12

just to make sure it exists Ive even changed to an extra if in the template, like:

    secondary: >
      {% if states[config.entity] is not none %}
        {% if states(config.entity) not in ['unknown','unavailable'] and
              states[config.entity] is not none %}
          {% set temp = states('sensor.pond_buiten_sensor_calibrated_temperature')|float(none)|round(2,'Not ready') %}
          {% set dark = 'Licht' if is_state('binary_sensor.dark_outside','off') else 'Donker'%}
          {{relative_time(states[config.entity].last_changed)}} ago, {{dark}} en {{temp}} °C
        {% else %} Not yet set
        {% endif %}
      {% else %} No entity yet
      {% endif %}

The round filter can take three parameters and the third parameter is for setting the default value. However in this example it appears in the second position so it’s not understood to be the default value.

round(2, 'Not ready')

You can either specify all three parameters, in the correct order, or do this to explicitly indicate the default parameter:

round(2, default='Not ready')
1 Like

thanks Taras, will try and yes I see that now the warning is gone.
very odd though, because I have things like

value_template: >
  {{value|round(0,none)}}

throughout the system, and no warning has been issued anywhere. Must check why…

supplying none to method results in a normal round.

Screenshot from 2021-10-17 11-30-22

You’ll notice it did not report none as the result, plus it generated a warning message:

Logger: homeassistant.helpers.template
Source: helpers/template.py:1210
First occurred: 11:29:22 (1 occurrences)
Last logged: 11:29:22

Template warning: ‘round’ got invalid input ‘junk’ when compiling template ‘value_template: > {{‘junk’|round(0,none)}}’ but no default was specified. Currently ‘round’ will return ‘junk’, however this template will fail to render in Home Assistant core 2021.12

It will report none if defined like this:

Screenshot from 2021-10-17 11-33-51

Or if all three parameters are supplied like this:
Screenshot from 2021-10-17 11-35-18

no warnings on

{{(states('sensor.zp_actuele_opbrengst')|float(0) -
            states('sensor.netto_verbruik')|float(0))|round(2,none)}}

or

{{expand('sensor.zp_actuele_opbrengst','sensor.netto_verbruik')
             |map(attribute='state')|rejectattr('state','in',['unknown','unavailable'])
             |map('float',0)|sum|round(2,none)}}

or even without any default:

      - unique_id: intercom_volume
        name: Intercom volume
        state: >
           {{states('input_number.intercom_volume')|float|round(2)}}

Petro, do I read your post correctly, that when I use round(2,none) it will work ok, defaulting to an unavailable entity, and, when I need to have it show an actual value like ‘Not yet set’ it needs to be the 3 argument, or, use the default= construction?

No warnings because round was supplied with a value it was able to convert.

1 Like

warnings will only appear if it fails to round

1 Like

ok, I see, that makes sense :wink:

you can omit defaults in places that ensure no text-to-xyz conversions happen.

I’m running the dev build and I have a ton of | int without bothering to supply a default because I know they will always be a number that is being converted.

1 Like

They have overridden some of the standard filters. For example, this PR mentions how it overrides the behavior of int

According to the docs I found on jinja both the float and int have defaults set. Not understanding the pr you referenced ? Maybe the jinja docs don’t match the code? Trying to find the correct document to follow is not easy. The template editor in HA references the jinja docs not any HA doc or any PRs.

Override means replaced. There is no default for float. It was 0. Same for int in 2021.11 when it comes out.

Not according to the documents linked in the template editor.

float(value, default=0.0)¶
Convert the value into a floating point number. If the conversion doesn’t work it will return 0.0. You can override this default using the first parameter.

Yes and if you don’t provide a default it will warning and return zero. Again, that’s changing in December and it will error. Meaning no default, this is what the breaking change is.

Not trying to be argumentative but I am not following this at all. According to the jinja docs that are still linked in HA there is a default value for the float and int. I quoted the float above. The default says 0.0. I guess you are saying that the October release rewrote the jinja filter and it removed the default values or I don’t understand what default means in the doc? Not sure why this would be changed? Hopefully someone can change the document linked to the correct information.

This is covered between the breaking changes and the HA template documentation, not the jinja documentation.