thought the issue might also have been that because of this, the system looped in unavailable devices/integrations. I had not guarded the if b.state not in ['unavailable','unknown'] which I now added too.
but, wait, yes, I had changed that. What I hadn’t done yet, was refresh the frontend! I ran into this before, with the template-entity-row card. Log showing an error, of a config version not yet updated/refreshed.
Ive refreshed now, the unavailable sensor has left the markdown, and I’ll restart the HA instance once more.
I could set the default as 0 or None, but in either case it seems like I’ll have another if statement to check for that before trying to call isoformat on it. It seems cumbersome and I’m wondering how I can do this more cleanly.
I’ve read klogg’s post that seems related, but unanswered.
Copy-paste this into the Template Editor and see if it reports the desired result:
{% set f = state_attr('sensor.home_assistant', 'file_list') | sort(reverse=True) | first %}
{{ 'unknown' if f is not defined else (f[-19:-4].replace('_', ' ') | as_datetime).astimezone().isoformat() }}
Hi, i have a warning error with this sensor template:
- platform: template
sensors:
apc2_time_remaining:
friendly_name: 'APC Scrivania Remaining'
value_template: >-
{% set time = (states.sensor.apc2_battery_runtime.state | int) | int %}
{% set minutes = ((time % 3600) / 60) | int %}
{% set hours = ((time % 86400) / 3600) | int %}
{% set days = (time / 86400) | int %}
{%- if time < 60 -%}
meno di un minuto
{%- else -%}
{%- if days > 0 -%}
{{ days }} d
{%- endif -%}
{%- if hours > 0 -%}
{%- if days > 0 -%}
{{ ' ' }}
{%- endif -%}
{{ hours }} h
{%- endif -%}
{%- if minutes > 0 -%}
{%- if days > 0 or hours > 0 -%}
{{ ' ' }}
{%- endif -%}
{{ minutes }} m
{%- endif -%}
{%- endif -%}
In log have this warning:
Logger: homeassistant.helpers.template
Source: helpers/template.py:1291
First occurred: 17 dicembre 2021, 19:44:10 (1 occurrences)
Last logged: 17 dicembre 2021, 19:44:10
Template warning: ‘int’ got invalid input ‘unavailable’ when rendering template ‘{% set time = (states.sensor.apc2_battery_runtime.state | int) | int %} {% set minutes = ((time % 3600) / 60) | int %} {% set hours = ((time % 86400) / 3600) | int %} {% set days = (time / 86400) | int %} {%- if time < 60 -%} meno di un minuto {%- else -%} {%- if days > 0 -%} {{ days }} d {%- endif -%} {%- if hours > 0 -%} {%- if days > 0 -%} {{ ’ ’ }} {%- endif -%} {{ hours }} h {%- endif -%} {%- if minutes > 0 -%} {%- if days > 0 or hours > 0 -%} {{ ’ ’ }} {%- endif -%} {{ minutes }} m {%- endif -%} {%- endif -%}’ but no default was specified. Currently ‘int’ will return ‘0’, however this template will fail to render in Home Assistant core 2022.1
how can i fix the template to make the error disappear?
Thanks
I have read the guide, but honestly, already having problems with the English language, I am Italian, and not having much familiarity with programming, I did not understand how to set the default value. If someone could kindly solve the problem for me I would be very happy.
Would the division not keep creating a float, which Marco is now seeing?
I believe that, for integer division, one must use two slashes instead of the one, like ((time % 3600) // 60). Or just go back to converting it after .
Bringing my rant from here over here, to avoid pissing off nice people like @petro.
I want to understand something. The warning says: “this template will fail to render in Home Assistant core 2022.1”. Does this mean:
a. The template will give a warning/error in the logs and will not have a default value anymore
or
b. The system will refuse to start when it sees such a construct (w/o a default)
?
If a), then I don’t really see the reason for the warning, and I, for my part, won’t need to make any changes, because I don’t want to hide “default” assignments to zero.
If b), that is IMHO a very poor decision, because “fixing” this warning will hide actual bugs.
I still believe this change is not warranted and it’s been implemented as a workaround for a specific problem, but it will affect a lot of HA deployments out there.
the only reason I can see for the change is that there were some users who were getting erroneous results in their templates and had no idea it was happening or why.
I think I would have handled it differently if that was the case.
I think it would have been sufficient to maintain the default “defaults” but also log a warning/error that the template was forced to fall back to the default values.
that way it shows users where (and if) they made a mistake but also provides for using the built-in default if that was already sufficient for what someone needs without the need for extra coding or trying to figure out the complexities of which defaults are needed and the required syntax.
the “make it easy” mantra always seems to be undermined by these types of changes.
It will produce an error instead of a warning. If it happens at startup (likely) you’re entity won’t be created. If it happens randomly but the entity already exists (i.e. after startup), you’ll just get an error.
I have this template sensor with availability template:
template:
- sensor:
- name: Living TV Volume Level
unit_of_measurement: "%"
availability: >-
{{ state_attr("media_player.living_tv", "volume_level") not in
[None, unknown, 'none', 'unknown', 'unavailable'] }}
state: >
{{ (state_attr("media_player.living_tv", "volume_level") * 100)
| int }}
icon: >
{% set vol = states('sensor.living_tv_volume_level')
| int %}
{% set lvl = (( (vol-1) // (100/3) ) | int) + 1 %}
{% set icons = ['off','low','medium','high'] %}
{% if 0 <= lvl <= 3 %}
mdi:volume-{{ icons[lvl] }}
{% else %}
mdi:volume-{{ icons[1] }}
{% endif %}
The log shows this entry:
Logger: homeassistant.helpers.template
Source: helpers/template.py:1291
First occurred: 22:45:38 (5 occurrences)
Last logged: 22:45:53
...
Template warning: 'int' got invalid input 'unknown' when rendering template '{% set vol = states('sensor.living_tv_volume_level') | int %} {% set lvl = (( (vol-1) // (100/3) ) | int) + 1 %} {% set icons = ['off','low','medium','high'] %} {% if 0 <= lvl <= 3 %} mdi:volume-{{ icons[lvl] }} {% else %} mdi:volume-{{ icons[1] }} {% endif %}' but no default was specified. Currently 'int' will return '0', however this template will fail to render in Home Assistant core 2022.1
An availability template does not stop the state template from attempting to resolve. It just masks the result. I think there is a PR in progress to alter this behaviour. Either way your issue is with the icon template that this PR will not address. Just supply a default value for your use of |int in this template.