Clock#Time versus %systime% on certain days

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
ThomasK
New user
Posts: 8
Joined: 05 Aug 2020, 17:34

Clock#Time versus %systime% on certain days

#1 Post by ThomasK » 05 Aug 2020, 18:11

Time problem for Presence simulation

Code: Select all

on Light#Lux do
 if [Light#Lux] < 25 and %systime% < 23:15:00 and %systime% > 16:30:00
   GPIO,2,0
 elseif [Light#Lux] < 35 and %systime% < 7:30:00 and %systime% > 6:30:00
   GPIO,2.0
 else
   GPIO,2,1
  endif
endon

on dummy#switch=0 do
event,lightoff

on dummy#switch=1 do
event,lighton

endon

// Attention!: GPIO2 has been intentionally "inverted" !
// At: dummy => switch => Edit "inversed logic" checked!
Trigger on 25 lux p.m. from 16:30 - 23:15 only on working days
Trigger on 35 lux a.m. from 6:30 - 7:30 only on working days
Why?
Due to the sun coming up and going down the TSL2561 catches different light.

Working days:
Coming home from work: 16:30 – I want the light only switched on at 25 lux.
Going to sleep: 23:15 - I want the light switched off.
Wake up:
6:30 – 7:30 I want the light only switched on at < (less then) 15 lux

Weekend:
The same, but everything 1 hour later

I just build the attached Rules set.
This is working fine, but the only problem is I cann’t declare specific days
The same Rule with Clock#Time doesn’t work, ór only tiggers only one time.
If this is befóre it is dark, GPIO will not go to 1 anymore.

=>Anyone some advice how to work further on?
(I don't want to work with sunset/sunrise)

Thnx. Thomas

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

Re: Clock#Time versus %systime% on certain days

#2 Post by Ath » 05 Aug 2020, 19:54

ThomasK wrote: 05 Aug 2020, 18:11 Time problem for Presence simulation

Code: Select all

on Light#Lux do
 if [Light#Lux] < 25 and %systime% < 23:15:00 and %systime% > 16:30:00
   GPIO,2,0
 elseif [Light#Lux] < 35 and %systime% < 7:30:00 and %systime% > 6:30:00
   GPIO,2.0
 else
   GPIO,2,1
  endif
endon

on dummy#switch=0 do
event,lightoff

on dummy#switch=1 do
event,lighton

endon

// Attention!: GPIO2 has been intentionally "inverted" !
// At: dummy => switch => Edit "inversed logic" checked!
Trigger on 25 lux p.m. from 16:30 - 23:15 only on working days
Trigger on 35 lux a.m. from 6:30 - 7:30 only on working days
Why?
Due to the sun coming up and going down the TSL2561 catches different light.

Working days:
Coming home from work: 16:30 – I want the light only switched on at 25 lux.
Going to sleep: 23:15 - I want the light switched off.
Wake up:
6:30 – 7:30 I want the light only switched on at < (less then) 15 lux

Weekend:
The same, but everything 1 hour later

I just build the attached Rules set.
This is working fine, but the only problem is I cann’t declare specific days
The same Rule with Clock#Time doesn’t work, ór only tiggers only one time.
If this is befóre it is dark, GPIO will not go to 1 anymore.

=>Anyone some advice how to work further on?
(I don't want to work with sunset/sunrise)

Thnx. Thomas
Hi Thomas, welcome to this forum.

There are a few issues with your rules:
- There was an 'endon' missing, that'll possibly disrupt the proper processing of rules
- I don't see a check for Lux < 15 yet? (Assumed your 35 should be a 15, so changed that)
- I haven't found your events lighton and lightoff (so I added them)
- How is the dummy#switch event triggered? Have a switch or button connected there?

The Clock#Time event only supports either All or 1 day of the week, so I used %sysweekday% instead

I'll throw in my 2 cents:

Code: Select all

on Light#Lux do
  if %sysweekday% >= 2 and %sysweekday% <= 6  // Monday (2) to Friday (6)
    if [Light#Lux] < 25 and %systime% < 23:15:00 and %systime% > 16:30:00
      event,LightOn
    elseif [Light#Lux] < 15 and %systime% < 7:30:00 and %systime% > 6:30:00
      event,LightOn
    else
      event,LightOff
    endif
  else // Sunday (1) or Saturday (7)
    if [Light#Lux] < 25 and %systime% < 0:15:00 and %systime% > 17:30:00
      Event,LightOn
    elseif [Light#Lux] < 15 and %systime% < 8:30:00 and %systime% > 7:30:00
      event,LightOn
    else
      ecvent,LightOff
    endif
  endif
endon

on dummy#switch do
  if %eventvalue%=1
    event,LightOn
  else
    event,LightOff
  endif
endon

on LightOn do
  GPIO,2,0  // On
endon

on LightOff do
  GPIO,2,1  // Off
endon
NB: Untested, so sorry if I made typos...
/Ton (PayPal.me)

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

Re: Clock#Time versus %systime% on certain days

#3 Post by TD-er » 05 Aug 2020, 22:38

You can also try to use the cron plugin to generate an event which is then used in the rules to set a variable?
But not sure how well that will work out at boot, so I guess the solution proposed by Ath has a better chance to give the desired result.

ThomasK
New user
Posts: 8
Joined: 05 Aug 2020, 17:34

Re: Clock#Time versus %systime% on certain days

#4 Post by ThomasK » 06 Aug 2020, 13:44

Thanks for your quick reply guys, in the meantime I indeed found out something more about %sysweekday%, but didn't know how to apply that. I am curious if %sysweekday% will work in this Rule.
I gonna try this for shure and let you know the results.

As first time user of this forum, I indeed made some typos, but the Rule has been tested in practice.

TD-er can you "push me in the right direction" where I can find more about that "cron plugin"?

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

Re: Clock#Time versus %systime% on certain days

#5 Post by TD-er » 06 Aug 2020, 13:49

See here for more info on the Cron plugin: https://espeasy.readthedocs.io/en/lates ... /P081.html

ThomasK
New user
Posts: 8
Joined: 05 Aug 2020, 17:34

Re: Clock#Time versus %systime% on certain days

#6 Post by ThomasK » 06 Aug 2020, 21:22

We got the clue!
Kudos+1 for you Ath!

I have tested my Rule including your %sysweekday% line and it works!

Now I can "play around" with my "Presence simulation" and try also the more interesting stuff.
For example: Light On at Wednesday 16:30 and late too bed on Thursday 00:30 Light Off (over midnight).
Do you think with me again Ath ;-)

Thank you very much this was just the little “push” I needed!

Some more info about my original Rule.
Dummy switch in combination with Event is used to send a message to Domoticz to switch on a light.
I think your approach is more “elegant”.
But as a “newbie” in programming I have to study more about Rules to understand what is %eventvalue% etc.
(as I want to understand what I am doing).

Of cóúrse I am going to "normalise" my Rule with your suggestions.

Can take some time, because I am too glad it works now and in the meantime I have "square eyes" from all my "trial and error" programming ;-)

Thank you too TD-er for your link to the Cronjob plugin, I am running EspEasy Test versions, but didn't know there was this plugin.

Keep you informed guys!

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

Re: Clock#Time versus %systime% on certain days

#7 Post by TD-er » 07 Aug 2020, 00:50

The %eventvalue$ is simply a parameter (or one of multiple) sent along with an event.

For example, if a plugin has performed a PLUGIN_READ call and it yielded a value (or multiple), then it will send out an event per value.
For example a task named "bme" is running the BME280 plugin.
This task can yield 3 output values, temp, hum and baro.
These are named as such.
So there can be an event "bme#temp" which obviously also can have an event value like "23.45"

So if you trigger a rules block

Code: Select all

on bme#temp do
   let 1,$eventvalue$
   ...
Then $eventvalue$ can be used as in this example to set an internal variable with its value.

The nice part of the event value is that it remains constant during the handling of the event.
If between generating the event and handling of the event is some read of that plugin, then $eventvalue$ could be different from [bme#temp] even though they should represent the same task variable.

ThomasK
New user
Posts: 8
Joined: 05 Aug 2020, 17:34

Re: Clock#Time versus %systime% on certain days

#8 Post by ThomasK » 10 Aug 2020, 17:29

Ath, I implemented your complete Rule in my application now and I am testing and logging it.
It works!

From your basic Rule I "composed" my overnight switching.
See attached code.

As I am a novice in programming, would you please be so kind to look over my shoulder to make sure I don't make too big mistakes or forget commands.
(ánd every test to discover my faults will take a whole week ;-()

As I could'nt find more info about this, Is it possible to "parse" commands?
For example If % sysweek% >=6 and <= 2 ór if [Light#Lux] <25 and >30.
Can I use semicolons or work with curly braces?
E.g. %sysweek% {>=6;<=2} ór [Light#Lux] {<25;>30}
Where can I find more info about this?

Thanks a lot, Thomas

Code: Select all

on Light#Lux do
    if %sysweekday% >= 2 and %sysweekday% <= 6   // Monday (2) to Friday (6)
    if [Light#Lux] < 25 and %systime% > 16:30:00  
	// has to be > for switching on at dark after 16:30 => Switching Off in next else statement.
      event,LightOn

   elseif [Light#Lux] < 15 and %systime% < 7:30:00 and %systime% > 6:30:00
      event,LightOn
    
  else
    if %sysweekday% = 2 or %sysweekday% = 3  or %%sysweekday% = 6 and %systime% = 23:15:00
    if %sysweekday% = 5  and %systime% = 00:15:00 // Thursday (5) at 00:15
      event,LightOff
   endif
endif   // Necessary 2nd endif? => do Ineed to use a third one?
  
	// Above business days part with late to bed on Wednesday (Thursday 00:15)


	// Below Weekend part with late to bed on Friday (Saturday 00:15) and Saturday (Sunday 00:15)

  else 
    if %sysweekday% = 7 or %sysweekday% = 1 // Sat (7) or Sun (1)
    if [Light#Lux] < 25 and %systime% > 16:00:00
          Event,LightOn
   
  elseif  [Light#Lux] < 15 and  %sysweekday% = 7 or %sysweekday% = 1   // Sat (7) or Sun (1)
  elseif %systime% < 8:30:00   and %systime% > 7:30:00
      event,LightOn 

  elseif  [Light#Lux] < 25 and  %sysweekday% = 7 or %sysweekday% = 2   // Sat (7) or Mon (2)
  elseif %systime% = 00:15:00

event,LightOff
   
   endif
  endif  // Necessary 2nd endif? => do I need to use a third one?

endon

	// Below only the Dummy part = OK, switches the Wemos board LED On/Off
on dummy#switch do
  if %eventvalue%=1
    event,LightOn
  else
    event,LightOff
  endif
endon

on LightOn do
  GPIO,2,0  // On
endon

on LightOff do
  GPIO,2,1  // Off
endon


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

Re: Clock#Time versus %systime% on certain days

#9 Post by TD-er » 10 Aug 2020, 17:55

You have a lot of nested if statements.
Not sure if the if .. endif all match, and you stay below the depth limit.

So maybe you can split things into smaller parts for testing.
Also you can use variables to keep the state (and initialize them on the boot event) so you don't need to check all exceptions each time.

ThomasK
New user
Posts: 8
Joined: 05 Aug 2020, 17:34

Re: Clock#Time versus %systime% on certain days

#10 Post by ThomasK » 11 Aug 2020, 17:50

Addictif that programming.
Can anyone tell me how Rules Time is calculated?
In Domoticz this is from 00:00 to 24:00

What about this in EspEasy Rules? From: 00:00 – 24:00 plus 1 houre??

Why: As I want program overnight events.

What is Wednesday late to bed 00:15 => is this Wednesday 00:15 ór Thursday 00:15?
I can imagine this changes at 01:00 (problem still stays for clocktimes after 01:00 😉)

See also my generated code for this problem.
As novice in programming all suggestions are very welcome.
Thnx. Thomas

Code: Select all

on Light#Lux do
    if %sysweekday% = 2 // Monday (2) 
    if [Light#Lux] < 25 and %systime% < 23:15:00 and %systime% > 16:30:00
      event,LightOn
    elseif [Light#Lux] < 15 and %systime% < 7:30:00 and %systime% > 6:30:00
      event,LightOn
  else
      event,LightOff
    endif
  
elseif
   if %sysweekday% = 3 // Tuesday (3) 
    if [Light#Lux] < 25 and %systime% < 23:15:00 and %systime% > 16:30:00
      event,LightOn
    elseif [Light#Lux] < 15 and %systime% < 7:30:00 and %systime% > 6:30:00
      event,LightOn
  else
      event,LightOff
    endif

elseif
   if %sysweekday% = 4 // Wednesday (4) 
    if [Light#Lux] < 25 and %systime% > 16:30:00
      event,LightOn
    elseif [Light#Lux] < 15 and %systime% < 7:30:00 and %systime% > 6:30:00
      event,LightOn
  else
      event,LightOff
    endif

elseif
   if %sysweekday% = 5 // Thursday (5) 
    if [Light#Lux] < 25 and %systime% < 00:15:00 and %systime% > 16:30:00
      event,LightOn
    elseif [Light#Lux] < 15 and %systime% < 7:30:00 and %systime% > 6:30:00
      event,LightOn
  else
      event,LightOff
    endif

elseif
   if %sysweekday% = 6 // Friday (6) 
    if [Light#Lux] < 25 and %systime% < 00:15:00 and %systime% > 16:30:00
      event,LightOn
    elseif [Light#Lux] < 15 and %systime% < 7:30:00 and %systime% > 6:30:00
      event,LightOn
  else
      event,LightOff
    endif
elseif
   if %sysweekday% = 7 // Saturday (7) 
    if [Light#Lux] < 25 and %systime% < 00:15:00 and %systime% > 16:30:00
      event,LightOn
    elseif [Light#Lux] < 15 and %systime% < 8:30:00 and %systime% > 7:30:00
      event,LightOn
  else
      event,LightOff
    endif

elseif
   if %sysweekday% = 1 // Sunday (1) 
    if [Light#Lux] < 25 and %systime% < 23:15:00 and %systime% > 16:30:00
      event,LightOn
    elseif [Light#Lux] < 15 and %systime% < 8:30:00 and %systime% > 7:30:00
      event,LightOn
  else
      event,LightOff
     endif

  endif
endon

on dummy#switch do
  if %eventvalue%=1
    event,LightOn
  else
    event,LightOff
  endif
endon

on LightOn do
  GPIO,2,0  // On
endon

on LightOff do
  GPIO,2,1  // Off
endon

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

Re: Clock#Time versus %systime% on certain days

#11 Post by TD-er » 11 Aug 2020, 21:20

You can have a look at the system variables page to see the current value of all variables available in ESPEasy.
Also you can either set the time manually (see 'datetime' https://espeasy.readthedocs.io/en/lates ... nds-listed )
This will testing a bit more practical.

The rules are based on the "local time", so depending on the use of DST, you may run into strange effects twice a year between 2 and 3 am. (or whatever DST rule you enter)
As far as I know, the day is from 0:00 - 23:59, so there should be no 24:00 at all.

By the way, have you seen there is a variable for sunrise and sunset and you can also add an offset for them.
Maybe that's also useful for your application?
The sunrise/sunset do need to know the coordinates in the Tools=>advanced settings. (and the date + time of course)

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 8 guests