AssertionError in helpers/template.py

Anyone have ideas how to provide more context for this error? It happens seemingly at random, but when it does it really blows up my log and then my HA instance (see over 10k log entries in under 2 mins).

I keep tweaking templates thinking I’ve fixed it and eventually it pops up again. But when it happens and I catch it soon enough, I can just reboot my instance and everything’s good again for a while.

Logger: homeassistant
Source: helpers/template.py:687
First occurred: 10:31:40 AM (10900 occurrences)
Last logged: 10:33:14 AM

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/automation/init.py”, line 637, in async_trigger
and not self._cond_func(variables)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/components/automation/init.py”, line 1014, in if_action
if check(hass, variables) is False:
^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/helpers/condition.py”, line 179, in wrapper
result = condition(hass, variables)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/helpers/condition.py”, line 787, in template_if
return async_template(hass, value_template, variables)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/helpers/condition.py”, line 767, in async_template
info = value_template.async_render_to_info(variables, parse_result=False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 687, in async_render_to_info
assert self.hass and _render_info.get() is None
AssertionError

I’m having similar issues and it’s driving me nuts!!

Logger: homeassistant
Source: helpers/template.py:687
First occurred: 8:30:13 AM (49767 occurrences)
Last logged: 8:37:24 AM

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/automation/init.py”, line 637, in async_trigger
and not self._cond_func(variables)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/components/automation/init.py”, line 1014, in if_action
if check(hass, variables) is False:
^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/helpers/condition.py”, line 179, in wrapper
result = condition(hass, variables)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/helpers/condition.py”, line 787, in template_if
return async_template(hass, value_template, variables)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/helpers/condition.py”, line 767, in async_template
info = value_template.async_render_to_info(variables, parse_result=False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 687, in async_render_to_info
assert self.hass and _render_info.get() is None
AssertionError

I hear you. Mine will sometimes go weeks without the issue, making me think I’ve fixed it. But nope.

I have set up the following, in case it helps with the madness. So far it has caught the issue 100% of the time.

sensor: 
  - platform: folder
    folder: /config
    filter: home-assistant.log

  - platform: statistics
    name: "Log Size Recent Change"
    unique_id: 33c8413e-25ce-40a6-9a56-7709d53b52c1
    entity_id: sensor.config
    state_characteristic: change
    max_age:
      minutes: 30
    precision: 0

automation:
- id: '1709930553428'
  alias: Log File Size Increasing Rapidly Notification
  description: ''
  trigger:
  - platform: numeric_state
    entity_id:
    - sensor.log_size_recent_change
    above: 1
    value_template: '{{ state.state | int(0) }}'
  action:
  - service: notify.email_me
    metadata: {}
    data:
      title: Home Assistant Log Increased {{ states("sensor.log_size_recent_change")
        }} MB
      message: The Home Assistant Log File recently increased {{ states("sensor.log_size_recent_change")
        }} MB.  Please check whether something may be wrong
  mode: single
1 Like

The AssertionError should normally not be reachable, which indicates you have an integration doing non-threadsafe operations on asyncio objects which is corrupting the internal asyncio state.

When an integration modifies the event loop internals from another thread the behavior is undefined and will usually eventually lead to a crash.

If you put asyncio in debug mode, it will be able to detect some of these bad calls and maybe even block them. You can use the profiler service to do that:
https://www.home-assistant.io/integrations/profiler/#service-profilerset_asyncio_debug

If you can’t get far enough along you can also enable debugpy: in configuration.yaml instead (but its much slower)
https://www.home-assistant.io/integrations/debugpy/

2 Likes

Thank you sir, I will try that, and will report back if I figure out anything.