After update to 2021.5.0 recorder fails to start due to connection failure.
Error during connection setup to mssql+pyodbc://[email protected]/HomeAssistant?charset=utf8;DRIVER={FreeTDS};Port=1434;: (pyodbc.InterfaceError) (‘IM002’, ‘[IM002] [unixODBC][Driver Manager]Data source name not found and no default driver specified (0) (SQLDriverConnect)’)
I’m SQL Server as the database for HA and it’s also broken for me. In my connection string I have to include the DRIVER=FreeTDS, maybe that is where the issue is.
In Home Assistant 2021.5.0 Sqlalchemy was upgraded to 1.4.11. I checked the changelog (for sqlalchemy) and found that in 1.4.12 a bug was fixed:
…
Fixed regression caused by #6306 which added support for DateTime(timezone=True) , where the previous behavior of the pyodbc driver of implicitly dropping the tzinfo from a timezone-aware date when INSERTing into a timezone-naive DATETIME column were lost, leading to a SQL Server error when inserting timezone-aware datetime objects into timezone-native database columns.
…
This seems to be the root of the db-error that is thrown.
Just playing around with Python, not sure if this would break all the other database types:
import datetime
import pytz
import pytz.tzinfo as pytzinfo
UTC = pytz.utc
def process_timestamp_to_utc_isoformat(ts):
"""Process a timestamp into UTC isotime."""
if ts is None:
return None
if ts.tzinfo != UTC:
ts = ts.astimezone(UTC)
return ts.replace(tzinfo=None).isoformat(timespec='milliseconds')
def test_me(value):
result = process_timestamp_to_utc_isoformat(value)
print(f'({value.tzinfo}): {value} => {result}')
unsetnow = datetime.datetime.now()
localnow = unsetnow.astimezone()
utcnow = unsetnow.astimezone(UTC)
test_me(unsetnow)
test_me(localnow)
test_me(utcnow)
(None): 2021-05-07 09:58:55.151784 => 2021-05-07T14:58:55.151
(Central Daylight Time): 2021-05-07 09:58:55.151784-05:00 => 2021-05-07T14:58:55.151
(UTC): 2021-05-07 14:58:55.151784+00:00 => 2021-05-07T14:58:55.151
A change something like that would result in something MSSQL could handle. It might break every other database, I haven’t a clue.
Maybe the aforementioned sqlalchemy change will fix handling the timezone and the excessive precision on the seconds. That would be the preferred case of course.
To get Home Assistant 2021.5.0 running with mssql odbc-drivers it is possible to do the following:
Update the connection string as stated in previous comment: mssql+pyodbc://usr:[email protected]:1433/homeassistant?charset=utf8&driver=ODBC+Driver+17+for+SQL+Server
Stop home assistant
Copy create-scripts for each index that contains any of the datetime-columns
Drop the indexes
Alter each table in the database and convert the datetime columns to datetimeoffset e.g.
alter table dbo.[events]
alter column time_fired datetimeoffset(6) null;
alter table dbo.[events]
alter column created datetimeoffset(6) null;
6. Update the column data to match your timezone (mine is w.europe) update [events] set time_fired = convert(datetime2, time_fired) at time zone ‘W. Europe Standard Time’, created = convert(datetime2, created) at time zone ‘W. Europe Standard Time’
Run the copied create index-scripts to recreate each index
Start Home Assistant
Not leaving any guarantees… please backup before doing anything so that you can revert if necessary. Or wait to see if next release has an updated version of sqlalchemy that can handle this without manual alterations