Skip to content

battery_last_replaced sensors crash with "missing timezone information" on HA 2026.4+ #4614

@d-trattner

Description

@d-trattner

Checklist

  • I have restarted Home Assistant after the HACS or manual install.
  • I have read the FAQ's.
  • I have enabled debug logging for my installation.
  • I have filled out the issue template to the best of my ability.
  • This issue only contains 1 issue (if you have multiple issues, open one issue for each issue).
  • This issue is not a duplicate issue of any previous issues..

Battery Notes Version

v3.4.3

Describe the issue

After updating to HA Core 2026.4, all sensor.*_battery_last_replaced entities
crash during setup with:

ValueError: Invalid datetime: sensor.XXX_battery_last_replaced provides state
'2024-08-31 13:27:15.201230', which is missing timezone information

Impact: The error causes the coordinator update to fail, which makes ALL
battery_notes entities unavailable — not just the _last_replaced sensors but
also _battery_plus, _battery_plus_low, and _battery_type entities.
Effectively, the entire integration is broken.

This happens because coordinator.py returns naive datetimes (without timezone)
from the last_replaced and last_reported property getters. HA Core 2026.4
strictly enforces timezone-aware datetimes for SensorDeviceClass.TIMESTAMP.

Important: This also happens after a complete clean reinstall of battery_notes
(remove integration + all entities via HACS, reinstall, re-add all devices).
The naive timestamps come from device_entry.created_at which is used as the
default for last_replaced in coordinator.py __init__.

Root Cause: In coordinator.py, the last_replaced property getter returns
a naive datetime via datetime.fromisoformat(str(entry[LAST_REPLACED])) when the
stored value has no timezone suffix. The setter stores device_entry.created_at
without ensuring timezone awareness. The store migration (_fix_datetime_string)
only fixes the colon/period issue from pre-3.3.2, not missing timezones.
Same issue affects last_reported.

Proposed Fix: A minimal helper ensuring UTC on all datetime reads and writes:

def _ensure_utc(dt_val):
    if dt_val.tzinfo is None:
        return dt_val.replace(tzinfo=datetime.timezone.utc)
    return dt_val

Applied in coordinator.py to last_replaced and last_reported getters/setters.
Tested locally — resolves the issue completely. I can submit a PR if desired.

Related: #4595 fixed set_battery_replaced action but not this init path.

Reproduction steps

  1. Update to HA Core 2026.4+
  2. Install battery_notes v3.4.3 via HACS
  3. Add any device with a battery (or have existing devices)
  4. Restart HA
  5. Check Settings → System → Logs for "missing timezone information" errors
  6. Observe that all sensor.*_battery_last_replaced entities are unavailable

System Health details

System Information

version core-2026.4.2
installation_type Home Assistant OS
dev false
hassio true
docker true
container_arch amd64
user root
virtualenv false
python_version 3.14.2
os_name Linux
os_version 6.12.77-haos
arch x86_64
timezone Europe/Vienna
config_dir /config
Home Assistant Community Store
GitHub API ok
GitHub Content ok
GitHub Web ok
HACS Data ok
GitHub API Calls Remaining 4989
Installed Version 2.0.5
Stage running
Available Repositories 2938
Downloaded Repositories 75
Home Assistant Cloud
logged_in true
subscription_expiration 22. September 2026 um 02:00
relayer_connected true
relayer_region eu-central-1
remote_enabled true
remote_connected true
alexa_enabled false
google_enabled true
cloud_ice_servers_enabled true
remote_server eu-central-1-13.ui.nabu.casa
certificate_status ready
instance_id 8559d840d5474c2ab1a66243384b65b0
can_reach_cert_server ok
can_reach_cloud_auth ok
can_reach_cloud ok
Home Assistant Supervisor
host_os Home Assistant OS 17.2
update_channel stable
supervisor_version supervisor-2026.04.0
agent_version 1.8.1
docker_version 29.3.1
disk_total 125.3 GB
disk_used 26.6 GB
nameservers 10.0.0.101, 10.0.0.100
healthy true
supported true
host_connectivity true
supervisor_connectivity true
ntp_synchronized true
virtualization kvm
board ova
supervisor_api ok
version_api ok
installed_addons Advanced SSH & Web Terminal (23.0.6), ESPHome Device Builder (2026.3.3), File editor (6.0.0), PS5 MQTT (1.4.0), Mosquitto broker (6.5.2), Studio Code Server (6.0.1), Music Assistant (2.7.8), HA Vibecode Agent (2.10.40), Home Assistant Time Machine (2.3.1)
Dashboards
dashboards 15
resources 46
views 46
mode storage
Network Configuration
adapters lo (disabled), enp6s18 (enabled, default, auto), docker0 (disabled), hassio (disabled), veth0620562 (disabled), veth77a9a03 (disabled), vethefb9b7d (disabled), veth24fdb69 (disabled), veth4879bf5 (disabled), vethb0689c7 (disabled), veth7ed418a (disabled), veth6a64c71 (disabled), veth2be522f (disabled), veth6530e40 (disabled), veth2d64dc2 (disabled)
ipv4_addresses lo (127.0.0.1/8), enp6s18 (10.0.0.116/24), docker0 (172.30.232.1/23), hassio (172.30.32.1/23), veth0620562 (), veth77a9a03 (), vethefb9b7d (), veth24fdb69 (), veth4879bf5 (), vethb0689c7 (), veth7ed418a (), veth6a64c71 (), veth2be522f (), veth6530e40 (), veth2d64dc2 ()
ipv6_addresses lo (::1/128), enp6s18 (fe80::2eec:17a4:6014:e461/64), docker0 (fe80::c8bf:c5ff:feef:f71d/64), hassio (fd0c:ac1e:2100::1/48, fe80::8cde:eff:fe64:da4b/64), veth0620562 (fe80::60c6:6eff:fe7b:9ae3/64), veth77a9a03 (fe80::4043:6fff:fe23:664e/64), vethefb9b7d (fe80::2070:89ff:fe13:5044/64), veth24fdb69 (fe80::c830:e3ff:fe9c:b51f/64), veth4879bf5 (fe80::9475:43ff:fe27:f948/64), vethb0689c7 (fe80::6406:24ff:fec4:5d90/64), veth7ed418a (fe80::aca0:ff:fe60:1d6f/64), veth6a64c71 (fe80::cc2:98ff:fe6e:5b3d/64), veth2be522f (fe80::68ec:30ff:fef1:fbb4/64), veth6530e40 (fe80::906e:35ff:fe9a:c1b6/64), veth2d64dc2 (fe80::3cb9:8aff:feee:7763/64)
announce_addresses 10.0.0.116, fe80::2eec:17a4:6014:e461
Recorder
oldest_recorder_run 8. März 2026 um 14:16
current_recorder_run 15. April 2026 um 11:26
estimated_db_size 3209.72 MiB
database_engine sqlite
database_version 3.49.2

Debug logs

2026-04-15 11:02:25.590 ERROR (MainThread) [homeassistant.components.sensor]
Error adding entity sensor.wasser_hobbyraum_battery_last_replaced for domain
sensor with platform battery_notes
Traceback (most recent call last):
  File ".../entity_platform.py", line 680, in _async_add_entities
    ...
  File ".../sensor/__init__.py", line 622, in state
    raise ValueError(...)
ValueError: Invalid datetime: sensor.wasser_hobbyraum_battery_last_replaced
provides state '2024-08-31 13:27:15.201230', which is missing timezone information

Diagnostics dump

Diagnostics download not available for this integration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions