I'm having an issue keeping my system time accurate. It always appears to be 5-6 seconds off following scheduled power on/off's. Any ideas? There is no network lag/latency as this is local.
>>> Your system time is: 2023-10-23 07:36:20 PDT >>> Your RTC time is: 2023-10-23 07:36:25 PDT
Even when I "Write RTC to System" it still reports 2 seconds behind:
What do you want to do? (1~13) 2 Writing RTC time to system... Done :-) ================================================================================ >>> Current temperature: 28.25°C / 82.85°F >>> Your system time is: 2023-10-23 07:40:32 PDT >>> Your RTC time is: 2023-10-23 07:40:34 PDT
So I wrote up a quick .sh file to run the following functions from utilities.sh:
get_rtc_timestamp get_sys_timestamp get_network_timestamp 1698073913 1698073911 1698073914
And 2 hours later (using supplied schedule up 5 minutes every 20). RTC and Network still basically match, but system is off by 5-6 sec:
get_rtc_timestamp get_sys_timestamp get_network_timestamp 1698084251 1698084247 1698084252
Witty Pi's RTC is actually invisible to the OS because we do not load the RTC driver into system, and only Witty Pi's software knows how to get time from RTC and write to the system.
It seems your raspberry pi's internal clock is ticking slower than it should. This problem is not on Witty Pi, it is on your raspberry pi.
I am not sure if it is the same issue, but this seems to be similar: https://github.com/MichaIng/DietPi/issues/4455
Thanks for the link, however the internal clock doesn't seem to be drifting. It's consistently set at ~5 seconds behind during boot. And 2 seconds behind if set via the wittyPi.sh menu script.
It seems the WittyPi utilities script reads the $I2C_RTC_SECONDS incorrectly for some reason? Why the difference in values?
I modified rtc_to_system() to log the $I2C_RTC_SECONDS value and the $rtc_ts
rtc_to_system() { log ' Writing RTC time to system...' local rtc_ts=$(get_rtc_timestamp) log "I2C_RTC_SECONDS $(bcd2dec $((0x7F&$(i2c_read 0x01 $I2C_MC_ADDRESS $I2C_RTC_SECONDS))))" log "rtc_ts timestamp $rtc_ts" sudo timedatectl set-ntp 0 >/dev/null sudo date -s @$rtc_ts >/dev/null TIME_UNKNOWN=0 log "I2C_RTC_SECONDS $(bcd2dec $((0x7F&$(i2c_read 0x01 $I2C_MC_ADDRESS $I2C_RTC_SECONDS))))" log "rtc_ts timestamp $rtc_ts" log ' Done :-)' }
Here's the wittyPi.log output on boot:
[xxxx-xx-xx xx:xx:xx] Witty Pi daemon (v4.13) is started. [xxxx-xx-xx xx:xx:xx] Running on Raspberry Pi Zero W Rev 1.1 [xxxx-xx-xx xx:xx:xx] Seems RTC has good time, write RTC time into system [xxxx-xx-xx xx:xx:xx] Writing RTC time to system... [xxxx-xx-xx xx:xx:xx] I2C_RTC_SECONDS 13 [xxxx-xx-xx xx:xx:xx] rtc_ts timestamp 1698163032 [2023-10-24 08:57:12] I2C_RTC_SECONDS 17 [2023-10-24 08:57:12] rtc_ts timestamp 1698163032 [2023-10-24 08:57:12] Done :-) [2023-10-24 08:57:12] Firmware ID: 0x36 [2023-10-24 08:57:12] Firmware Revison: 0x03 [2023-10-24 08:57:13] Current Vout=5.09V, Iout=0.21A [2023-10-24 08:57:13] System starts up because the button is clicked.
Just swapped with a second/spare Witty Pi 4 Mini Board and the time issue is gone?
>>> Your system time is: 2023-10-24 10:19:02 PDT >>> Your RTC time is: 2023-10-24 10:19:02 PDT
How can I "fix" the first board?
Nevermind, it's still off with a different board.
>>> Your system time is: 2023-10-24 13:44:13 PDT >>> Your RTC time is: 2023-10-24 13:44:18 PDT
I tried to reproduce the issue and I modify the rtc_to_system function in the same way, and then I reboot my Pi and check the log:
[xxxx-xx-xx xx:xx:xx] Witty Pi daemon (v4.00) is started.
[xxxx-xx-xx xx:xx:xx] Running on Raspberry Pi 3 Model B Rev 1.2
[xxxx-xx-xx xx:xx:xx] Seems RTC has good time, write RTC time into system
[xxxx-xx-xx xx:xx:xx] Writing RTC time to system...
[xxxx-xx-xx xx:xx:xx] I2C_RTC_SECONDS 35
[xxxx-xx-xx xx:xx:xx] rtc_ts timestamp 1698241295
[2023-10-25 15:41:35] I2C_RTC_SECONDS 36
[2023-10-25 15:41:35] rtc_ts timestamp 1698241295
[2023-10-25 15:41:35] Done 🙂
The log I got is differnt than yours. It is normal to have one second difference because executing command takes time too.
In your log, the difference goes to 4 seconds, which is not normal. You may add more logging steps into it and find out which command takes that long:
sudo timedatectl set-ntp 0 >/dev/null
sudo date -s @$rtc_ts >/dev/null