AssertionError in an automation with state_change trigger

Hello,
since I updated HA from version 2024.1.6 to version 2024.2.1 I have problems with an automation triggered by a change of state; completely randomly after a few hours it starts generating AssertionErrors hundreds of times per second, when I noticed it I had almost 5GB of log files.
The automation in question is the following:

alias: Media players - Event - Backup volume_level
mode: queued
max: 25
trigger:
  - platform: event
    event_type: state_changed
condition:
  - condition: template
    value_template: >-
      {{ trigger.event.data.entity_id.startswith('media_player.') }}
  - condition: template
    value_template: "{{ 'volume_level' in trigger.event.data.new_state.attributes }}"
variables:
  entity_id: "{{ trigger.event.data.entity_id }}"
action:
  - variables:
      volume_level: >-
        {{ trigger.event.data.new_state.attributes['volume_level'] | float(0.5)
        | round(2, default=2) }}
  - service: saver.set_variable
    data:
      name: "{{ entity_id }}.volume_level"
      value: "{{ volume_level }}"

The error log is as follows:

Logger: homeassistant
Source: helpers/template.py:686
First occurred: 20:00:03 (1252 occurrences)
Last logged: 20:00:27

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 1009, 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 686, in async_render_to_info
    assert self.hass and _render_info.get() is None
AssertionError

The line that generates the error is the following:

{{ trigger.event.data.entity_id.startswith('media_player.') }}

I don’t understand why it completely randomly starts to cause problems at a certain point.
I’ve been using that automation for over a year and it has never given me any problems.
Has anyone else encountered the same problem as me?

1 Like

Whoever suggested you do that has played a mean trick on you. That Event Trigger will fire for every state-change produced by every entity in your system.

After it fires, the conditions check if the entity’s domain is media_player and if it was a volume change. Even if the entity isn’t a media_player, a trace is still produced. By default, a maximum of 10 traces are stored for each automation. However, it’s the error message produced by each failed triggering (for every state-change by every entity) that is bloating your log.

I suggest you consider re-designing the trigger.

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/

Its likely a media player integration that is changing state and writing the state change from the wrong thread in this case