Templating changes in 2021.10.0

Nothing wrong with Fortran

yup. :slight_smile:

Thanks once again for all your help Petro - No more errors!

The only thing I changed was

{{ (humidities | sum / humidities | count) | round(2)}}

Just to tidy up the output.

Correct.


@bschatzow
Never used python until Home Assistant. I know just enough now to get by. Lots of online python resources (Google is your friend).

Does it make sense to start with Jinja tutorials?

Probably since most of what is shown in this thread is Jinja2 (with a smattering of python). However, good luck finding Jinja2 tutorials targeted for Home Assistantā€™s implementation of Jinja2.

The official Jinja2 documentation is thorough but dry and not everything it describes is implemented in Home Assistant. I found the most useful section to be the lists of Builtin Filters and Builtin Tests but not all are available in Home Assistant and there are custom filters not mentioned in Jinja2ā€™s documentation.

I donā€™t think they exist. You might get something looking up ansible, not entirely sure what the link is between the 2 but it seems to be more widely used.

duhā€¦ ofc. thanks (being quite for a moment to contemplate my blunder)

.19ā€¦20ā€¦

do we already have consensus on that that would be ā€˜correctyā€™ ?

I mean we can set |float(0) but that would only mean the template always succeeds, not perse the value is useful or even correct . If the value of an mqtt sensor would be unavailable and we float it to default, it still is evaluated as 0, with rather undesired consequencesā€¦

need to default to float(none) after all then?

even worse: ive got a group with power sensors, some of which are turned off , as in dont have a power reading at all, being unavailable. the template sensor returns

Template warning: 'float' got invalid input 'unavailable' when rendering template '{{(expand('group.lights_inside_total_device_power')

on those:

          {{states('sensor.lights_inside_total_device_power')|float +
            states('sensor.lights_outside_total_device_power')|float}}

based on

          {{(expand('group.lights_inside_total_device_power')
             |map(attribute='state')|map('float')|sum)|round(2)}}

where to set a (which) defaultā€¦?
testing this for a reload

|map('float',0)

which should be fine for the individual power sensors, but not for the energy sensors (they are total_increasingā€¦)

also, the all lights power sensor (derived from the expand group template) shouldnā€™t default to 0 either, because it never can/will/should be 0ā€¦

I would hope we do. Like any computer language there are rules, definitions, etc. The problem with most of us (myself included) I really did not understand what the code was doing when I copied different examples and modified them to my system. I would think if you follow the rules and you understand what each statement is doing it ā€œshouldā€ work. If it doesnā€™t I believe with the experts here someone will be able to explain why.
I am still trying to understand what each line in my ā€œcopied codeā€ does. Most of my errors have to do with command line sensors used to show updates on core, system, hacs, etc. Other errors have to do with markdown cards that are using templates. Again, I justed edited them and changed the sensors to my sensors. This worked well for me until this release.

So this templateā€¦

{{ max|round(0,default='none')}}Ā°/{{ min|round(0,default='none')}}Ā°/{{states('sensor.kariong_rain_chance_0')|round(0,default='none')}}%

I previously used round(0) to round to 0 decimal places (I thought) Now I added the default=ā€˜noneā€™ as I donā€™t want it using 0 as the default. It also wouldnā€™t work without the default= part.
In the docs for round it seems that round(x,y,z) is the syntax? Where x is the rounding, y could be floor or ceiling and z is the default but I am only seeing the default in the above examples. Why do I have to use default=?

If you donā€™t provide the second argument, you need to provide the word default so that the code knows itā€™s the value for default.

2 Likes

thanks that simplifies it quite a bit.

btw, no rest allowed: https://github.com/home-assistant/core/pull/57470

only a few left, this being a prominent one in the log:

          {%- set tracker_timestamp = as_timestamp(strptime(states('sensor.afvalwijzer_next_date'),'%d-%m-%Y')) %}
          {%  set months = ['Januari','Februari','Maart','April','Mei','Juni','Juli','Augustus','September','Oktober','November','December'] %}
          {%- set wdays = ['Zondag','Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag','Zaterdag'] %}
          {%- set wday = tracker_timestamp|timestamp_custom('%w')|int %}
          {%- set month = tracker_timestamp |timestamp_custom('%m')|int %}
          {{tracker_timestamp|timestamp_custom(wdays[wday]  ~ ' ' '%-d' ' ' ~ months[month-1])}}

it sees 3 warnings:

2021-10-12 00:53:36 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'strptime' got invalid input 'Geen' when rendering template '{%- set tracker_timestamp = as_timestamp(strptime(states('sensor.afvalwijzer_next_date'),'%d-%m-%Y')) %} {%  set months = ['Januari','Februari','Maart','April','Mei','Juni','Juli','Augustus','September','Oktober','November','December'] %} {%- set wdays = ['Zondag','Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag','Zaterdag'] %} {%- set wday = tracker_timestamp|timestamp_custom('%w')|int %} {%- set month = tracker_timestamp |timestamp_custom('%m')|int %} {{tracker_timestamp|timestamp_custom(wdays[wday]  ~ ' ' '%-d' ' ' ~ months[month-1])}}' but no default was specified. Currently 'strptime' will return 'Geen', however this template will fail to render in Home Assistant core 2021.12
2021-10-12 00:53:36 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'as_timestamp' got invalid input 'Geen' when rendering template '{%- set tracker_timestamp = as_timestamp(strptime(states('sensor.afvalwijzer_next_date'),'%d-%m-%Y')) %} {%  set months = ['Januari','Februari','Maart','April','Mei','Juni','Juli','Augustus','September','Oktober','November','December'] %} {%- set wdays = ['Zondag','Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag','Zaterdag'] %} {%- set wday = tracker_timestamp|timestamp_custom('%w')|int %} {%- set month = tracker_timestamp |timestamp_custom('%m')|int %} {{tracker_timestamp|timestamp_custom(wdays[wday]  ~ ' ' '%-d' ' ' ~ months[month-1])}}' but no default was specified. Currently 'as_timestamp' will return 'None', however this template will fail to render in Home Assistant core 2021.12
2021-10-12 00:53:36 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'timestamp_custom' got invalid input 'None' when rendering template '{%- set tracker_timestamp = as_timestamp(strptime(states('sensor.afvalwijzer_next_date'),'%d-%m-%Y')) %} {%  set months = ['Januari','Februari','Maart','April','Mei','Juni','Juli','Augustus','September','Oktober','November','December'] %} {%- set wdays = ['Zondag','Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag','Zaterdag'] %} {%- set wday = tracker_timestamp|timestamp_custom('%w')|int %} {%- set month = tracker_timestamp |timestamp_custom('%m')|int %} {{tracker_timestamp|timestamp_custom(wdays[wday]  ~ ' ' '%-d' ' ' ~ months[month-1])}}' but no default was specified. Currently 'timestamp_custom' will return 'None', however this template will fail to render in Home Assistant core 2021.12

please help me out where to ad some defaults? Though this mentions the input being invalid, and I am not sure how to handle that. The input is ok however, and currently as intendedā€¦

the only other warningI get is on the DST sensor:

2021-10-12 00:53:37 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'timestamp_local' got invalid input '167 days, 0:00:00' when rendering template '{%- set ns = namespace(previous=2,spring=none,fall=none) %} {%- set today = strptime(states('sensor.date'),'%Y-%m-%d').astimezone().replace(hour=ns.previous) %} {%- for i in range(365) %} {%- set day = (today + timedelta(days=i)).astimezone() %} {%- if ns.previous - day.hour == -1 %} {%- set ns.spring = today + timedelta(days=i)|timestamp_local %} {%- elif ns.previous - day.hour == 1 %} {%- set ns.fall = today + timedelta(days=i)|timestamp_local %} {%- endif %} {%- set ns.previous = day.hour %} {%- endfor %} {{([ns.spring,ns.fall]|min).isoformat()}}' but no default was specified. Currently 'timestamp_local' will return '167 days, 0:00:00', however this template will fail to render in Home Assistant core 2021.12

but that was before I deleted |timestamp_local. Must re-read this thread again to understand why I can now take that out, or why that caused the errorā€¦

I though I understood what to do but I have a simple sensor that is giving a warning.

platform: template
sensors:

  total_distance_mtr_miles:
    friendly_name: "miles_walked"
    unit_of_measurement: 'miles'
#added round(2,default=0) 10/8/2021
    value_template: "{{ states('sensor.total_distance_mtr') | multiply (1/1609.3444) | float(2) | round(2,default=0)}}"
  • Template warning: ā€˜multiplyā€™ got invalid input ā€˜unavailableā€™ when rendering template ā€˜{{ states(ā€˜sensor.total_distance_mtrā€™) | multiply (1/1609.3444) | float(2) | round(2,default=0)}}ā€™ but no default was specified. Currently ā€˜multiplyā€™ will return ā€˜unavailableā€™, however this template will fail to render in Home Assistant core 2021.12
    Tried float (defaultt=0) and it didnā€™t help.

Itā€™s saying that the value multiply received was unavailable. That means the result of states('sensor.total_distance_mtr') was unavailable.

Even if it wasnā€™t unavailable, the sensorā€™s state value is a string and you canā€™t do arithmetic with strings (you have to first convert them to numbers using float or int).

I thought I did with the float? It has worked for several years as is. Is the order wrong in the statement? I tried float | multiply (changed order) and no difference. It is just converting the garmin meters to miles. Why would the Meters be a string?

The float is applied to what comes before it. Thatā€™s how filters work.

what_filter_is_apply_to | filter

Youā€™re applying the filter to

multiply (1/1609.3444) | float(2)

it should be

states('sensor.total_distance_mtr') | float(2) | multiply (1/1609.3444) | round(2,default=0)

also, I think multiply needs a default too.

Also multiply converts strings to floats for you.

soā€¦

states('sensor.total_distance_mtr') | multiply (1/1609.3444, 0) | round(2,default=0)

On multiple you donā€™t need , default=0?

No, multiply only takes 2 arguments. So you donā€™t need to call out the names because you arenā€™t omitting an argument.

See:

the only reason you need to use the word default is when the argument you are trying to set is not the next argument thatā€™s required.

I.e. my_method(arg1, arg2=True, arg3=47) requires 3 arguments named arg1, arg2, and arg3. arg1 is required and arg2 and arg3 are optional, because they have a default value for them. When you omit arg2 and arg3, they auto populate my_method with True and 47. Meaning if you call my_method with my_method(7), arg1 will have 7, arg2 will have True, and arg3 will have 47. Well, what happens when you want to supply my_method with arg3 as 50 instead of 47? Well you have 2 options. Populate all 3 arguments; my_method(7, True, 50) or supply the single argument you want to populate my_method(7, arg3=50).

This is why you see default used and not used all over the place. FYI you can also supply all 3 and it still works; my_method(7, arg2=True, arg3=50).

1 Like