Skip to content

Non-serializable attributes break automation traces: TypeError: keys must be str, int, float, bool or None #147416

Open
@davidrapan

Description

@davidrapan

The problem

The issue occurs during automation trace view if some attributes of the entities used within automation triggers for example contain non-serializable types such as datetime.datetime:

Logger: homeassistant.components.websocket_api.http.connection
Source: components/trace/websocket_api.py:79
integration: Home Assistant WebSocket API ([documentation](https://www.home-assistant.io/integrations/websocket_api), [issues](https://github.com/home-assistant/core/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+websocket_api%22))
First occurred: 10:21:52 PM (12 occurrences)
Last logged: 10:41:36 PM

[281472370093216] Error handling message: Unknown error (unknown_error) David from 192.168.144.155 (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0)
[281472316443200] Error handling message: Unknown error (unknown_error) David from 192.168.144.155 (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0)
[281472237024288] Error handling message: Unknown error (unknown_error) David from 192.168.144.155 (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 28, in _handle_async_response
    await func(hass, connection, msg)
  File "/usr/src/homeassistant/homeassistant/components/trace/websocket_api.py", line 79, in websocket_trace_get
    json.dumps(message, cls=ExtendedJSONEncoder, allow_nan=False)
    ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
          ~~~~~~^^^^^
  File "/usr/local/lib/python3.13/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/local/lib/python3.13/json/encoder.py", line 261, in iterencode
    return _iterencode(o, 0)
TypeError: keys must be str, int, float, bool or None, not datetime.datetime

There is an inconsistency of handling (reading of entity state and attributes) because some flows are casting these types to str (for example, displaying entity details -> Attributes which is using isoformat() directly, since the default separator 'T' is used while __str__ cast would call isoformat(sep=' ')) and some do not (automation trace view), and therefore fail.

What version of Home Assistant Core has the issue?

core-2025.6.1

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Troubleshooting automations

Link to integration documentation on our website

https://www.home-assistant.io/integrations/trace

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions