Logger: "unsupported operand type(s) for +=: 'float' and 'datetime.timedelta'" when reloading template sensors (ALL template sensors broken!)

Hi community,

I need urgent help regarding this error which happens when trying to reload template entities (from the dev tools section):

Logger: homeassistant.components.websocket_api.http.connection
Source: setup.py:490
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 01:52:32 (3 occurrences)
Last logged: 02:19:25

[546186835808] unsupported operand type(s) for +=: 'float' and 'datetime.timedelta'
[547470548208] unsupported operand type(s) for +=: 'float' and 'datetime.timedelta'
[546987027824] unsupported operand type(s) for +=: 'float' and 'datetime.timedelta'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 199, in handle_call_service
    await hass.services.async_call(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1820, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1857, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 874, in admin_handler
    await result
  File "/usr/src/homeassistant/homeassistant/components/template/__init__.py", line 51, in _reload_config
    await async_reload_integration_platforms(hass, DOMAIN, PLATFORMS)
  File "/usr/src/homeassistant/homeassistant/helpers/reload.py", line 53, in async_reload_integration_platforms
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/reload.py", line 98, in _resetup_platform
    await _async_reconfig_platform(platform, root_config[integration_platform])
  File "/usr/src/homeassistant/homeassistant/helpers/reload.py", line 138, in _async_reconfig_platform
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 238, in async_setup
    await self._async_setup_platform(async_create_setup_task)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 299, in _async_setup_platform
    with async_start_setup(hass, [full_name]):
  File "/usr/local/lib/python3.10/contextlib.py", line 142, in __exit__
    next(self.gen)
  File "/usr/src/homeassistant/homeassistant/setup.py", line 490, in async_start_setup
    setup_time[integration] += time_taken
TypeError: unsupported operand type(s) for +=: 'float' and 'datetime.timedelta'

I absolutely don’t get where that points at or originates from.

Template entities render fine when starting HA. :white_check_mark:
But once reloading them manually above error is shown…


…and ALL template entities are not rendering - meaning unavailable!

  1. I compared recent changes to all template entities (all my *.yaml files) and could only find these added lines to three template sensors:

a.
availability: "{{ states('sensor.network_throughput_in_end0') not in ['NULL', 'null', 'none', 'undefined', 'unavailable', 'unknown', ''] }}"
b.
availability: "{{ states('sensor.network_throughput_out_end0') not in ['NULL', 'null', 'none', 'undefined', 'unavailable', 'unknown', ''] }}"
c.

        availability_template: >-
          {{
            states('sensor.plug_a') not in ['NULL', 'null', 'none', 'undefined', 'unavailable', 'unknown', ''] and
            states('sensor.plug_b') not in ['NULL', 'null', 'none', 'undefined', 'unavailable', 'unknown', ''] and
            states('sensor.plug_c') not in ['NULL', 'null', 'none', 'undefined', 'unavailable', 'unknown', ''] and
            states('sensor.plug_d') not in ['NULL', 'null', 'none', 'undefined', 'unavailable', 'unknown', ''] and
            states('sensor.plug_e') not in ['NULL', 'null', 'none', 'undefined', 'unavailable', 'unknown', ''] and
            states('sensor.plug_f') not in ['NULL', 'null', 'none', 'undefined', 'unavailable', 'unknown', ''] and
            states('sensor.plug_g') not in ['NULL', 'null', 'none', 'undefined', 'unavailable', 'unknown', ''] and
            states('sensor.plug_h') not in ['NULL', 'null', 'none', 'undefined', 'unavailable', 'unknown', ''] and
            states('sensor.plug_i') not in ['NULL', 'null', 'none', 'undefined', 'unavailable', 'unknown', ''] and
            states('sensor.plug_j') not in ['NULL', 'null', 'none', 'undefined', 'unavailable', 'unknown', ''] and
            states('sensor.plug_k') not in ['NULL', 'null', 'none', 'undefined', 'unavailable', 'unknown', ''] and
            states('sensor.plug_l') not in ['NULL', 'null', 'none', 'undefined', 'unavailable', 'unknown', '']
          }}

I don’t think that’s relevant, isn’t it.

  1. 2nd change due to a HA restart after 15 days: custom integrations have been updated. Also not relevant as this error is about template entities, right?

  2. 3rd change was adding a device to an integration. Also not related to template entities at all I think.


Could you please assist on how to track down the root cause for this?

That is not producing your error, but you can simplify it. Use this instead:

availability: "{{ has_value('sensor.network_throughput_in_end0') }}"

Your error is occurring somewhere you are trying to add a floating point number to a timedelta object.

Is that really “me” or might this be due to an integration?

I searched for “timedelta” in Studio Code Server and could only find *.py files of integrations.

Interesting. Wondering what “has_value” does in detail (docs? → update: found at 2023.4: Custom template macros, and many more new entity dialogs! - Home Assistant) and why others implement that kind of “difficult” way then… @Sir_Goodenough Add availability template to the new template sensor helper - #3 by Sir_Goodenough.
→ edit: according to 2023.4: Custom template macros, and many more new entity dialogs! - Home Assistant has_value “only” filters unavailable and unknown - right? Is that really sufficient for an availability template?

Now back to topic.

It could indeed be an integration.

I have no idea.

The other simple option |is_number is useful for attributes (has_value() only works for states). An example is listed at the bottom of this community guide:

I might have found the integration causing this mess, which costs me half of the night:

Short protocol:

  • Note: Start time has been updated from 1.1.6 to 1.1.7 recently, but the update only became effective now that HA has been restarted after 15 days of smooth uptime.

  • 03:18 10.03.2024 Reload template sensors works (Tested MULTIPLE times) :white_check_mark: → Start time or Trakt.tv (v0.9) on suspicious list!

  • 03:19 10.03.2024 Start time updated again from v1.1.6 to v1.1.7

  • 03:20 10.03.2024 HA restarted

  • 03:29 10.03.2024 ERROR LOADING AGAIN! :x::x::x:

  • 03:29 10.03.2024 Start time downgraded to v1.1.6 again

  • 03:32 10.03.2024 HA restarted

  • 03:48 10.03.2024 Reloading template sensors works (tested MULTIPLE times) :white_check_mark:

I don’t know what changed in that integration, it is currently my number one suspect. 1.1.6 used timedelta already, not sure why 1.1.7 creates such an issue now:

Report in that custom integration issue is upcoming. Enough of wasted time this night.


Oh yeah. That integration is a mess. Stop using it.

In the beta testing it caused someone’s system to bootloop.

Well on the one hand… it worked for years without any issue. On the other hand… it cost me now almost 3 hours of sleep. Rolled back to 1.1.6, ignored 1.1.7 update, let’s see what @AlexxIT will do about it. Shipping it without any requirement settings in hacs.json is a major mistake nowadays in my opinion. Not everyone will be on the latest HA 2024.3 release.

If there’s another option to get a sensor for how long HA takes to start… feel free to share :slight_smile:

This is what balloob (Founder of Home Assistant) said during the beta testing:

you need to remove the start_time custom component. It no longer works

and that custom component hacks HA so badly, that it prevents HA from starting up

That custom component is really bad. It patches the logging function on one of our loggers which just takes the whole system down when the custom component breaks. And because it reads out internal data that is not meant for external consumption, it broke as that data changed in Refactor integration startup time tracking to reduce overhead by bdraco · Pull Request #110136 · home-assistant/core · GitHub