Checking that time is right

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
peces
New user
Posts: 7
Joined: 22 Feb 2018, 22:51

Checking that time is right

#1 Post by peces » 17 Jun 2022, 18:45

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

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Checking that time is right

#2 Post by Ath » 17 Jun 2022, 19:31

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
/Ton (PayPal.me)

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: Checking that time is right

#3 Post by igorka » 17 Jul 2022, 14:52

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!

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Checking that time is right

#4 Post by Ath » 17 Jul 2022, 15:59

igorka 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?)
1. Usually the NTP server is polled once per hour

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 ;)
igorka wrote: 17 Jul 2022, 14:52 will be turned on again at 18:33. Condition (rule) will not be executed until the next day at 07:00.
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
igorka 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
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 times ;)
/Ton (PayPal.me)

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: Checking that time is right

#5 Post by igorka » 17 Jul 2022, 17:38

Ath wrote: 17 Jul 2022, 15:59 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 times ;)
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!

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Checking that time is right

#6 Post by Ath » 17 Jul 2022, 17:50

igorka wrote: 17 Jul 2022, 17:38 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...
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)

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Checking that time is right

#7 Post by Ath » 17 Jul 2022, 19:48

igorka wrote: 17 Jul 2022, 17:38 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......
DeviceName#ValueName=<value>
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 :D)
/Ton (PayPal.me)

TD-er
Core team member
Posts: 8642
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Checking that time is right

#8 Post by TD-er » 17 Jul 2022, 23:07

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.

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: Checking that time is right

#9 Post by igorka » 18 Jul 2022, 15:24

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.
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, 19:48 There is quite some documentation on Rules, with lots of examples, so reading that would be a good starting point.
You are absolutely right, I read and try to understand, but unfortunately not everything is clear.
3.jpg
3.jpg (72.44 KiB) Viewed 6615 times
2.jpg
2.jpg (117.38 KiB) Viewed 6615 times
1.jpg
1.jpg (105.96 KiB) Viewed 6615 times
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.

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Checking that time is right

#10 Post by Ath » 18 Jul 2022, 15:28

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.
All my devices have a fixed IP address, for years, and NTP works just fine (about 4 years...).
I assume when using a fixed IP you have correctly set the Gateway, Subnetmask and DNS fields also?
/Ton (PayPal.me)

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: Checking that time is right

#11 Post by igorka » 18 Jul 2022, 15:48

TD-er wrote: 17 Jul 2022, 23:07 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.
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?
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.
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 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.
This is useful information. Thank you!

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Checking that time is right

#12 Post by Ath » 18 Jul 2022, 15:59

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...
/Ton (PayPal.me)

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: Checking that time is right

#13 Post by igorka » 18 Jul 2022, 16:06

igorka wrote: 18 Jul 2022, 15:24 I assume when using a fixed IP you have correctly set the Gateway, Subnetmask and DNS fields also?
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.

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: Checking that time is right

#14 Post by igorka » 18 Jul 2022, 16:18

Ath wrote: 18 Jul 2022, 15:59 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)
Okay, now I'll try to do that.
Ath wrote: 18 Jul 2022, 15:59 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...
You were right, the problem was DNS.
Thank you!

TD-er
Core team member
Posts: 8642
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Checking that time is right

#15 Post by TD-er » 18 Jul 2022, 16:37

igorka wrote: 18 Jul 2022, 15:48 Hello! Are you the author of ESP_Easy? Thank you for your work, what you do is very cool!!!
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. :)
igorka wrote: 18 Jul 2022, 15:48 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?
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.
igorka wrote: 18 Jul 2022, 15:48 2. It is impossible to set the refresh rate with NTP, as I understand it, is this DDoS protection?
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.
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.
Not yet, but there will be an automatic NTP sync over the P2P layer.
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.

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: Checking that time is right

#16 Post by igorka » 20 Jul 2022, 18:27

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.

User avatar
chromo23
Normal user
Posts: 821
Joined: 10 Sep 2020, 16:02
Location: germany

Re: Checking that time is right

#17 Post by chromo23 » 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
  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
Edit: and there was an endif missing... :)

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: Checking that time is right

#18 Post by igorka » 20 Jul 2022, 18:47

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)
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 .

User avatar
chromo23
Normal user
Posts: 821
Joined: 10 Sep 2020, 16:02
Location: germany

Re: Checking that time is right

#19 Post by chromo23 » 20 Jul 2022, 18:52

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

User avatar
chromo23
Normal user
Posts: 821
Joined: 10 Sep 2020, 16:02
Location: germany

Re: Checking that time is right

#20 Post by chromo23 » 20 Jul 2022, 18:55

igorka wrote: 20 Jul 2022, 18:47 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 .
Not quite.

Code: Select all

on System#Wake do
  GPIO,0,1
endon
This is your mysticism.....
So if something in the rest of the code do not function properly gpio 0 stays high...

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: Checking that time is right

#21 Post by igorka » 20 Jul 2022, 18:58

chromo23 wrote: 20 Jul 2022, 18:52И это?
So it worked. I'll try how this code will work when restarting ESP after 21:00 and after 00:00.
Thank you!
chromo23 wrote: 20 Jul 2022, 18:55 This is your mysticism.....
So if something in the rest of the code do not function properly gpio 0 stays high...
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

TD-er
Core team member
Posts: 8642
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Checking that time is right

#22 Post by TD-er » 20 Jul 2022, 21:16

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
The "OR" is wrong here.
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%.

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: Checking that time is right

#23 Post by igorka » 29 Oct 2022, 20:46

I want to make rules that will work differently depending on the day of the week (working days and weekends).Something like that:

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
But such a record does not work :roll: .
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
It doesn't work that way either.
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
Etc...

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Checking that time is right

#24 Post by Ath » 29 Oct 2022, 21:03

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)

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: Checking that time is right

#25 Post by igorka » 30 Oct 2022, 12:36

Ath wrote: 29 Oct 2022, 21:03 Or a little bit simpler:
Thanks!It really looks better :idea: .
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
This will be an extension of this code (following your example).

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
I did so and it even seems to work...But I'm not sure that's the right thing to do.Correct me if I'm wrong.

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

TD-er
Core team member
Posts: 8642
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Checking that time is right

#26 Post by TD-er » 30 Oct 2022, 13:09

Something like this?

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
(untested)

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: Checking that time is right

#27 Post by igorka » 30 Oct 2022, 13:18

TD-er wrote: 30 Oct 2022, 13:09 Something like this?
Exactly.
Although my latest code seems to be working, but it is clearly redundant!

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Checking that time is right

#28 Post by Ath » 30 Oct 2022, 13:20

igorka wrote: 30 Oct 2022, 13:18 Although my latest code seems to be working, but it is clearly redundant!
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)

TD-er
Core team member
Posts: 8642
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Checking that time is right

#29 Post by TD-er » 30 Oct 2022, 13:22

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

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: Checking that time is right

#30 Post by igorka » 30 Oct 2022, 13:31

Ath, TD-er Thank you, I will experiment ;)

Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests