Error homeassistant.components.recorder:'charmap' codec can't encode character

Hi all, first post for me :slight_smile:
Since a couple of weeks i see two recurrent errors in my logs about Recorder.

Logger: homeassistant.components.recorder
Source: components/recorder/__init__.py:526
Integration: Recorder (documentation, issues)
First occurred: 15:59:53 (34 occurrences)
Last logged: 16:09:06

Error executing query: 'charmap' codec can't encode character '\U0001f44d' in position 86: character maps to <undefined>

and

Logger: homeassistant.components.recorder
Source: components/recorder/__init__.py:509
Integration: Recorder (documentation, issues)
First occurred: 15:59:53 (34 occurrences)
Last logged: 16:09:06

Error saving events: 'charmap' codec can't encode character '\U0001f44d' in position 86: character maps to <undefined>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/recorder/__init__.py", line 454, in _commit_event_session_or_retry
    self._commit_event_session()
  File "/usr/src/homeassistant/homeassistant/components/recorder/__init__.py", line 509, in _commit_event_session
    self.event_session.flush()
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 2540, in flush
    self._flush(objects)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 2682, in _flush
    transaction.rollback(_capture_exception=True)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
    compat.raise_(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 2642, in _flush
    flush_context.execute()
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 419, in execute
    n.execute_aggregate(self, set_)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 697, in execute_aggregate
    persistence.save_obj(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 239, in save_obj
    _emit_insert_statements(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 1135, in _emit_insert_statements
    result = cached_connections[connection].execute(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1011, in execute
    return meth(self, multiparams, params)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/sql/elements.py", line 298, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1124, in _execute_clauseelement
    ret = self._execute_context(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1316, in _execute_context
    self._handle_dbapi_exception(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1514, in _handle_dbapi_exception
    util.raise_(exc_info[1], with_traceback=exc_info[2])
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1276, in _execute_context
    self.dialect.do_execute(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 609, in do_execute
    cursor.execute(statement, parameters)
  File "/usr/local/lib/python3.8/site-packages/MySQLdb/cursors.py", line 199, in execute
    args = tuple(map(db.literal, args))
  File "/usr/local/lib/python3.8/site-packages/MySQLdb/connections.py", line 280, in literal
    s = self.string_literal(o.encode(self.encoding))
  File "/usr/local/lib/python3.8/encodings/cp1252.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f44d' in position 86: character maps to <undefined>

I use MariaDB locally with HomeAssistant using the addon.
I don’t remember on what version i saw this issue the first time, but my current configuration is:

System Health

version: core-2021.2.3
installation_type: Home Assistant OS
dev: false
hassio: true
docker: true
virtualenv: false
python_version: 3.8.7
os_name: Linux
os_version: 5.4.94
arch: x86_64
timezone: Europe/Rome

host_os: Home Assistant OS 5.11
update_channel: stable
supervisor_version: supervisor-2021.02.6
docker_version: 19.03.13
healthy: true
supported: true
board: ova
supervisor_api: ok
version_api: ok
installed_addons: 	Check Home Assistant configuration (3.6.0), Samba share (9.3.0), Terminal & SSH (9.0.1), MariaDB (2.2.1), Mosquitto broker (5.1), Zigbee2mqtt (1.17.1), Nginx Proxy Manager (0.10.0), Home Assistant Google Drive Backup (0.103.1), Network UPS Tools (0.5.0), motionEye (0.11.0), AppDaemon 4 (0.4.3), Visual Studio Code (3.0.3), File editor (5.2.0), ESPHome (1.16.0), phpMyAdmin (0.2.0)

Have you ever seen this error?

Thanks

Sounds like an unicode error. I am not aware how add-ons work, but have you set the connection to MySQL to utf8 in the connection string, as it is described on the recorder integration page?

	mysql://user:password@SERVER_IP/DB_NAME?charset=utf8mb4
2 Likes

mmmm, actually no. I configured MariaDB many months ago and I was pretty sure the config was rights, in fact the error showed only recently.
Anyway i added the unicode setting and restarted.
For now no error anymore.

I’ll wait for a couple of days and give you final feedback.
Thanks for your input.

This solved my issue. Thanks :slight_smile:

2 Likes

I had same issue, odd as been using the MariaDB without issue for several years, only last few days have I had this issue.

Thanks for posting this and helping me sort it.

1 Like