Rules with several time/date ranges

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
marstu
Normal user
Posts: 37
Joined: 29 May 2021, 02:15

Rules with several time/date ranges

#1 Post by marstu » 09 May 2022, 22:53

Dear all,

long time no see, because of being happy until a few days. since the last weekend I though about having the OLEDs of my ESPs only active for a few hours per day, with different time ranges at the weekend and during the week. I tried a lot, but didn't find a solution, I hope to get something running like the one below, but was not able:

Code: Select all

on Clock#Time=All,**:** do
 LogEntry,"Processing One-Minute Clock Event: Time is %systm_hm%, Day=%sysweekday%"
 if %sysweekday%>=1 and %sysweekday%<=7
  if %sysweekday%>1 and %sysweekday%<7  // Allowed Date Range, Mon=2, Fri=6 
   LogEntry,"Mon(2)-Fri(6) Detected, (Day=%sysweekday%)"
   if %systm_hm% > 18:00 and %systm_hm% < 21:00 // 6PM - 09:00PM Time Range
     LogEntry,"Oled Display Turn On Time"
     OledFramedCmd,display,on
   endif
  endif
  if %sysweekday%>1 and %sysweekday%<7  // Allowed Date Range, Mon=2, Fri=6 
   LogEntry,"Mon(2)-Fri(6) Detected, (Day=%sysweekday%)"
   if %systm_hm% > 06:00 and %systm_hm% < 08:00 // 6AM - 8:00AM Time Range
     LogEntry,"Oled Display Turn On Time"
     OledFramedCmd,display,on
   endif
  endif
  if %sysweekday%=1 or %sysweekday%=7  // Allowed Date Range, Sun=1, Sat=7 
   LogEntry,"Sun(1) or Sat(7) Detected, (Day=%sysweekday%)"
   if %systm_hm% > 09:00 and %systm_hm% < 20:00 // 9AM - 8:00PM Time Range
     LogEntry,"Oled Display Turn On Time"
     OledFramedCmd,display,on
   endif
  endif
 else
  LogEntry,"Oled Display Turn Off Time"
  OledFramedCmd,display,off
 endif
endon
Where is my mistake?
BTW: The idea was outside the ranges above the OLEDs should be off.

Thanks already for your ideas already and have a nice evening

marstu
--
ESP8266, ESPEasy (always latest mega release in use :D ), and Domoticz (only beta versions :) )

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

Re: Rules with several time/date ranges

#2 Post by TD-er » 09 May 2022, 23:12

Not looked through all the code, but this one looks weird:

Code: Select all

 if %sysweekday%>=1 and %sysweekday%<=7
With weekdays ranging 1...7, this check is always true.

marstu
Normal user
Posts: 37
Joined: 29 May 2021, 02:15

Re: Rules with several time/date ranges

#3 Post by marstu » 28 May 2022, 09:38

TD-er wrote: 09 May 2022, 23:12 Not looked through all the code, but this one looks weird:
With weekdays ranging 1...7, this check is always true.
Hi TD-er,

you are totally right, I changed the code slightly and now at least it is working from Mon to Fri, but not on Sat and Sun:

Code: Select all

on Clock#Time=All,**:** do
 LogEntry,"Processing One-Minute Clock Event: Time is %systm_hm%, Day=%sysweekday%"
  if %sysweekday%>1 and %sysweekday%<7  // Allowed Date Range, Mon=2, Fri=6 
   LogEntry,"Mon(2)-Fri(6) Detected, (Day=%sysweekday%)"
   if %systm_hm% > 18:00 and %systm_hm% < 22:00 // 6PM - 10:00PM Time Range
     LogEntry,"Oled Display Turn On Time"
     OledFramedCmd,display,on
   elseif %systm_hm% > 06:00 and %systm_hm% < 08:00 // 6AM - 8:00AM Time Range
     LogEntry,"Oled Display Turn On Time"
     OledFramedCmd,display,on
   elseif %sysweekday%=1 or %sysweekday%=7  // Allowed Date Range, Sun=1, Sat=7 
   LogEntry,"Sun(1) or Sat(7) Detected, (Day=%sysweekday%)"
    if %systm_hm% > 09:00 and %systm_hm% < 20:00 // 9AM - 8:00PM Time Range
     LogEntry,"Oled Display Turn On Time"
     OledFramedCmd,display,on
    endif
   endif
  else
   LogEntry,"Oled Display Turn Off Time"
   OledFramedCmd,display,off
  endif
endon
I am not sure if there is an "endif" too much in between, but tried with one less with out a change. On Sat and Sun only a "display off" will be logged.

Any idea?


KR marstu
--
ESP8266, ESPEasy (always latest mega release in use :D ), and Domoticz (only beta versions :) )

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

Re: Rules with several time/date ranges

#4 Post by TD-er » 28 May 2022, 09:52

I just checked your rules in Notepad++ with the ESPEasy rules color highlighter and the ifs and elses to seem to match.
See: https://github.com/letscontrolit/ESPEas ... epad%2B%2B

Also your observation seems to match with the rules.
I just deleted the rest of the lines, so you can see for yourself:

Code: Select all

on Clock#Time=All,**:** do
 LogEntry,"Processing One-Minute Clock Event: Time is %systm_hm%, Day=%sysweekday%"
  if %sysweekday%>1 and %sysweekday%<7  // Allowed Date Range, Mon=2, Fri=6 
// deleted
  else
   LogEntry,"Oled Display Turn Off Time"
   OledFramedCmd,display,off
  endif
endon
N.B. with the syntax highlighting, you can even fold blocks, which makes it easier to check for matching if... endif.

marstu
Normal user
Posts: 37
Joined: 29 May 2021, 02:15

Re: Rules with several time/date ranges

#5 Post by marstu » 28 May 2022, 13:27

TD-er wrote: 28 May 2022, 09:52 I just checked your rules in Notepad++ with the ESPEasy rules color highlighter and the ifs and elses to seem to match.
See: https://github.com/letscontrolit/ESPEas ... epad%2B%2B

Also your observation seems to match with the rules.
I just deleted the rest of the lines, so you can see for yourself:

N.B. with the syntax highlighting, you can even fold blocks, which makes it easier to check for matching if... endif.
Great, thanks for the idea of using blocks, it should be better structured now, although

Code: Select all

on Clock#Time=All,**:** do
 LogEntry,"Processing One-Minute Clock Event: Time is %systm_hm%, Day=%sysweekday%"
  if %sysweekday%>1 and %sysweekday%<7  // Allowed Date Range, Mon=2, Fri=6 
   LogEntry,"Mon(2)-Fri(6) Detected, (Day=%sysweekday%)"
    if %systm_hm% > 18:00 and %systm_hm% < 22:00 // 6PM - 10:00PM Time Range
     LogEntry,"Oled Display Turn On Time"
     OledFramedCmd,display,on
    elseif %systm_hm% > 06:00 and %systm_hm% < 08:00 // 6AM - 8:00AM Time Range
     LogEntry,"Oled Display Turn On Time"
     OledFramedCmd,display,on
    elseif %sysweekday%=1 or %sysweekday%=7  // Allowed Date Range, Sun=1, Sat=7 
     LogEntry,"Sun(1) or Sat(7) Detected, (Day=%sysweekday%)"
      if %systm_hm% > 09:00 and %systm_hm% < 20:00 // 9AM - 8:00PM Time Range
       LogEntry,"Oled Display Turn On Time"
       OledFramedCmd,display,on
      endif
    endif
  else
   LogEntry,"Oled Display Turn Off Time"
   OledFramedCmd,display,off
  endif
endon
Anyway, the part for the weekend is not not working (see log entry below), although the time window fits and the day, but the display is still powered off. It should be on...grrrr
  • 334863879: EVENT: Clock#Time=Sat,13:15
    334863890: ACT : LogEntry,'Processing One-Minute Clock Event: Time is 13:15, Day=7'
    334863893: Processing One-Minute Clock Event: Time is 13:15, Day=7
    334863938: ACT : LogEntry,'Oled Display Turn Off Time'
    334863940: Oled Display Turn Off Time
    334863942: ACT : OledFramedCmd,display,off
I am really getting crazy, during the week it works well, but not at the weekend.
--
ESP8266, ESPEasy (always latest mega release in use :D ), and Domoticz (only beta versions :) )

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

Re: Rules with several time/date ranges

#6 Post by Ath » 28 May 2022, 14:18

The first IF in your event handler excludes sat (7) and sun (1), so trying to select either of them in the last ELSEIF in that block is never giving the desired result...
/Ton (PayPal.me)

marstu
Normal user
Posts: 37
Joined: 29 May 2021, 02:15

Re: Rules with several time/date ranges

#7 Post by marstu » 28 May 2022, 18:36

Ath wrote: 28 May 2022, 14:18 The first IF in your event handler excludes sat (7) and sun (1), so trying to select either of them in the last ELSEIF in that block is never giving the desired result...
Thanks @Ath for the hint...grrrr...you are right:

Code: Select all

on Clock#Time=All,**:** do
 LogEntry,"Processing One-Minute Clock Event: Time is %systm_hm%, Day=%sysweekday%"
  if %sysweekday%>1 and %sysweekday%<7  and %systm_hm% > 06:00 and %systm_hm% < 08:00 or %systm_hm% > 18:00 and %systm_hm% < 22:00// Allowed Date Range, Mon=2, Fri=6 
   LogEntry,"Mon(2)-Fri(6) Detected, (Day=%sysweekday%)"
    LogEntry,"Oled Display Turn On Time"
    OledFramedCmd,display,on
  elseif %sysweekday%=1 or %sysweekday%=7 and %systm_hm% > 09:00 and %systm_hm% < 20:00// Allowed Date Range, Sun=1, Sat=7 
    LogEntry,"Sun(1) or Sat(7) Detected, (Day=%sysweekday%)"
    LogEntry,"Oled Display Turn On Time"
    OledFramedCmd,display,on
  else
   LogEntry,"Oled Display Turn Off Time"
   OledFramedCmd,display,off
  endif
endon
I changed to the above, and at least now the display is on.
--
ESP8266, ESPEasy (always latest mega release in use :D ), and Domoticz (only beta versions :) )

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

Re: Rules with several time/date ranges

#8 Post by Ath » 28 May 2022, 19:07

marstu wrote: 28 May 2022, 18:36

Code: Select all

on Clock#Time=All,**:** do
 LogEntry,"Processing One-Minute Clock Event: Time is %systm_hm%, Day=%sysweekday%"
  if %sysweekday%>1 and %sysweekday%<7  and %systm_hm% > 06:00 and %systm_hm% < 08:00 or %systm_hm% > 18:00 and %systm_hm% < 22:00// Allowed Date Range, Mon=2, Fri=6 
   LogEntry,"Mon(2)-Fri(6) Detected, (Day=%sysweekday%)"
    LogEntry,"Oled Display Turn On Time"
    OledFramedCmd,display,on
  elseif %sysweekday%=1 or %sysweekday%=7 and %systm_hm% > 09:00 and %systm_hm% < 20:00// Allowed Date Range, Sun=1, Sat=7 
    LogEntry,"Sun(1) or Sat(7) Detected, (Day=%sysweekday%)"
    LogEntry,"Oled Display Turn On Time"
    OledFramedCmd,display,on
  else
   LogEntry,"Oled Display Turn Off Time"
   OledFramedCmd,display,off
  endif
endon
Combining AND and OR without any round braces is probably not going to work as intended:

Code: Select all

  if %sysweekday%>1 and %sysweekday%<7  and ((%systm_hm% > 06:00 and %systm_hm% < 08:00) or (%systm_hm% > 18:00 and %systm_hm% < 22:00)) // Allowed Date Range, Mon=2, Fri=6 
This looked the most logical expectation.

NB: Untested, might need to be split up, for having too much logical operations in 1 line
/Ton (PayPal.me)

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

Re: Rules with several time/date ranges

#9 Post by TD-er » 28 May 2022, 19:24

You could also split it in 2 parts:
- Detect the day of the week and set the time thresholds accordingly
- Check the time against the set thresholds.

Code: Select all

on Clock#Time=All,**:** do
  LogEntry,"Processing One-Minute Clock Event: Time is %systm_hm%, Day=%sysweekday%"
  if %sysweekday%>1 and %sysweekday%<7
       LogEntry,"Mon(2)-Fri(6) Detected, (Day=%sysweekday%)"
       let,1,6 // 6:00
       let,2,8 // 8:00
       let,3,18 // 18:00
       let,4,22 // 22:00
  else
       let,1,9  // 9:00
       let,2,20 // 20:00
       // Duplicate the hours, as we only need 1 interval in the weekends.
       let,3,9  // 9:00
       let,4,20 // 20:00
  endif
  if %systm_hm% > [int#1]:00 and %systm_hm% < [int#2]:00
       // turn on
  elseif %systm_hm% > [int#3]:00 and %systm_hm% < [int#4]:00
       // turn on
  else
       // turn off 
  endif
endon

marstu
Normal user
Posts: 37
Joined: 29 May 2021, 02:15

Re: Rules with several time/date ranges

#10 Post by marstu » 28 May 2022, 19:27

Ath wrote: 28 May 2022, 19:07
Combining AND and OR without any round braces is probably not going to work as intended:

Code: Select all

  if %sysweekday%>1 and %sysweekday%<7  and ((%systm_hm% > 06:00 and %systm_hm% < 08:00) or (%systm_hm% > 18:00 and %systm_hm% < 22:00)) // Allowed Date Range, Mon=2, Fri=6 
This looked the most logical expectation.

NB: Untested, might need to be split up, for having too much logical operations in 1 line
I changed to the following...

Code: Select all

on Clock#Time=All,**:** do
 LogEntry,"Processing One-Minute Clock Event: Time is %systm_hm%, Day=%sysweekday%"
  if %sysweekday%>1 and %sysweekday%<7  and ((%systm_hm% > 06:00 and %systm_hm% < 08:00) or (%systm_hm% > 19:00 and %systm_hm% < 22:00))// Allowed Date Range, Mon=2, Fri=6 
   LogEntry,"Mon(2)-Fri(6) Detected, (Day=%sysweekday%)"
    LogEntry,"Oled Display Turn On Time"
    OledFramedCmd,display,on
  elseif ((%sysweekday%=1 or %sysweekday%=7) and (%systm_hm% > 19:10 and %systm_hm% < 20:00)) // Allowed De Range, Sun=1, Sat=7 
    LogEntry,"Sun(1) or Sat(7) Detected, (Day=%sysweekday%)"
    LogEntry,"Oled Display Turn On Time"
    OledFramedCmd,display,on
  else
   LogEntry,"Oled Display Turn Off Time"
   OledFramedCmd,display,off
  endif
endon
...but this message appeared in the log window:
  • 357003879: EVENT: Clock#Time=Sat,19:24
    357003889: ACT : LogEntry,'Processing One-Minute Clock Event: Time is 19:24, Day=7'
    357003892: Processing One-Minute Clock Event: Time is 19:24, Day=7
    357003904: Calculate: Unknown token input: ((19:24)) = 0
    357003906: Calculate: Unknown token input: (19:24) = 0
    357003923: Calculate: Unknown token input: (19:24) = 0

    357003933: ACT : LogEntry,'Oled Display Turn Off Time'
    357003936: Oled Display Turn Off Time
    357003938: ACT : OledFramedCmd,display,off
I have never seen such message highlighted before. So, obviously the "elseif" part is not working.
--
ESP8266, ESPEasy (always latest mega release in use :D ), and Domoticz (only beta versions :) )

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

Re: Rules with several time/date ranges

#11 Post by TD-er » 28 May 2022, 19:31

Better not make these if checks too complicated.
N.B. I did edit my post to remove the () in the if statement too.

marstu
Normal user
Posts: 37
Joined: 29 May 2021, 02:15

Re: Rules with several time/date ranges

#12 Post by marstu » 28 May 2022, 21:43

TD-er wrote: 28 May 2022, 19:31 Better not make these if checks too complicated.
N.B. I did edit my post to remove the () in the if statement too.
Thank you very much, a much better and smart way to solve my problem. It works very well, and I definitively need to get more familiar with "let".

Kind regards and have a nice weekend
--
ESP8266, ESPEasy (always latest mega release in use :D ), and Domoticz (only beta versions :) )

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 4 guests