Skip to content

Updated handling to resolve TextReadEntityType passed as unit_of_measurement causing recorder statistics failure#419

Open
kcoffau wants to merge 4 commits intosfstar:mainfrom
kcoffau:main
Open

Updated handling to resolve TextReadEntityType passed as unit_of_measurement causing recorder statistics failure#419
kcoffau wants to merge 4 commits intosfstar:mainfrom
kcoffau:main

Conversation

@kcoffau
Copy link
Contributor

@kcoffau kcoffau commented Mar 1, 2026

Breaking change

Proposed change

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

When Home Assistant's recorder attempts to compile statistics for Victron text/enum sensor entities (e.g. sensor.victronvebus_microgrid_error), it crashes with:
sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) Error binding parameter 3:
type 'TextReadEntityType' is not supported
[SQL: INSERT INTO statistics_meta (statistic_id, source, unit_of_measurement, ...) VALUES ...]
[parameters: ('sensor.victronvebus_microgrid_error227', 'recorder',
<custom_components.victron.const.TextReadEntityType object at 0x...>, ...)]
This occurs because registerInfo.unit for TextReadEntityType registers holds a TextReadEntityType object rather than a plain string or None. The HA recorder passes this directly to SQLite as a bind parameter, which SQLite cannot handle, causing the statistics task to fail on every cycle. The result is that all history recording stops for the entire HA instance, not just the affected entity.
Root Cause
In async_setup_entry within sensor.py, the VictronEntityDescription is constructed without guarding against non-string unit values for text entities:
python# Before — passes TextReadEntityType object directly
description = VictronEntityDescription(
native_unit_of_measurement=registerInfo.unit, # ← object, not string
state_class=registerInfo.determine_stateclass(), # ← meaningless for text entities
device_class=determine_victron_device_class(...) # ← unit is not a string here
...
)
Fix
Guard all three unit-dependent properties against TextReadEntityType instances, returning None for text entities which have no meaningful numeric unit, state class, or device class:
python# After — safely returns None for text entities
is_text_entity = isinstance(registerInfo.entityType, TextReadEntityType)

description = VictronEntityDescription(
native_unit_of_measurement=registerInfo.unit if not is_text_entity else None,
state_class=registerInfo.determine_stateclass() if not is_text_entity else None,
device_class=determine_victron_device_class(register_name, registerInfo.unit) if not is_text_entity else None,
...
)
Impact

Fixes recorder statistics failure for all users with any text/enum Victron entities present
Restores full HA history recording which is blocked by this error
No functional change to how text entity values are decoded or displayed — only the metadata passed to the recorder changes
Affects any unit ID that exposes text registers (e.g. unit 227 microgrid_error, mode registers, alarm state registers)

Testing
Verified by checking HA logs after applying the patch — Unhandled database error while processing StatisticsTask errors no longer appear and history recording resumes for all entities.
HA Version Confirmed Affected

Home Assistant OS 17.1 / Core 2026.2.3

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • The code has been formatted using Ruff (ruff format custom_components/victron)

kcoffau added 2 commits March 1, 2026 18:33
Added extended entity handling to cater for Venus 3.7.0
Handle TextReadEntityType in Victron sensor description
@Borsty
Copy link

Borsty commented Mar 4, 2026

Much anticipated <3

@GeertFolkertsma
Copy link

I would be quite happy to have this fix, because now the integration is breaking all HA's statistics… Is there anything I can do to help?

@kcoffau
Copy link
Contributor Author

kcoffau commented Mar 15, 2026 via email

@GeertFolkertsma
Copy link

Actually, this is not what caused (or at least) solved the bug in my case; I had to apply #424 to make it work. I can imagine that this fix addresses other, rarer problems—the microgrid error seems to have had a different cause.

Enhanced VictronHub to support TCP keepalive and configurable Modbus retries after transport errors. Updated configuration flow and translations to include new options. Improved error handling for transport-related exceptions during Modbus operations.
@kcoffau
Copy link
Contributor Author

kcoffau commented Mar 23, 2026

Added extra configuration for Modbus TCP resilience. Keep Alive and retries.
helps resolve people with modbus drops for whatever reason, as seen in #421

@kcoffau
Copy link
Contributor Author

kcoffau commented Mar 23, 2026

@sfstar for your review.

@kcoffau
Copy link
Contributor Author

kcoffau commented Mar 23, 2026

X Options

@kcoffau kcoffau mentioned this pull request Mar 23, 2026
Added advanced Modbus TCP settings section with details on TCP Keep Alive and Modbus TCP Retries.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

History Errors Venus 3.7 - Unhandled database error while processing task StatisticsTask

3 participants