Clock#Time and several days of the week

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
User avatar
Andrew Mamohin
Normal user
Posts: 95
Joined: 12 Jun 2020, 08:30
Location: Belarus, Gomel
Contact:

Clock#Time and several days of the week

#1 Post by Andrew Mamohin » 27 Jun 2023, 15:36

Is it possible to use one event Clock#Time for several days of the week?

For example, I need an alarm every working day (excluding Saturday and Sunday).
In the cron, it was possible to specify the days of the week 1-5, but here I can only duplicate events?

Code: Select all

On Clock#Time=Mon,6:30 do
 event,bb
endon
On Clock#Time=Tue,6:30 do
 event,bb
endon
.....
Andy.

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

Re: Clock#Time and several days of the week

#2 Post by Ath » 27 Jun 2023, 16:45

Andrew Mamohin wrote: 27 Jun 2023, 15:36 Is it possible to use one event Clock#Time for several days of the week?

For example, I need an alarm every working day (excluding Saturday and Sunday).
In the cron, it was possible to specify the days of the week 1-5, but here I can only duplicate events?

Code: Select all

On Clock#Time=Mon,6:30 do
 event,bb
endon
On Clock#Time=Tue,6:30 do
 event,bb
endon
.....
That should trigger on day 'Wrk' for 'Working day'.

Code: Select all

On Clock#Time=Wrk,6:30 do
 event,bb
endon
Duplicating events won't work, as only the first found instance will be executed for performance reasons (stops scanning the rules file(s) when a match is found).
/Ton (PayPal.me)

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

Re: Clock#Time and several days of the week

#3 Post by Ath » 27 Jun 2023, 22:31

I've added PR #4719 with some documentation on those 'Wrk' and 'Wkd' day indicators, as, although they have been available since 2016, they where never documented.
/Ton (PayPal.me)

User avatar
Andrew Mamohin
Normal user
Posts: 95
Joined: 12 Jun 2020, 08:30
Location: Belarus, Gomel
Contact:

Re: Clock#Time and several days of the week

#4 Post by Andrew Mamohin » 28 Jun 2023, 06:32

Ath wrote: 27 Jun 2023, 22:31 'Wrk' and 'Wkd' day indicators
Thanks, I'll test tomorrow morning. But, it does not allow you to set the event to an group of any days (for example: Tuesday, Thursday, Saturday).
Andy.

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

Re: Clock#Time and several days of the week

#5 Post by Ath » 28 Jun 2023, 08:37

Andrew Mamohin wrote: 28 Jun 2023, 06:32 Thanks, I'll test tomorrow morning. But, it does not allow you to set the event to an group of any days (for example: Tuesday, Thursday, Saturday).
No, that's not possible, though you can have multiple events with different arguments, and you can create a new event handler that does the specific tasks, and call that from several other event handlers, so there will be minimal code duplication:

Code: Select all

On Clock#Time=Mon,6:30 Do
  AsyncEvent,SomeWeekdaysMorningTask
Endon
On Clock#Time=Wed,6:30 Do
  AsyncEvent,SomeWeekdaysMorningTask
Endon
On Clock#Time=Fri,6:30 Do
  AsyncEvent,SomeWeekdaysMorningTask
Endon
On SomeWeekdaysMorningTask do
  // Several commands to be executed on some days of the week
Endon
/Ton (PayPal.me)

User avatar
Andrew Mamohin
Normal user
Posts: 95
Joined: 12 Jun 2020, 08:30
Location: Belarus, Gomel
Contact:

Re: Clock#Time and several days of the week

#6 Post by Andrew Mamohin » 28 Jun 2023, 08:46

Ath wrote: 28 Jun 2023, 08:37 No, that's not possible, though you can have multiple events with different arguments, and you can create a new event handler that does the specific tasks, and call that from several other event handlers, so there will be minimal code duplication:
Of course, I did just that (I provided the code in the first post).
Andy.

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

Re: Clock#Time and several days of the week

#7 Post by TD-er » 28 Jun 2023, 09:53

Better use asyncevent when possible though.
The difference with "event" is that asyncevent will be appended to the eventqueue.
event will create a new rules parsing and thus may eventually lead to stack overflow if this also triggers event, etc.
Apart from the fact that 'event' does use more resources and will cause longer rules execution thus 'blocking' other things to do on the ESP.

User avatar
Andrew Mamohin
Normal user
Posts: 95
Joined: 12 Jun 2020, 08:30
Location: Belarus, Gomel
Contact:

Re: Clock#Time and several days of the week

#8 Post by Andrew Mamohin » 28 Jun 2023, 10:23

TD-er wrote: 28 Jun 2023, 09:53 Better use asyncevent when possible though.
Ok. In which case it is impossible to use AsyncEvent instead of Event?
I want to replace in my rules, but I don't want problems.
Andy.

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

Re: Clock#Time and several days of the week

#9 Post by TD-er » 28 Jun 2023, 10:31

If you need things to be updated/set/handled immediately.

event -> Handled immediately
asyncevent -> Added to the event queue

N.B. adding to the event queue also checks to see if the same event is already present and thus performs a deduplication.

So both have their pros and cons.
Using asyncevent will introduce some delayed response to events, which may help getting out of (or even preventing) typical loops like acting on an event when a relay is closed, causing it to open again, etc.

But depending on the situation it may also trigger such loops when other event handling is based on assumed states after some event should have been handled.

So the answer to your question is: It depends :)

Post Reply

Who is online

Users browsing this forum: No registered users and 41 guests