-
-
Notifications
You must be signed in to change notification settings - Fork 29.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
datetime.datetime.now() is in UTC and not local time zone in 0.118 #43412
Comments
I've noticed something similar as of 0.118, but I think the issue might go beyond Python. I have a When Home Assistant (supervised) runs the script to update the sensor, it gives the current UTC time. For example, If there were a Python problem with |
Same behavior here! |
Are you sure your locale is correct? Even with your example |
The datetime.datetime.now() function in my Python script also returns UTC instead of local time (+6 hours in my case). That function returned local time prior to upgrading to HA 0.118. |
@ludeeus in my case, yes, I have my locale set to Europe/Madrid. Home Assistant Logs Registry show me the right local time. Supervisor logs show me UTC time (it has always been in that way, it's not new), but python_script , after 0.118, give me the UTC time, so all my python_scripts that use datetime.datetime.now() are not working propertly |
fwiw, because of that behavior, I've always used (/had to use) this in all of my python scripts, and this goes way back. I see no change in python scripts using
with the
|
@sermayoral can you try running |
Marius is correct. Your system timezone should be set by Home Assistant if you run Supervised / OS. If that is wrong, Also make sure the timezone is right when you go to config -> General. |
@ludeeus sure!
The process has finished, Home Assistant restarted but same issue :-( @balloob I'm running Supervised, so my timezone should be set by Home Assistant. My timezone is right in Config -> General: A logger output of a python_script [ System timezone is right in the Home Assistant Environment:
System timezone is right in the Debian OS as well:
|
@Mariusthvdb I'm pretty sure |
well, ymmv, but in my setup I've been struggling with that ever since (or even sometime before) this had a hard coded +1 there, which obviously didn't help during DS time periods. So finally decided to use the Utc sensor. Which fixed it ever since. Using OS, (Hassio at the time), if that's of any relevance. |
@Mariusthvdb My mileage vary. Sorry. I'm sorry for your problems more than 3 years ago, but there is anything wrong with python datetime in 0.118 Home Assistant system. Issue that didn't happen in 0.117-. @tggman and @DendelX seem to have the same issue. Something has been broken. It's 18:24 right now in my country (local time). I have tried the same function in all the PCs in my home, different OS, etc. All of them are returning the right local time:
With the images attached above, it is shown that the timezone is correctly set, both on the Home Assistant system and on the Debian system. But python datetime is not working as expected. Additionally. In the Home Assistant System, I have checked this: The python 2.7 installed in the HA system gives me the correct local time! So, it's clear the HA system has the timezone right. So it seems to affect python_script integration only. But this is not working well, neither with |
Sorry I had a mistake with the keyboard :-( |
@sermayoral What do you see if you press "LOAD FULL HOME ASSISTANT LOG" to see the raw logs in that last image? |
@amelchio in the full log i can see UTC time:
|
This test passes on 117, 118 and 119dev.
|
@balloob yeah, the time is the same, but they are shown in different timezones: |
@balloob can you do a test that compare dt_util.now().hour and datetime.datetime.now().hour on 117, 118 and 119? But it must be restricted_python datetime classes. |
Well, I think I already understand everything. The problem is not the system timezone. I have also tried doing a {{now ()}} in the template engine, and it also returns the local time (with the correct timezone). The problem only happens with the datetime you use inside python_scripts. This datetime does not have the defined system timezone associated with it (for this reason it shows the UTC time instead of the local time). The funny thing is why the datetime that I used inside the python_scripts showed me the local time before 0.118.0? Quite a mystery ... Perhaps it is produced by the changes of #42146, but as much as I look at the PR, I do not see anything strange in it. I think the solution to this issue is to set the timezone in the time module of restricted_python. Something like this: In https://github.com/home-assistant/core/blob/dev/homeassistant/components/python_script/__init__.py set:
But i'm not sure how to do this :-( |
For Home Assistant "the system" is the Docker container. I believe all your findings are consistent with the |
@amelchio Thanks. I undestand, but two points about it:
|
Can you run |
|
@ludeeus sure!
|
Well, that's good! but does not explain anything since that should be used for Did you list all your configured integrations somewhere (including custom_component)? if not can you list them here? |
@ludeeus I swear I have been using
|
We are trying to find out what's wrong, nobody is saying you broke it. Can you try adding this sensor to verify that
It should give the same |
And this one would be interresting:
|
@ludeeus here you are:
This time does not have the timezone. |
Something seems to overwrite how it does that. This is from inside my
UTC would be 11:28 at the point of posting this. Can you try what @amelchio sugested?
|
|
$ docker exec homeassistant python3 -m pip freeze
|
What OS is running on the host, and is that 32 or 64 bit?
|
Can you run this to see if
The output should be something like |
|
32 bits both, OS and HA
|
Issue comes from: alpinelinux/docker-alpine#117 |
Thanks @pvizeli. This reference makes more sense, and that explains why this is working on some systems and no @balloob this is probably the base problem in #43337 as well |
i fix the problem by remove environment : TZ , and add volume
@sermayoral i used with 64-bit and have the same problem |
Thank you guys 😊 |
I can confirm it's working again :-) |
The problem
datetime.datetime.now () function, before 0.118, it gave me the local time. After 0.118 i get an hour less (UTC).
Seems to be a python datetime issue with timezones.
Environment
Problem-relevant
configuration.yaml
python_script:
Traceback/Error logs
Additional information
It's a problem similar to #43337. Looking at the history, I have observed that, since I updated Home Assistant to 0.118, I am experiencing the reported problem.
@balloob I have tagged you as you asked me :-). Thanks!
The text was updated successfully, but these errors were encountered: