Rules between sunset and sunrise

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
edstobi
Normal user
Posts: 39
Joined: 28 May 2020, 20:33

Rules between sunset and sunrise

#1 Post by edstobi » 18 May 2023, 20:03

hello
I have a small problem
I want to check when on the esp an event occurs if it is in the time between sunset and sunrise. If yes then turn light(GPIO 12,1) on otherwise ignore.
I have tried with time comparison but it does not work because of date change at midnight.

Code: Select all

 
 If  %systime%>%sunrise% 
  let,1,0
Endif

 If  %systime%<%sunset% 
  let,1,1
Endif

Second attempt. I set a variable to 1 when sunset and to 0 when sunrise.

Code: Select all

on Clock#Time=All,%sunrise% do
 let,1,0
 endon
on Clock#Time=All,%sunset-2h% do
 let,1,1
endon
This works if the esp runs constantly. But if it is rebooted after sunset, the variable is not set because we are already after sunset. The cycle works again the next day. This is annoying and I would like to find a solution for it.

Does anyone have an idea how i can get the start after sunset?

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

Re: Rules between sunset and sunrise

#2 Post by TD-er » 18 May 2023, 21:18

Let's say your event is called "myevent"...

Code: Select all

on myevent do
  if %systime% > %sunset% or %systime% < %sunrise%
    gpio,12,1
    timerSet,1,30 // Set timer #1 to 30 seconds      
  endif
endon


On Rules#Timer=1 do  //When Timer1 expires, do
    gpio,12,0
endon
So this will also set a timer to turn off the GPIO pin 12 after 30 seconds.

N.B. you can also add or subtract some time from sunrise and sunset.
For example %sunrise+10m% to add 10 minutes to sunrise.

edstobi
Normal user
Posts: 39
Joined: 28 May 2020, 20:33

Re: Rules between sunset and sunrise

#3 Post by edstobi » 18 May 2023, 22:20

THX

Code: Select all

if %systime% > %sunset% or %systime% < %sunrise%
just take an OR, its so easy´( when you think of it) :oops:
EDSTOBY

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

Re: Rules between sunset and sunrise

#4 Post by TD-er » 18 May 2023, 23:18

You can still use an and, but then you need to 'negate' it :)

A or B == not( not(A) and not(B) )

The simplest "not" in ESPEasy rules is an "else" :)

Code: Select all

if %systime% > %sunrise% and %systime% < %sunset% 
  // Not interesting as it is daytime
else
  // Situation: %systime% > %sunset% or %systime% < %sunrise%
endif

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 37 guests