Templating changes in 2021.10.0

I think it’s a phonetic date

Exactly. Above “composition” gives me today’s date and current time:

Sre, 13. Okt - 20:53

Which would be (in english) : Wed, 13th. Oct - 20:53
If there’s any easier way i’m very open to suggestions. I’m not skilled programmer, i manage to “assemble” thing like this with internet searh&help (from you, guys!) and that’s pretty much it…

Copy-paste this into the Template Editor and see if it reports the date and time in your desired format:

{{'{}, {}. {} - {}'.format(states('sensor.danes')[:3], now().day, states('sensor.mesec_number'), (now().time()|string)[:5])}}
1 Like

Yeeeeah! It works! Thanks a lot! Now I will play a bit with your template in Template Editor to try understand how it works… i like to at least try to understand what i’m doing…

Now only thing left in core’s log is warning for my sensor “sensor.danes”. How do i change this into new format:

    dan:
      friendly_name: "dan"
      icon_template: mdi:calendar-range
      value_template: "{{ float (states('sensor.danes'))}}"

this sensor should give me only day of the week. I have same template for month name, too. It’s easy for “native englishmen”, since they have such names easier available (it’s not needed to convert to local)…

to be honest, i’m not quite sure why “float” is needed for strings…?

EDIT: is this correct? It gives correct result in template editor… I removed [:x]part, because day length is “day dependant”…

{{'{}'.format(states('sensor.danes'),(now().time()|string))}}

I don’t see how it can do that with this template:

{{ float (states('sensor.danes')) }}

The value reported by sensor.danes will be the name of the current weekday from this list:

["Ponedeljek", "Torek", "Sreda", "Četrtek", "Petek", "Sobota", "Nedelja"]

The float function can’t convert non-numeric strings into numbers. When it can’t convert the string it simply reports 0 by default (or whatever default value you specify).

Effectively, you are trying to do this and it will always report 0

{{ float("Sreda") }}

2 posts were split to a new topic: Template help: value_json is undefined

I guess i wrote wrong… with day of the week i meant NAME of that day (as you already figured it out…).
So, if i understand correct float is for conversion to numbers and as such not needed in my case? As i said, i made this on the base of what i found on the internet, and since it worked i left it this way… if this is the case then my question is really in wrong place. Sorry for that, my basic question is how to add default to float…

2 posts were split to a new topic: Time template troubles

FYI - I traced this warning:

2021-10-08 14:03:34 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'float' got invalid input 'None' when rendering template '{{value_json.currentZ|float}}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2021.12

to the “HomeAssistant Discovery” plugin in OctoPrint and have summitted a pull request to fix issue #78.

Can someone help me with this template?

  afvalinfo_kerstboom_next:
    friendly_name: Kerstboom
    entity_picture_template: /local/images/afvalbeheer/afval_papier_small.png
    value_template: >
      {{state_attr('sensor.afvalinfo_kerstboom','days_until_collection_date')}}
    attribute_templates:
      day: >
        {{as_timestamp(strptime(states('sensor.afvalinfo_kerstboom'), '%d-%m-%Y'))
          |timestamp_custom('%A') | default='0'}}
      date: >
        {{as_timestamp(strptime(states('sensor.afvalinfo_kerstboom'),'%d-%m-%Y'))
          |timestamp_custom('%d %B',default='0')}}
      timestamp: >
        {{as_timestamp(strptime(states('sensor.afvalinfo_kerstboom'), '%d-%m-%Y',default='0'))}}        
  • Template warning: ‘as_timestamp’ got invalid input ‘unknown’ when rendering template ‘{{as_timestamp(strptime(states(‘sensor.afvalinfo_kerstboom’), ‘%d-%m-%Y’)) |timestamp_custom(’%A’) | as_timestamp(‘0’)}}’ but no default was specified. Currently ‘as_timestamp’ will return ‘None’, however this template will fail to render in Home Assistant core 2021.12
  • Template warning: ‘timestamp_custom’ got invalid input ‘None’ when rendering template ‘{{as_timestamp(strptime(states(‘sensor.afvalinfo_kerstboom’), ‘%d-%m-%Y’)) |timestamp_custom(’%A’) | as_timestamp(‘0’)}}’ but no default was specified. Currently ‘timestamp_custom’ will return ‘None’, however this template will fail to render in Home Assistant core 2021.12
  • Template warning: ‘strptime’ got invalid input ‘unknown’ when rendering template ‘{{as_timestamp(strptime(states(‘sensor.afvalinfo_’), ‘%d-%m-%Y’)) |timestamp_custom(’%A’) | as_timestamp(‘0’)}}’ but no default was specified. Currently ‘strptime’ will return ‘unknown’, however this template will fail to render in Home Assistant core 2021.12
  • Template warning: ‘as_timestamp’ got invalid input ‘unknown’ when rendering template ‘{{as_timestamp(strptime(states(‘sensor.afvalinfo_’), ‘%d-%m-%Y’)) |timestamp_custom(’%A’) | as_timestamp(‘0’)}}’ but no default was specified. Currently ‘as_timestamp’ will return ‘None’, however this template will fail to render in Home Assistant core 2021.12
  • Template warning: ‘timestamp_custom’ got invalid input ‘None’ when rendering template ‘{{as_timestamp(strptime(states(‘sensor.afvalinfo_’), ‘%d-%m-%Y’)) |timestamp_custom(’%A’) | as_timestamp(‘0’)}}’ but no default was specified. Currently ‘timestamp_custom’ will return ‘None’, however this template will fail to render in Home Assistant core 2021.12

Adding default=‘none’ in both places of this code resolved the warnings for me. Sample config:

value_template: "{{ as_timestamp(state_attr('sensor.backup_state', 'last_backup'), default='none') | timestamp_custom('%m/%d/%Y %I:%M%p', default='none') }}"
1 Like

Hi there, I do get them same warnings but could not find the cause, could anybody help on that? Thanks!

  • Template warning: ‘float’ got invalid input ‘unavailable’ when rendering template ‘{% set daily_min = states(‘sensor.daily_wohnen_temp_min’) | float %} {% set current = states(‘sensor.indoor_temperature’) | float %} {% if daily_min == ‘unknown’ %} 99 {% elif (now().hour == 0 and now().minute == 0) %} {{ current | float }} {% else %} {{ current if current <= daily_min | float else daily_min | float }} {% endif %}’ but no default was specified. Currently ‘float’ will return ‘0’, however this template will fail to render in Home Assistant core 2021.12

and the code is:

  value_template: >-
    {% set daily_min = states('sensor.daily_outdoor_temp_min') | float %}
    {% set current = states('sensor.outdoor_temperature') | float %}
    {% if daily_min == 'unknown' %}
      99
    {% elif (now().hour == 0 and now().minute == 0) %}
      {{ current | float }}
    {% else %}
      {{ current if current <= daily_min | float else daily_min | float }}
    {% endif %}

The message indicates the float filter was given a value of unavailable. That means this returned unavailable

states('sensor.daily_outdoor_temp_min')

float cannot convert unavailable to a numeric value so it will report a default value. Traditionally, float’s default value is 0 but, in the future, you will be obligated to specify a default value. If you fail to provide a default value in the current version (2021.10.0) you get a warning message but starting in 2021.12.0 it will be an error message.

If the value of states('sensor.daily_outdoor_temp_min') is unavailable and you do this:

{% set daily_min = states('sensor.daily_outdoor_temp_min') | float(0) %}

you won’t get a warning message and daily_min will be set to 0 (but you can set the default value to whatever you prefer).

BTW, this line in your template will never be true:

{% if daily_min == 'unknown' %}

daily_min’s value is set by float and it never returns unknown (unless you make that it’s default value).

Does this mean that Jinja had default values for the filters and this was changed on this coming release? I tried to look up default Jinja filter values and could not find any information. I would think if there is a default setting for the filter it should be set and be able to be over written by a user.

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…