Checking that time is right
Moderators: grovkillen, Stuntteam, TD-er
Checking that time is right
Hi Everyone !
In an application, I need to check if system time is correct in order to prevent wrong scenarios at wrong time in the event of booting up the esp while internet is down, thus making system time starting at midnight in middle of the day.
As far as I understand it, the "Time#Set" event is supposed to be triggered when the NTP synchronisation appears.
This happeans correctly on a soft reboot (Tools -> reboot), but not on a fresh power on boot (0V to power).
In latter case : the "Time#Set" function is not triggered, even if the NTP seams to do its job because I still notice that the time is correct in main page.
NTP is enabled in advanced settings and the behaviour remains the same wether its adress is left blank as documentation says or filled with a local NTP server here in my LAN.
And about the "Time#Initialized" event, it looks to be triggered at every boot even if NTP server is not reachable or wrong NTP server is setup in the advanced tab.
ESP_Easy_mega_20211105_normal_ESP8266_1M on a sonoff switch, and
Same behaviour on ESP_Easy_mega_20220427_test_B_ESP8266_4M1M_VCC on Wemos D1 mini
Thanks for any inputs !,
Have a great espeasy day
In an application, I need to check if system time is correct in order to prevent wrong scenarios at wrong time in the event of booting up the esp while internet is down, thus making system time starting at midnight in middle of the day.
As far as I understand it, the "Time#Set" event is supposed to be triggered when the NTP synchronisation appears.
This happeans correctly on a soft reboot (Tools -> reboot), but not on a fresh power on boot (0V to power).
In latter case : the "Time#Set" function is not triggered, even if the NTP seams to do its job because I still notice that the time is correct in main page.
NTP is enabled in advanced settings and the behaviour remains the same wether its adress is left blank as documentation says or filled with a local NTP server here in my LAN.
And about the "Time#Initialized" event, it looks to be triggered at every boot even if NTP server is not reachable or wrong NTP server is setup in the advanced tab.
ESP_Easy_mega_20211105_normal_ESP8266_1M on a sonoff switch, and
Same behaviour on ESP_Easy_mega_20220427_test_B_ESP8266_4M1M_VCC on Wemos D1 mini
Thanks for any inputs !,
Have a great espeasy day
Re: Checking that time is right
Hi,
To have a reliable time-source, ESPEasy supports a number of real-time clock chips, usually accompanied by a backup-battery, connected via I2C to the ESP, as documented here: https://espeasy.readthedocs.io/en/lates ... ime-source
To have a reliable time-source, ESPEasy supports a number of real-time clock chips, usually accompanied by a backup-battery, connected via I2C to the ESP, as documented here: https://espeasy.readthedocs.io/en/lates ... ime-source
/Ton (PayPal.me)
Re: Checking that time is right
Hello! I'm just getting started with ESP_Easy and I have some questions...
At the moment, I'm interested in working with time. I'm sorry if I'm not clear about my essence.
1. How often and with what frequency does synchronization with the NTP server take place, is it possible to set this parameter somewhere?
2. Can I write several servers in the NTP Hostname line? I wrote it like this: 0.openwrt.pool.ntp.org,ntp.time.in.ua,ntp.msk-ix.ru,time.nist.g is it possible to do so?
3.In a situation where there is a loss of connection to wi-fi or to the Internet, how long will the local time on the ESP module itself be correct?
4. A simple example (by the way, how else can this be written more compactly and perhaps more correctly?)
on Clock#Time=All,07:00 do
GPIO,0,1
endon
on Clock#Time=All,22:00 do
GPIO,0,0
endon
Everything works correctly under ideal conditions, good internet, no power outages ESP and so on ... It's a completely different situation if, for example, if the ESP module is de-energized (turns off) at 15:21 and will be turned on again at 18:33. Condition (rule) will not be executed until the next day at 07:00. How to solve such a problem, what are the options?
This is an example from the Wiki:
on Pir#State=1 do
If %systime% < 07:00:00
Gpio,16,0
Endif
If %systime% > 19:00:00
Gpio,16,1
Endif
Endon
Can I use it somehow? I don’t know how to correctly write down what needs to be specified in the on.....do body?
You can not do it this way?
on Time#Initialized do
If %systime% < 07:00:00
Gpio,16,0
Endif
If %systime% > 19:00:00
Gpio,16,1
Endif
Endon
Thank you!
At the moment, I'm interested in working with time. I'm sorry if I'm not clear about my essence.
1. How often and with what frequency does synchronization with the NTP server take place, is it possible to set this parameter somewhere?
2. Can I write several servers in the NTP Hostname line? I wrote it like this: 0.openwrt.pool.ntp.org,ntp.time.in.ua,ntp.msk-ix.ru,time.nist.g is it possible to do so?
3.In a situation where there is a loss of connection to wi-fi or to the Internet, how long will the local time on the ESP module itself be correct?
4. A simple example (by the way, how else can this be written more compactly and perhaps more correctly?)
on Clock#Time=All,07:00 do
GPIO,0,1
endon
on Clock#Time=All,22:00 do
GPIO,0,0
endon
Everything works correctly under ideal conditions, good internet, no power outages ESP and so on ... It's a completely different situation if, for example, if the ESP module is de-energized (turns off) at 15:21 and will be turned on again at 18:33. Condition (rule) will not be executed until the next day at 07:00. How to solve such a problem, what are the options?
This is an example from the Wiki:
on Pir#State=1 do
If %systime% < 07:00:00
Gpio,16,0
Endif
If %systime% > 19:00:00
Gpio,16,1
Endif
Endon
Can I use it somehow? I don’t know how to correctly write down what needs to be specified in the on.....do body?
You can not do it this way?
on Time#Initialized do
If %systime% < 07:00:00
Gpio,16,0
Endif
If %systime% > 19:00:00
Gpio,16,1
Endif
Endon
Thank you!
Re: Checking that time is right
1. Usually the NTP server is polled once per hourigorka wrote: ↑17 Jul 2022, 14:52 1. How often and with what frequency does synchronization with the NTP server take place, is it possible to set this parameter somewhere?
2. Can I write several servers in the NTP Hostname line? I wrote it like this: 0.openwrt.pool.ntp.org,ntp.time.in.ua,ntp.msk-ix.ru,time.nist.g is it possible to do so?
3.In a situation where there is a loss of connection to wi-fi or to the Internet, how long will the local time on the ESP module itself be correct?
4. A simple example (by the way, how else can this be written more compactly and perhaps more correctly?)
2. If you do not enter any NTP server names, the unit will request from the pool automatically, and the pool will pick one close to your location, based on the IP-address it is requested from.
3. There is some skew in time, but that should at most be a few seconds per day, often less than that.
4. Your code looks quite OK. Once you start extending it, you will probably make improvements yourself
I'd expect the second rule to trigger at 22:00, there are in fact to rules (eventhandlers) (NB: Adding code tags using the "</>" button in the Forum toolbar will improve readability)
Code: Select all
// First rule, will be triggered at 07:00
on Clock#Time=All,07:00 do
GPIO,0,1
endon
// Second rule, will be triggered at 22:00
on Clock#Time=All,22:00 do
GPIO,0,0
endon
That would be an extra security measure, executed once if the time is (re-)initialized. Adjusted the example to use the same GPIO pins and timesigorka wrote: ↑17 Jul 2022, 14:52 ...You can not do it this way?
Code: Select all
on Time#Initialized do If %systime% <= 07:00:00 Gpio,0,1 Endif If %systime% >= 22:00:00 Gpio,0,0 Endif Endon
/Ton (PayPal.me)
Re: Checking that time is right
Thank you! I will definitely check your code in action.
Faced such a situation: after several reboots of the ESP (I'm checking different code, I'm learning), the time has gone wrong, Local Time: 1970-01-01. Can I force a connection to the NTP server by command?It is strange that against NTP Initialized: there is a green checkmark, as if the time is accurate.If this cannot be corrected promptly, then this can lead to unpredictable consequences...I want to watch for possible malfunctions and I want to check the %sysyears% variable, if it contains the value 70 then take action.But I can't figure out how to compare.Will I be able to do this using the comparison operator and how to write it correctly, do I need to convert the value of the variable to int, bit or can it be to str?
DeviceName#ValueName=<value>
By the way, more than an hour has passed, and the Time#Set event still hasn't happened. The date and time is 1970-01-01...
Thank you!
Re: Checking that time is right
Have you set the Latitude and Longitude settings below the NTP and DST settings?
NTP Hostname should be empty (I've never set that myself, and it always worked fine).
And External Time Source should be set to None, if no RTC chip (via I2C) is connected. After changing that setting, a reboot is required.
The other questions I'll try to answer later.
/Ton (PayPal.me)
Re: Checking that time is right
There is quite some documentation on Rules, with lots of examples, so reading that would be a good starting point.
About the NTP failing:
What I said before, and:
You could share a screenshot of the Advanced settings page for us to check (from Time source up to and including Location Settings) (you can blur out the Latitude and Longitude for privacy reasons), it is advised to set Lat/Long, as they are used by the NTP request, 2 decimal positions is accurate enough, just click your location in google maps, and get the numbers from the url, example: ".../maps/@52.3032658,5.3127813,11.08z" has Latitude 52.30 and Longitude 5.31. (don't worry, that location is in a 'watery' area in NL )
/Ton (PayPal.me)
Re: Checking that time is right
Nope, the NTP update interval is not 1 hour.
It used to be, but then I realized this was a good "DDoS attack vector" on NTP servers.
For example, if a large region had an power outage (as we had 2 months ago here), then all ESPEasy nodes in that area would start at the same time and thus also will be repeating this burst of NTP requests at the same time.
So now it only will do a refresh at roughly 1h after the first one and then with an interval of roughly 5 - 6 hours.
There is some randomness added to prevent bursts of NTP requests.
Another way to get to know the time is to use a GPS module.
And a way to keep the time is by using one of the supported RTC modules with a backup battery.
Those can be had on Ali Express for very low prices and they typically all use a standard CR2032 to keep the internal clock running.
About the (in)accuracy of the internal clock of ESPEasy...
If you look on the sysinfo page, you see there is some "time wander" value. On one of my nodes here it reports: Time Wander: 0.009 [msec/sec]
Typically this should be less than 0.010 msec/sec or 10 ppm.
It is best to check this value after at least 7 hours uptime, since it will then be based on the 5-6h NTP interval.
It does reflect the crystal stability of the ESP and I mainly added it to see the really bad ones more easily as these also will have poor WiFi performance.
It used to be, but then I realized this was a good "DDoS attack vector" on NTP servers.
For example, if a large region had an power outage (as we had 2 months ago here), then all ESPEasy nodes in that area would start at the same time and thus also will be repeating this burst of NTP requests at the same time.
So now it only will do a refresh at roughly 1h after the first one and then with an interval of roughly 5 - 6 hours.
There is some randomness added to prevent bursts of NTP requests.
Another way to get to know the time is to use a GPS module.
And a way to keep the time is by using one of the supported RTC modules with a backup battery.
Those can be had on Ali Express for very low prices and they typically all use a standard CR2032 to keep the internal clock running.
About the (in)accuracy of the internal clock of ESPEasy...
If you look on the sysinfo page, you see there is some "time wander" value. On one of my nodes here it reports: Time Wander: 0.009 [msec/sec]
Typically this should be less than 0.010 msec/sec or 10 ppm.
It is best to check this value after at least 7 hours uptime, since it will then be based on the 5-6h NTP interval.
It does reflect the crystal stability of the ESP and I mainly added it to see the really bad ones more easily as these also will have poor WiFi performance.
Re: Checking that time is right
Everything is set up as you wrote, except for the coordinates of the area, they didn’t affect anything in my case, the reason is different ...Ath wrote: ↑17 Jul 2022, 17:50 Have you set the Latitude and Longitude settings below the NTP and DST settings?
NTP Hostname should be empty (I've never set that myself, and it always worked fine).
And External Time Source should be set to None, if no RTC chip (via I2C) is connected. After changing that setting, a reboot is required.
You are absolutely right, I read and try to understand, but unfortunately not everything is clear. I found why there is a bug with NTP, the problem appears when I set the ESP static IP address in the WI-FI IP Setting! If someone has a desire, check it on your device. As a result, while the module has a static IP address, it does not see the time from the network, an example of such work in the screenshots.
Another point was also noticed. When the ESP has a dynamic IP address and there are no problems with time synchronization, there is another problem, it is not significant, but it exists! If you remove the power from the module and then turn it on again, the module connects to the network, time synchronization occurs. But it does not work out Time#Set which is written in the rules. If, after that, tools/reboots esp is performed, then the rule clearly works.
Last edited by igorka on 18 Jul 2022, 16:22, edited 1 time in total.
Re: Checking that time is right
All my devices have a fixed IP address, for years, and NTP works just fine (about 4 years...).igorka wrote: ↑18 Jul 2022, 15:24 I found why there is a bug with NTP, the problem appears when I set the ESP static IP address in the WI-FI IP Setting! If someone has a desire, check it on your device. As a result, while the module has a static IP address, it does not see the time from the network, an example of such work in the screenshots.
I assume when using a fixed IP you have correctly set the Gateway, Subnetmask and DNS fields also?
/Ton (PayPal.me)
Re: Checking that time is right
Hello! Are you the author of ESP_Easy? Thank you for your work, what you do is very cool!!!
May I ask you a few questions?
1. If I write my NTP in the NTP Hostname field, from which server will the time be taken? From what I indicated or from your default?
2. It is impossible to set the refresh rate with NTP, as I understand it, is this DDoS protection?
I just have a few real time clock RTC modules, I will use them later, now I want to get a good understanding of NTP. Since you mentioned RTC, the question is: if you put RTC on one ESP, can you synchronize other ESPs over P2P networking? This would help save RTC modules.TD-er wrote: ↑17 Jul 2022, 23:07 Another way to get to know the time is to use a GPS module.
And a way to keep the time is by using one of the supported RTC modules with a backup battery.
Those can be had on Ali Express for very low prices and they typically all use a standard CR2032 to keep the internal clock running.
This is useful information. Thank you!
Re: Checking that time is right
I just now saw your screenshots (please attach them in the forum, not using an external service that will delete them after a couple of weeks/months)
Your DNS is not set, you can usually set it to the same value as your gateway, or if that won't work, enter 4.4.4.4 or 8.8.8.8 for the Google DNS servers.
Without a DNS it can't resolve the NTP server(url) properly...
Your DNS is not set, you can usually set it to the same value as your gateway, or if that won't work, enter 4.4.4.4 or 8.8.8.8 for the Google DNS servers.
Without a DNS it can't resolve the NTP server(url) properly...
/Ton (PayPal.me)
Re: Checking that time is right
Maybe. Then point out my mistake to me, if you don't mind.
This is how I have it:
Maybe. Then point out my mistake to me, if you don't mind.
Router IP address 192.168.0.1
This is how I have it:
WIFI IP Settings
ESP WiFi IP: 192.168.0.100
ESP WiFi Gateway: 192.168.0.1
ESP WiFi Subnetmask: 255.255.255.0
ESP WiFi DNS: leave blank.
Re: Checking that time is right
Okay, now I'll try to do that.
You were right, the problem was DNS.
Thank you!
Re: Checking that time is right
I'm not "the" author, as this is an Open Source project.
For example user Ath in this thread is Ton, who has also contributed a lot (!!) to the project.
But nice to see people appreciate all the work put into it.
If this field is empty, it will take one random NTP host from pool.ntp.org.
As soon as you enter something here, it will try to use that host.
Just make sure to set the DNS configuration in the network config on static IP.
I have also looked into getting the NTP host from the DHCP reply as some routers do give this as an option, but I have not yet been able to access the raw DHCP reply. So this is not (yet?) working.
So for now it is very simple; empty field -> pool.ntp.org.
Nope, right now there isn't a command to force an NTP call/refresh.
Not sure if it is really needed.
I think (not sure, have to check the code) there will be an attempt to reach the NTP servers when the time has not been set and you successfully (re)connect to a network.
Not yet, but there will be an automatic NTP sync over the P2P layer.igorka wrote: ↑18 Jul 2022, 15:48 I just have a few real time clock RTC modules, I will use them later, now I want to get a good understanding of NTP. Since you mentioned RTC, the question is: if you put RTC on one ESP, can you synchronize other ESPs over P2P networking? This would help save RTC modules.
You can however do this manually by acting on an event and send a command to another node via P2P.
One such command can be to set the time.
See the command datetime: https://espeasy.readthedocs.io/en/lates ... l-commands
However, this is probably not really accurate as it will probably take some time to format the time in rules and sending over the UDP P2P layer also takes some time.
For the ESP-NOW PR I'm working on, I have it already implemented and due to the very low latency of ESP-NOW, it can get all nodes in a mesh synchronized with an uncertainty of only a few msec. But that's still a work-in-progress.
Re: Checking that time is right
Hello! Please help, I can’t write it to work correctly. The task is the following, at 08:00 you need to transfer GPIO,0,0 at 21:00 transfer GPIO,0,1 (protection in case of power loss ESP ...). And what's so complicated, a simple task, but everything does not work correctly, so as not to write. Already the whole brain has broken ...
Code: Select all
on System#Wake do
GPIO,0,1
endon
on Clock#Time=All,08:00 do
GPIO,0,0
endon
on Clock#Time=All,21:00 do
GPIO,0,1
endon
on Time#Initialized do
if %systime% >= 00:00:00 or <= 08:00:01
GPIO,0,1
else
GPIO,0,0
if %systime% >= 21:00:01 or <= 23:59:59
GPIO,0,1
else
GPIO,0,0
endif
endon
Last edited by igorka on 20 Jul 2022, 18:33, edited 1 time in total.
Re: Checking that time is right
i found at least one issue... for each comparison you need to do the full statement (i think):
Edit: and there was an endif missing...
Code: Select all
on Time#Initialized do
if %systime% >= 00:00:00 or %systime% <= 08:00:01
GPIO,0,1
else
GPIO,0,0
endif
if %systime% >= 21:00:01 or %systime% <= 23:59:59
GPIO,0,1
else
GPIO,0,0
endif
endon
Re: Checking that time is right
Thank you! But it also didn’t help (there was an endif in the code, I missed it in my post). If you turn off and power on the ESP between 8:00 and 21:00, GPIO, 0 is in the "1" state. This is some kind of mysticism .
Re: Checking that time is right
And this?:
Code: Select all
on Time#Initialized do
if %systime% >= 08:00:00 And %systime% <= 21:00:00
GPIO,0,0
else
GPIO,0,1
endif
endon
Re: Checking that time is right
Not quite.
Code: Select all
on System#Wake do
GPIO,0,1
endon
So if something in the rest of the code do not function properly gpio 0 stays high...
Re: Checking that time is right
So it worked. I'll try how this code will work when restarting ESP after 21:00 and after 00:00.
Thank you!
The code is very simple, I showed you all of it.Let GPIO,0,1 be for a short time because of this command, but then it should work correctly...
Code: Select all
on System#Wake do
GPIO,0,1
Re: Checking that time is right
The "OR" is wrong here.chromo23 wrote: ↑20 Jul 2022, 18:31 i found at least one issue... for each comparison you need to do the full statement (i think):
Code: Select all
on Time#Initialized do if %systime% >= 00:00:00 or %systime% <= 08:00:01 [...] endif endon
In theory at least, since there is no systime before 00:00:00.
It should be an AND here.
Plus you're right, you need to have the full expression, so both compares should compare against %systime%.
Re: Checking that time is right
I want to make rules that will work differently depending on the day of the week (working days and weekends).Something like that:
But such a record does not work .
I tried it like this:
It doesn't work that way either.
Is it possible only in this way?
Etc...
Code: Select all
on Clock#Time=Mon,Tue,Wed,Thu,Fri,07:30 do
GPIO,0,0
endon
on Clock#Time=Mon,Tue,Wed,Thu,Fri,22:00 do
GPIO,0,1
endon
I tried it like this:
Code: Select all
on Clock#Time=Mon or Tue or Wed or Thu or Fri,07:30 do
GPIO,0,0
endon
Is it possible only in this way?
Code: Select all
on Clock#Time=Mon,07:30 do
GPIO,0,0
endon
on Clock#Time=Tuei,07:30 do
GPIO,0,0
endon
Re: Checking that time is right
Or a little bit simpler:
Code: Select all
on Clock#Time=All,07:30 do
if %sysweekday%>1 and %sysweekday%<7
gpio,0,0
endif
endon
on Clock#Time=All,22:00 do
if %sysweekday%>1 and %sysweekday%<7
gpio,0,1
endif
endon
/Ton (PayPal.me)
Re: Checking that time is right
Thanks!It really looks better .
If I may, I will ask you one more question.How to write down such a rule correctly?
Such a record works incorrectly, the first expression works, and the second one is ignored.
Code: Select all
on Time#Initialized do
if %sysweekday%=1 or %sysweekday%=7 and %systime% >= 08:30:00 and %systime% <= 23:00:00
GPIO,0,0
else
GPIO,0,1
endif
endon
on Time#Initialized do
if %sysweekday%>1 and %sysweekday%<7 and %systime% >= 07:30:00 and %systime% <= 22:00:00
GPIO,0,0
else
GPIO,0,1
endif
endon
Code: Select all
on Clock#Time=All,07:30 do
if %sysweekday%>1 and %sysweekday%<7
gpio,0,0
endif
endon
on Clock#Time=All,22:00 do
if %sysweekday%>1 and %sysweekday%<7
gpio,0,1
endif
endon
on Clock#Time=All,08:30 do
if %sysweekday%=7 or %sysweekday%=1
gpio,0,0
endif
on Clock#Time=All,23:00 do
if %sysweekday%=7 or %sysweekday%=1
gpio,0,1
endif
endon
Code: Select all
on Time#Initialized do
if (%sysweekday%>1 and %sysweekday%<7 and %systime% >= 07:30:00 and %systime% <= 22:00:00) or (%sysweekday%=1 or %sysweekday%=7 and %systime% >= 08:30:00 and %systime% <= 23:00:00)
GPIO,0,0
else
GPIO,0,1
endif
endon
Re: Checking that time is right
Something like this?
(untested)
Code: Select all
on Time#Initialized do
if %sysweekday%=1 or %sysweekday%=7
if %systime% >= 08:30:00 and %systime% <= 23:00:00
GPIO,0,0
else
GPIO,0,1
endif
else
if %systime% >= 07:30:00 and %systime% <= 22:00:00
GPIO,0,0
else
GPIO,0,1
endif
endif
endon
Re: Checking that time is right
Then please check your or/and logic, as without any braces used, that'll probably not always do what you expect it to do...
/Ton (PayPal.me)
Re: Checking that time is right
Maybe better to create an event for it, so you don't have to have duplicate code in your rules.
Code: Select all
on CheckTime do
if %sysweekday%=1 or %sysweekday%=7
if %systime% >= 08:30:00 and %systime% <= 23:00:00
GPIO,0,0
else
GPIO,0,1
endif
else
if %systime% >= 07:30:00 and %systime% <= 22:00:00
GPIO,0,0
else
GPIO,0,1
endif
endif
endon
on Time#Initialized do
asyncevent,CheckTime
endon
on Clock#Time=All,**:*0 do
// every 10 minutes
asyncevent,CheckTime
endon
Re: Checking that time is right
Ath, TD-er Thank you, I will experiment
Who is online
Users browsing this forum: Ahrefs [Bot] and 0 guests