Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template sensor value_template will generate 'no default' errors if availability_template renders false. #57287

Closed
ghost opened this issue Oct 7, 2021 · 23 comments

Comments

@ghost
Copy link

ghost commented Oct 7, 2021

The problem

With templates sensors, I used to be able to do input validation exclusively via the availability_template: template feature.

Prior to 2021.10 if availability_template renders false, what value_template renders to was irrelevant because availability_template took precedence and forced the state to unavailable. The rendering of value_template at least from the user's perspective was seemingly blocked.

After 2021.10 if availability_template renders false value_template is still rendered and checked for any filter default value errors and logged when the expected behavior is to completely disregard the rendered value or not render the template at all.

This new behavior requires input validation/default filter values to be added to value_template for the sole purpose of avoiding spamming the log when availability_template was enough to handle error checking on its own.

What is version of Home Assistant Core has the issue?

2021.10.0

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Core

Integration causing the issue

template

Link to integration documentation on our website

No response

Example YAML snippet

- platform: template
    sensors:
      br_humidity_delta:
        friendly_name: "Bedroom Humidity Delta"
        unit_of_measurement: 'Δ%rh'
        value_template: "{{ (states('sensor.bedroom_humidity')|float - states('sensor.upstairs_relative_humidity')|float) |round(1) }}"
        availability_template: "{{states('sensor.bedroom_humidity')|is_number and states('sensor.upstairs_relative_humidity')|is_number}}"

Anything in the logs that might be useful for us?

Logger: homeassistant.helpers.template
Source: helpers/template.py:1210
First occurred: 15:49:56 (2 occurrences)
Last logged: 15:49:56

Template warning: 'float' got invalid input 'unavailable' when rendering template '{{ (states('sensor.bedroom_humidity')|float - states('sensor.upstairs_relative_humidity')|float) |round(1) }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2021.12

Additional information

No response

@mbo18
Copy link
Contributor

mbo18 commented Oct 8, 2021

I’m using the new way of how to do templates and I have the same behavior. It’s like the state: is rendered before the availability: is checked

Error:
Template warning: 'float' got invalid input 'unavailable' when rendering template '{{ states('sensor.cave_th_humidity')|float >= 60 and states('sensor.cave_th_humidity')|float >= (states('sensor.jardin_th_humidity')|float + 5) }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2021.12

Template warning: 'float' got invalid input 'unavailable' when rendering template '{{ (states('sensor.salon_th_pressure')|float * (1 - ((0.0065 * 160) / (states('sensor.salon_th_temperature')|float + (0.0065 * 160) + 273.15)) )** -5.257)|round(1) }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2021.12

Template warning: 'float' got invalid input 'unknown' when rendering template '{{ states('sensor.cave_th_humidity')|float >= 60 and states('sensor.cave_th_humidity')|float >= (states('sensor.jardin_th_humidity')|float + 5) }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2021.12

Code:

- binary_sensor:
    - name: "Ventilo Cave"
      unique_id: XXX
      device_class: moving
      state: >-
        {{ states('sensor.cave_th_humidity')|float >= 60
          and states('sensor.cave_th_humidity')|float >= 
            (states('sensor.jardin_th_humidity')|float + 5) }}
      availability: >-
        {{ states('sensor.cave_th_humidity') not in ['none', 'unknown', 'unavailable']
          and states('sensor.jardin_th_humidity') not in ['none', 'unknown', 'unavailable'] }}

@mbo18
Copy link
Contributor

mbo18 commented Oct 8, 2021

@emontnemery Sorry to tag you here but you were the one working on #56453

Is there something we can do? state should not be rendered/tested if availability is false?

@Salvora
Copy link

Salvora commented Oct 9, 2021

exactly the same thing happening with me. Logically, if Availability is rendered true, state should not be rendered.

@probot-home-assistant
Copy link

Hey there @PhracturedBlue, @tetienne, @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (template) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)


template documentation
template source
(message by IssueLinks)

@emontnemery
Copy link
Contributor

The availability template does not block rendering of other templates, it just means the entity's state will be set to unavailable if the availability template returns False.
It seems reasonable to suppress rendering of other templates if the availability template returns False, but not sure if that can be implemented in a simple way.

@koying
Copy link
Contributor

koying commented Oct 24, 2021

I gave it a shot.
The different templates are rendered individually (via a list of _attr_* ), so there is no practical means to avoid them being rendered based upon availability, with the current codebase.

@emontnemery
Copy link
Contributor

That's right, it will require some changes to TemplateEntity where the availability template suppresses rendering of other templates when it returns False.
It's doable, but not trivial.

@emontnemery
Copy link
Contributor

This is an attempt of suppressing errors when the availability template doesn't render as True: #58477

@mbo18
Copy link
Contributor

mbo18 commented Nov 12, 2021

@emontnemery I'm still seeing these logs using HA 2021.11.3:

2021-11-12 12:34:23 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'int' got invalid input 'unavailable' when rendering template '{{ states('sensor.entree_motion_illuminance')|int >= 5
   and state_attr('sun.sun', 'elevation')|float > 3 }}' but no default was specified. Currently 'int' will return '0', however this template will fail to render in Home Assistant core 2022.1
2021-11-12 12:34:23 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'float' got invalid input 'unavailable' when rendering template '{{ states('sensor.cave_th_humidity')|float >= 60
  and states('sensor.cave_th_humidity')|float >= 
    (states('sensor.jardin_th_humidity')|float + 5) }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1
2021-11-12 12:34:23 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'float' got invalid input 'unavailable' when rendering template '{{ (states('sensor.salon_th_pressure')|float * (1 - ((0.0065 * 160) 
   / (states('sensor.salon_th_temperature')|float + (0.0065 * 160) + 273.15)))** -5.257)|round(1) }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1
2021-11-12 12:34:23 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'float' got invalid input 'unavailable' when rendering template '{{ (states('sensor.salon_th_pressure')|float * (1 - ((0.0065 * 160) 
   / (states('sensor.salon_th_temperature')|float + (0.0065 * 160) + 273.15)))** -5.257)|round(1) }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1

@emontnemery
Copy link
Contributor

@mbo18 OK, what's the configuration of the template entity which complains?

@mbo18
Copy link
Contributor

mbo18 commented Nov 12, 2021

Not all but these 3 for sure:

- binary_sensor:
    - name: "Light Outside"
      unique_id: ace5aa7a-fe98-4305-ba08-e451a3ac5849
      device_class: light
      state: >-
        {{ states('sensor.entree_motion_illuminance')|int >= 5
           and state_attr('sun.sun', 'elevation')|float > 3 }}
      availability: >-
        {{ states('sensor.entree_motion_illuminance') not in ['none', 'unknown', 'unavailable']
           and states('sun.sun') not in ['none', 'unknown', 'unavailable'] }}
      attributes:
        Entree_Illuminance: "{{ states('sensor.entree_motion_illuminance') }}"
        Sun_elevation: "{{ state_attr('sun.sun', 'elevation') }}"

    - name: "Ventilo Cave"
      unique_id: 55dc42f8-7562-4877-96fc-c8c533225380
      device_class: moving
      state: >-
        {{ states('sensor.cave_th_humidity')|float >= 60
          and states('sensor.cave_th_humidity')|float >= 
            (states('sensor.jardin_th_humidity')|float + 5) }}
      availability: >-
        {{ states('sensor.cave_th_humidity') not in ['none', 'unknown', 'unavailable']
          and states('sensor.jardin_th_humidity') not in ['none', 'unknown', 'unavailable'] }}

- sensor:
    - name: salon_th_pressure_msl
      unique_id: 7d46f607-99fe-47c3-b018-1c14b2bb2899
      unit_of_measurement: "hPa"
      device_class: pressure
      state_class: measurement
      state: >-
        {{ (states('sensor.salon_th_pressure')|float * (1 - ((0.0065 * 160) 
           / (states('sensor.salon_th_temperature')|float + (0.0065 * 160) + 273.15)))** -5.257)|round(1) }}
      availability: >-
        {{ states('sensor.salon_th_pressure') not in ['none', 'unknown', 'unavailable'] 
          and states('sensor.salon_th_temperature') not in ['none', 'unknown', 'unavailable'] }}

@emontnemery
Copy link
Contributor

Can you enable these logs and attach a log when it happens again:

logger:                      
  default: info              
  logs:                      
    homeassistant.core: debug
    homeassistant.components.template: debug
    homeassistant.helpers.event: debug

@emontnemery
Copy link
Contributor

I can't reproduce the problem
Can you check that:

  • You're really on Home Assistant Core 2021.11
  • You don't have the bad templates copied somewhere else in the configuration. A good way to test would be to change the templates, and verify that the errors go away
  • You have no local modification of Home Assistant Core, or some custom component which is overriding any Home Assistant Core code

@mbo18
Copy link
Contributor

mbo18 commented Nov 12, 2021

It happen each time I restart HA. I'm sure that I'm currently running latest release of HA (updated this morning to 2021.11.3 on HAOS 6.6). I do not use custom components nor custom frontend code. I have no local modifications of HA.
I have no template in my conf except in 1 file. I've modified the templates and logs changed accordingly and I can confirm that I do not have bad templates
I have the log you asked. It's quite long, is there any personal information I need to remove before dropping it in GitHub?

@gfrancesco
Copy link

I have the same problem with 2021.11.3.
Even when the availability template for a template sensor evaluate to false, I get the result of the state template for the sensor, and not unavailable.

Example:

availability: "{{ states('sensor.inverter_1') not in ['unavailable', 'unknown', 'none'] }}"
state: "{{ states('sensor.inverter_1').split(',')[0] | int(default='NaN') }}"

The resulting state of the sensor is NaN, even when the state of sensor.inverter_1 is unavailable.

@emontnemery
Copy link
Contributor

@mbo18 please contact me on discord, emontnemery#6618, if you don't want to publish the log here

@emontnemery
Copy link
Contributor

@gfrancesco Can you collect and share a log too please?

@gfrancesco
Copy link

Below the log lines that refer to sensor.inverter_1, I cut the rest to avoid posting hundreds of lines, but let me know if you need further info.

I'll just give you an overview of the sensors involved:

sensor.inverter_1 is a modbus sensors, it's value is something like this 2,0,0,0,0,0,0,0,0,7,154,0,0,0,0,0,0,0,0,7,154
I split the values returned by sensor.inverter_1 using a template, to extract the voltage, power, energy counter, etc, here's the code:

- name:  "Inv-1 E"
  state: "{{ states('sensor.inverter_1').split(',')[0] | int(default='NaN') }}"
- name:  "Inv-1 V DC"
  state: "{{ states('sensor.inverter_1').split(',')[1] | int(default='NaN') }}"
- name:  "Inv-1 I DC"
  state: "{{ states('sensor.inverter_1').split(',')[2] | int(default='NaN') }}"
- name:  "Inv-1 V AC-1"
  state: "{{ states('sensor.inverter_1').split(',')[3] | int(default='NaN') }}"
- name:  "Inv-1 V AC-2"
  state: "{{ states('sensor.inverter_1').split(',')[4] | int(default='NaN') }}"
- name:  "Inv-1 V AC-3"
  state: "{{ states('sensor.inverter_1').split(',')[5] | int(default='NaN') }}"
- name:  "Inv-1 I AC-1"
  state: "{{ states('sensor.inverter_1').split(',')[6] | int(default='NaN') }}"
- name:  "Inv-1 I AC-2"
  state: "{{ states('sensor.inverter_1').split(',')[7] | int(default='NaN') }}"
- name:  "Inv-1 I AC-3"
  state: "{{ states('sensor.inverter_1').split(',')[8] | int(default='NaN') }}"
- name:  "Inv-1 Power Factor"
  state: "{{ states('sensor.inverter_1').split(',')[9] | float(default='NaN') / 1000 }}"
- name:  "Inv-1 P"
  state: "{{ states('sensor.inverter_1').split(',')[10] | float(default='NaN') / 100 }}"
- name:  "Inv-1 f"
  state: "{{ states('sensor.inverter_1').split(',')[11] | float(default='NaN') / 100 }}"

All the template sensors above share the same availability template via this customize glob:

"sensor.inv_1_*":
      availability: >-
        {{ states('sensor.inverter_1') not in ['unavailable', 'unknown', 'none'] }}

When there is no sun, the inverter shuts down, at this point I can see that sensor.inverter_1 turns into unavailable from Developer Tools.
In the logs you can see errors like jinja2.exceptions.UndefinedError: list object has no element 1 that tells me that despite sensor.inverter_1 being unavailable HA is still trying to compute the template sensors state. The same error is raised for all the other template sensors, however the first one ([0]) doesn't raise any error and prints the default value NaN. All the other sensors ([1-11]) print unavailable, but from what I can understand that's the result of the error processing the state template, and does not come from the availability template of the sensors, otherwise the first one should also be unavailable and there should be no errors in the log. I'm not sure this is the reason, but from the logs what seems to happen is that the state template is evaluated even when the availability template returns False.

2021-11-16 19:12:36 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[0] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': False, 'entities': {'sensor.inverter_1'}, 'domains': set(), 'time': False}
2021-11-16 19:12:36 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[0] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': False, 'entities': {'sensor.inverter_1'}, 'domains': set(), 'time': False}
2021-11-16 19:12:36 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.inv_1_e, old_state=<state sensor.inv_1_e=unknown; friendly_name=Inv-1 E, unit_of_measurement=kWh, device_class=energy, availability={{ states('sensor.inverter_1') not in ['unavailable', 'unknown', 'none'] }} @ 2021-11-16T18:12:34.275940+00:00>, new_state=<state sensor.inv_1_e=NaN; friendly_name=Inv-1 E, unit_of_measurement=kWh, device_class=energy, availability={{ states('sensor.inverter_1') not in ['unavailable', 'unknown', 'none'] }} @ 2021-11-16T18:12:36.017584+00:00>>
2021-11-16 19:12:36 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{{ states('sensor.inverter_1').split(',')[1] | int(default='NaN') }}")
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 398, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1698, in _render_with_context
return template.render(**kwargs)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1304, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1539, in forgiving_int_filter
result = jinja2.filters.do_int(value, default=default, base=base)
File "/usr/local/lib/python3.9/site-packages/jinja2/filters.py", line 980, in do_int
return int(value)
jinja2.exceptions.UndefinedError: list object has no element 1
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 514, in async_render_to_info
render_info._result = self.async_render(variables, strict=strict, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 400, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: list object has no element 1
2021-11-16 19:12:36 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[1] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:12:36 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[1] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:12:36 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: list object has no element 1') while processing template 'Template("{{ states('sensor.inverter_1').split(',')[1] | int(default='NaN') }}")' for attribute '_attr_native_value' in entity 'sensor.inv_1_v_dc'
2021-11-16 19:12:36 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.inv_1_v_dc, old_state=<state sensor.inv_1_v_dc=unknown; friendly_name=Inv-1 V DC, unit_of_measurement=V, device_class=voltage, availability={{ states('sensor.inverter_1') not in ['unavailable', 'unknown', 'none'] }} @ 2021-11-16T18:12:34.276413+00:00>, new_state=<state sensor.inv_1_v_dc=unavailable; friendly_name=Inv-1 V DC, unit_of_measurement=V, device_class=voltage, availability={{ states('sensor.inverter_1') not in ['unavailable', 'unknown', 'none'] }} @ 2021-11-16T18:12:36.030211+00:00>>

2021-11-16 19:20:42 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[1] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:20:42 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[2] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:20:42 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[3] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:20:42 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[4] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:20:42 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[5] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:20:42 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[6] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:20:42 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[7] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:20:42 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[8] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:20:42 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[9] | float(default='NaN') / 1000 }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:20:42 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[10] | float(default='NaN') / 100 }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:20:42 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[11] | float(default='NaN') / 100 }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}

2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template update {{ states('sensor.inverter_1').split(',')[1] | int(default='NaN') }} triggered by event: <Event state_changed[L]: entity_id=sensor.s2_d1, old_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,7,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:09.035482+00:00>, new_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,6,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:39.256628+00:00>>
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[1] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template update {{ states('sensor.inverter_1').split(',')[4] | int(default='NaN') }} triggered by event: <Event state_changed[L]: entity_id=sensor.s2_d1, old_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,7,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:09.035482+00:00>, new_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,6,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:39.256628+00:00>>
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[4] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template update {{ states('sensor.inverter_1').split(',')[2] | int(default='NaN') }} triggered by event: <Event state_changed[L]: entity_id=sensor.s2_d1, old_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,7,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:09.035482+00:00>, new_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,6,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:39.256628+00:00>>
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[2] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template update {{ states('sensor.inverter_1').split(',')[3] | int(default='NaN') }} triggered by event: <Event state_changed[L]: entity_id=sensor.s2_d1, old_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,7,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:09.035482+00:00>, new_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,6,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:39.256628+00:00>>
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[3] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template update {{ states('sensor.inverter_1').split(',')[5] | int(default='NaN') }} triggered by event: <Event state_changed[L]: entity_id=sensor.s2_d1, old_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,7,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:09.035482+00:00>, new_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,6,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:39.256628+00:00>>
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[5] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template update {{ states('sensor.inverter_1').split(',')[6] | int(default='NaN') }} triggered by event: <Event state_changed[L]: entity_id=sensor.s2_d1, old_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,7,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:09.035482+00:00>, new_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,6,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:39.256628+00:00>>
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[6] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template update {{ states('sensor.inverter_1').split(',')[7] | int(default='NaN') }} triggered by event: <Event state_changed[L]: entity_id=sensor.s2_d1, old_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,7,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:09.035482+00:00>, new_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,6,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:39.256628+00:00>>
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[7] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template update {{ states('sensor.inverter_1').split(',')[8] | int(default='NaN') }} triggered by event: <Event state_changed[L]: entity_id=sensor.s2_d1, old_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,7,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:09.035482+00:00>, new_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,6,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:39.256628+00:00>>
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[8] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template update {{ states('sensor.inverter_1').split(',')[9] | float(default='NaN') / 1000 }} triggered by event: <Event state_changed[L]: entity_id=sensor.s2_d1, old_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,7,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:09.035482+00:00>, new_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,6,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:39.256628+00:00>>
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[9] | float(default='NaN') / 1000 }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template update {{ states('sensor.inverter_1').split(',')[10] | float(default='NaN') / 100 }} triggered by event: <Event state_changed[L]: entity_id=sensor.s2_d1, old_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,7,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:09.035482+00:00>, new_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,6,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:39.256628+00:00>>
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[10] | float(default='NaN') / 100 }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template update {{ states('sensor.inverter_1').split(',')[11] | float(default='NaN') / 100 }} triggered by event: <Event state_changed[L]: entity_id=sensor.s2_d1, old_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,7,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:09.035482+00:00>, new_state=<state sensor.s2_d1=2,0,0,0,0,0,0,0,0,6,154,0,0,0,0,0,0,0,0,7,154; friendly_name=S2-D1 @ 2021-11-16T18:22:39.256628+00:00>>
2021-11-16 19:23:37 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[11] | float(default='NaN') / 100 }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}

@emontnemery
Copy link
Contributor

All the template sensors above share the same availability template via this customize glob

You can't use customize.yaml like that, whatever is in customize.yaml is applied on top of the state from the integration, so the template sensors don't know or care about it.
Please try to add the availability template to each sensor instead.

@gfrancesco
Copy link

Didn't know about that, is it documented somewhere?
Anyway, I moved the availability template to the sensors, like this:

- name:         "Inv-1 E"
  state:        "{{ states('sensor.inverter_1').split(',')[0] | int(default='NaN') }}"
  availability: "{{ states('sensor.inverter_1') not in ['unavailable', 'unknown', 'none'] }}"

The first sensor looks like it works (no more NaN printed), but for the others ([1-11]), I still get similar error messages complaining that there is no element NN in the list.

2021-11-17 19:14:00 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{{ states('sensor.inverter_1').split(',')[1] | int(default='NaN') }}")
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 398, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1698, in _render_with_context
return template.render(**kwargs)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1304, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1539, in forgiving_int_filter
result = jinja2.filters.do_int(value, default=default, base=base)
File "/usr/local/lib/python3.9/site-packages/jinja2/filters.py", line 980, in do_int
return int(value)
jinja2.exceptions.UndefinedError: list object has no element 1
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 514, in async_render_to_info
render_info._result = self.async_render(variables, strict=strict, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 400, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: list object has no element 1
2021-11-17 19:14:00 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1') not in ['unavailable', 'unknown', 'none'] }}"), variables=None, rate_limit=None), TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[1] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-17 19:14:00 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1') not in ['unavailable', 'unknown', 'none'] }}"), variables=None, rate_limit=None), TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[1] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-17 19:14:00 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.inv_1_v_dc, old_state=<state sensor.inv_1_v_dc=unknown; friendly_name=Inv-1 V DC, unit_of_measurement=V, device_class=voltage @ 2021-11-17T18:13:59.258006+00:00>, new_state=<state sensor.inv_1_v_dc=unavailable; friendly_name=Inv-1 V DC, unit_of_measurement=V, device_class=voltage @ 2021-11-17T18:14:00.470161+00:00>>

@emontnemery
Copy link
Contributor

emontnemery commented Nov 23, 2021

Please share a log showing both the state change of sensor.inverter_1 leading to the error and the actual error.
Also, do you still see the error multiple times, or just once at startup?

@gfrancesco
Copy link

Here is a state change during startup. Starts as None, then unknownand then reads a good value and then switches to unavailable for around 30s before the good readings return.
As you can see when it goes into unavailable all the template errors are fired (I posted only the first couple).

As far as I can see it is only raising this errors on HA startup, I looked at when the inverter shuts off at sunset (state goes into unavailable) and couldn't find those errors anymore. 🤷

2021-11-23 12:56:38 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.inverter_1, old_state=None, new_state=<state sensor.inverter_1=unknown; friendly_name=Inverter-1 @ 2021-11-23T11:56:38.067736+00:00>>
2021-11-23 12:56:38 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.inverter_1, old_state=<state sensor.inverter_1=unknown; friendly_name=Inverter-1 @ 2021-11-23T11:56:38.067736+00:00>, new_state=<state sensor.inverter_1=1749326,600,190,161,162,156,238,238,238,1000,11037,4998; friendly_name=Inverter-1 @ 2021-11-23T11:56:38.068269+00:00>>
2021-11-23 12:56:39 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.inverter_1, old_state=<state sensor.inverter_1=1749326,600,190,161,162,156,238,238,238,1000,11037,4998; friendly_name=Inverter-1 @ 2021-11-23T11:56:38.068269+00:00>, new_state=<state sensor.inverter_1=unavailable; friendly_name=Inverter-1 @ 2021-11-23T11:56:39.114437+00:00>>
2021-11-23 12:56:39 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1') not in ['unavailable', 'unknown', 'none'] }}"), variables=None, rate_limit=None), TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[0] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': False, 'entities': {'sensor.inverter_1'}, 'domains': set(), 'time': False}
2021-11-23 12:56:39 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1') not in ['unavailable', 'unknown', 'none'] }}"), variables=None, rate_limit=None), TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[0] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': False, 'entities': {'sensor.inverter_1'}, 'domains': set(), 'time': False}
2021-11-23 12:56:39 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.inv_1_e, old_state=<state sensor.inv_1_e=unknown; friendly_name=Inv-1 E, unit_of_measurement=kWh, device_class=energy @ 2021-11-23T11:56:37.061672+00:00>, new_state=<state sensor.inv_1_e=unavailable; friendly_name=Inv-1 E, unit_of_measurement=kWh, device_class=energy @ 2021-11-23T11:56:39.440122+00:00>>
2021-11-23 12:56:39 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{{ states('sensor.inverter_1').split(',')[1] | int(default='NaN') }}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 398, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1698, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1539, in forgiving_int_filter
    result = jinja2.filters.do_int(value, default=default, base=base)
  File "/usr/local/lib/python3.9/site-packages/jinja2/filters.py", line 980, in do_int
    return int(value)
jinja2.exceptions.UndefinedError: list object has no element 1

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 514, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 400, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: list object has no element 1
2021-11-23 12:56:39 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1') not in ['unavailable', 'unknown', 'none'] }}"), variables=None, rate_limit=None), TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[1] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-23 12:56:39 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{ states('sensor.inverter_1') not in ['unavailable', 'unknown', 'none'] }}"), variables=None, rate_limit=None), TrackTemplate(template=Template("{{ states('sensor.inverter_1').split(',')[1] | int(default='NaN') }}"), variables=None, rate_limit=None)] listens for {'all': True, 'entities': set(), 'domains': set(), 'time': False}
2021-11-23 12:56:39 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.inv_1_v_dc, old_state=<state sensor.inv_1_v_dc=unknown; friendly_name=Inv-1 V DC, unit_of_measurement=V, device_class=voltage, state_class=measurement @ 2021-11-23T11:56:37.062117+00:00>, new_state=<state sensor.inv_1_v_dc=unavailable; friendly_name=Inv-1 V DC, unit_of_measurement=V, device_class=voltage, state_class=measurement @ 2021-11-23T11:56:39.460277+00:00>>
2021-11-23 12:56:39 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{{ states('sensor.inverter_1').split(',')[2] | int(default='NaN') }}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 398, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1698, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1539, in forgiving_int_filter
    result = jinja2.filters.do_int(value, default=default, base=base)
  File "/usr/local/lib/python3.9/site-packages/jinja2/filters.py", line 980, in do_int
    return int(value)
jinja2.exceptions.UndefinedError: list object has no element 2

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 514, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 400, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: list object has no element 2

@emontnemery
Copy link
Contributor

Template errors during startup is fixed by #59827 which will be included in Home Assistant Core 2021.12.

@github-actions github-actions bot locked and limited conversation to collaborators Dec 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants