kheylen25
Posts: 111
Joined: Sun Apr 11, 2021 7:58 am
Location: Girona, Spain

Zero 2W travelling in time

Tue Nov 28, 2023 5:52 pm

Hey guys,

I have a simple python program that just takes a temperature reading, adds it with timestamp in mariadb and then exits. I run this with cron every two minutes. Then I can view the last reading (actual temperature) and graph with Grafana, and this is working for weeks now. This is on a headless Zero 2 W with Bullseye light as the OS, and the official power supply.

Today the graph was weird so I checked the database, and found that the time has drifted. It jumps from 16:44 back to 16:28, so it's off with about 28 minutes. I did do a reboot (by unplugging, I am also having some network problems so it sometimes disappears, but that's another thing), probably around that time, so maybe before it has been disconnected from the internet for a while to fetch the correct time.

Is this normal? Of course I don't have an RTC... I did turn off wlan power management now, and let's see what happens.

Here's the tail of the db. Or did I find a way to travel in time? :lol:

Code: Select all

|   44752 | 2023-11-28 16:18:02 |       19.79 |
|   44753 | 2023-11-28 16:20:01 |       19.83 |
|   44754 | 2023-11-28 16:22:02 |       19.83 |
|   44755 | 2023-11-28 16:24:01 |       19.84 |
|   44756 | 2023-11-28 16:26:02 |       19.89 |
|   44757 | 2023-11-28 16:28:02 |       19.93 |
|   44758 | 2023-11-28 16:30:01 |       19.96 |
|   44759 | 2023-11-28 16:32:02 |       20.00 |
|   44760 | 2023-11-28 16:34:01 |       20.01 |
|   44761 | 2023-11-28 16:36:02 |       20.13 |
|   44762 | 2023-11-28 16:38:02 |       20.29 |
|   44763 | 2023-11-28 16:40:01 |       20.39 |
|   44764 | 2023-11-28 16:42:02 |       20.43 |
|   44765 | 2023-11-28 16:44:01 |       20.47 |
|   44766 | 2023-11-28 16:46:02 |       20.49 |
|   44767 | 2023-11-28 16:18:02 |       20.57 |
|   44768 | 2023-11-28 16:20:02 |       20.66 |
|   44769 | 2023-11-28 16:22:01 |       20.72 |
|   44770 | 2023-11-28 16:24:02 |       20.81 |
|   44771 | 2023-11-28 16:26:02 |       20.91 |
|   44772 | 2023-11-28 16:28:02 |       20.96 |
|   44773 | 2023-11-28 16:30:01 |       20.96 |
|   44774 | 2023-11-28 16:32:02 |       21.05 |
|   44775 | 2023-11-28 16:34:02 |       21.11 |
|   44776 | 2023-11-28 16:36:01 |       21.07 |
|   44777 | 2023-11-28 16:38:02 |       21.13 |
+---------+---------------------+-------------+

pidd
Posts: 5591
Joined: Fri May 29, 2020 8:29 pm
Location: Wirral, UK

Re: Zero 2W travelling in time

Wed Nov 29, 2023 3:52 am

Does the program autorun on boot?

kheylen25
Posts: 111
Joined: Sun Apr 11, 2021 7:58 am
Location: Girona, Spain

Re: Zero 2W travelling in time

Wed Nov 29, 2023 8:44 am

pidd wrote:Does the program autorun on boot?
The program runs with cron, every two minutes. I don't think there is any problem with the program itself, just the clock that started drifting, probably due to a loss of the internet connection so it couldn't stay in sync. A certain drift may be expected, but 28 minutes seems a lot to me. For me, not really important at this moment, just puzzled.

User avatar
rpdom
Posts: 23375
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Zero 2W travelling in time

Wed Nov 29, 2023 9:49 am

That looks to me like a hard reset happened. The clock got saved automatically at 16:17 as usual, then a power fail or something happened at about 16:47 and when the Pi restarted the clock reloaded from the last saved point.

Possible lack of internet connection meant it couldn't resynchronise to the correct time.

uptime -s should show you the last boot time.
Unreadable squiggle

hippy
Posts: 15879
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Zero 2W travelling in time

Wed Nov 29, 2023 1:41 pm

rpdom wrote:
Wed Nov 29, 2023 9:49 am
The clock got saved automatically at 16:17 as usual, then a power fail or something happened at about 16:47 and when the Pi restarted the clock reloaded from the last saved point.
I had something similar a few days ago which I also put down to that happening; the code printing the time having run before the Pi had done an NTP synchronisation.

If it were continually reporting the time I would have expected a subsequent jump forward, or stay with that if it couldn't, jump when it could.

I'm afraid I can't find my code which checks if a Pi is running on its last remembered time or synchronised time.

Code: Select all

import os

def IsTimeSynchronised():
  return os.popen("timedatectl").read().find("synchronized: yes") > 0

if IsTimeSynchronised():
  print("Time synchronised")
else:
  print("Time is not synchronised")
Not exhaustively tested.

kheylen25
Posts: 111
Joined: Sun Apr 11, 2021 7:58 am
Location: Girona, Spain

Re: Zero 2W travelling in time

Wed Nov 29, 2023 1:53 pm

rpdom wrote: That looks to me like a hard reset happened. The clock got saved automatically at 16:17 as usual, then a power fail or something happened at about 16:47 and when the Pi restarted the clock reloaded from the last saved point.

Possible lack of internet connection meant it couldn't resynchronise to the correct time.

uptime -s should show you the last boot time.
Indeed the difference in time showed up after a hard reset by unplugging. I was forced to do this because I couldn't connect to the graphana server nor SSH into it, not even ping, but it kept recording every two minutes with the timestamp out of sync. I am pretty sure that internet connection was lost before the reset (as it was not available on the network) and the Zero had started drifting, so all the last lines have the time in sync again. It may have been offline for a miximum of 2-3 days.

DeaD_EyE
Posts: 150
Joined: Sat May 17, 2014 9:49 pm

Re: Zero 2W travelling in time

Thu Nov 30, 2023 8:13 am

Improved version of the check if the clock is synchronized:

Code: Select all

import sys
from subprocess import check_output


def is_synchronized() -> bool:
    stdout = check_output(["timedatectl", "status"], encoding="ascii", env={"LANG": "C"})
    return "System clock synchronized: yes" in stdout


if __name__ == "__main__":
    sys.exit(not is_synchronized())

kheylen25
Posts: 111
Joined: Sun Apr 11, 2021 7:58 am
Location: Girona, Spain

Re: Zero 2W travelling in time

Thu Nov 30, 2023 8:54 am

Thank you so much for the code hippy and DeaD_EyE! The result says NOT synchronised. Then I have digged further...

The Zero is online on the local network and I can SSH and I have access to the graphana server. But from here it seems no connection to the internet is possible, i.e. I can't ping google or any other website, no update, no pip install, no curl... Error says that the name can't be resolved, so I am now looking into that. The nameserver points to my router (192.168.1.1), and I checked the country settings. Yesterdays system time was about 30 minutes off again.

I believe that the last time I downloaded or installed something with internet connection was precisely when installing graphana and mariadb, and this is probably about two months ago.

Return to “Python”