Page 1 of 1

sunrise/sunset to be used in rule

Posted: 30 May 2018, 08:35
by TungstenE2
Hi all,

I would like to use sunrise/sunset time in a rule (eg sunrise + 30min) on a Wemos D1 mini using latest mega build.
How to do this? An external HTTP request would be good, but not to a lokal controler.
I want to be independent from another controler.

Any ideas or tipps?

thx

Re: sunrise/sunset to be used in rule

Posted: 01 Jun 2018, 13:48
by enesbcs
TungstenE2 wrote: 30 May 2018, 08:35 Hi all,
I would like to use sunrise/sunset time in a rule (eg sunrise + 30min) on a Wemos D1 mini using latest mega build.
How to do this? An external HTTP request would be good, but not to a lokal controler.
I want to be independent from another controler.
Any ideas or tipps?
thx
You need to know the date and time at least (NTP or RTC), and you based on it you could use something like this library:
https://github.com/dmkishi/Dusk2Dawn

Re: sunrise/sunset to be used in rule

Posted: 01 Jun 2018, 16:45
by papperone
enesbcs wrote: 01 Jun 2018, 13:48
TungstenE2 wrote: 30 May 2018, 08:35 Hi all,
I would like to use sunrise/sunset time in a rule (eg sunrise + 30min) on a Wemos D1 mini using latest mega build.
How to do this? An external HTTP request would be good, but not to a lokal controler.
I want to be independent from another controler.
Any ideas or tipps?
thx
You need to know the date and time at least (NTP or RTC), and you based on it you could use something like this library:
https://github.com/dmkishi/Dusk2Dawn
you need as well Longitude and Latitute, I think it's too much to implement this in EspEasy, I do this already for my garden lights but I do it in my NodeRed@RasPi :mrgreen:

Re: sunrise/sunset to be used in rule

Posted: 01 Jun 2018, 22:32
by TD-er
I think it is a useful feature, so could you add an issue for it?

Re: sunrise/sunset to be used in rule

Posted: 05 Jun 2018, 15:34
by TungstenE2

Re: sunrise/sunset to be used in rule

Posted: 01 Jul 2018, 18:21
by hooijschuur
Is IT also possible tot post An example rule
There are examples but not for Sunset etc.

Re: sunrise/sunset to be used in rule

Posted: 04 Jul 2018, 08:41
by TungstenE2
I was just about to test it.

New %sunrise% and %sunset% variables were added, but I am not sure about the syntax to be used in rules.

Is there a way to test the variables?

https://www.letscontrolit.com/wiki/inde ... _Variables

Re: sunrise/sunset to be used in rule

Posted: 04 Jul 2018, 14:47
by TungstenE2
could someone give an example rule how to use %sunrise% and %sunset% variables?

on this page nothing is mentioned on how to use variables:
https://www.letscontrolit.com/wiki/inde ... rial_Rules

would this be something like this?

Code: Select all

on  %sunrise% do
 if <test>
   <action>
   <action>
 endif
endon

Re: sunrise/sunset to be used in rule

Posted: 04 Jul 2018, 18:04
by enesbcs
TungstenE2 wrote: 04 Jul 2018, 14:47 could someone give an example rule how to use %sunrise% and %sunset% variables?

on this page nothing is mentioned on how to use variables:
https://www.letscontrolit.com/wiki/inde ... rial_Rules

would this be something like this?

Code: Select all

on  %sunrise% do
 if <test>
   <action>
   <action>
 endif
endon
I am pretty sure that variables do not ment to be used in that way.
I think that Clock#Time event need to be used:
https://www.letscontrolit.com/wiki/inde ... .29_events

Re: sunrise/sunset to be used in rule

Posted: 04 Jul 2018, 22:17
by xury
Perhaps that way:

Code: Select all

on Clock#Time=All,%sunrise% do
 if <test>
   <action>
   <action>
 endif
endon

Re: sunrise/sunset to be used in rule

Posted: 06 Jul 2018, 12:45
by TungstenE2
this is working great! thx!

Code: Select all

 On Clock#Time=All,%sunrise% do 
   <action>                               
endon

That makes it possible to do something at sunrise or sunset and also start a timer to do something at those moments + N seconds.

Code: Select all

//at sunrise time set timer1 for 1 hour
On Clock#Time=All,%sunrise% do 
        timerSet,1,3600                               
endon



//When Timer1 expires, <action>
 On Rules#Timer=1 do  
    <action>
 endon
 
 
 //at sunset set timer2 for 0,5 hour
On Clock#Time=All,%sunset% do 
        timerSet,2,1800                               
endon



//When Timer2 expires,  <action>
 On Rules#Timer=2 do  
    <action>
 endon

Re: sunrise/sunset to be used in rule

Posted: 09 Nov 2018, 11:00
by reliable
Hey.
I would like to use the state of sunrise or sunset when reloading ESP.
I write this rule:

Code: Select all

On System#Wake do
  if[Clock#Time=All,%sunrise%]=1
    TaskValueSet 4,3,0
  endif
endon
On System#Wake do
  if[Clock#Time=All,%sunset%]=1
    TaskValueSet 4,3,1
  endif
endon
but it does not work.
Tell me how to write the rules correctly?

Re: sunrise/sunset to be used in rule

Posted: 09 Nov 2018, 12:24
by grovkillen
Try this one instead:

Code: Select all

On Time#Initialized do
  if %systime%>%sunrise% and %systime%<%sunset%
    TaskValueSet,4,3,0
   else
    TaskValueSet,4,3,1
  endif
endon
Just as an example. I really think it would be unique if you have the system booted at the exact minute of either sunset or sunrise...

Or if you want the rule to check each minute:

Code: Select all

On Clock#Time=All,%sunrise% do
    TaskValueSet 4,3,0
endon

On Clock#Time=All,%sunset% do
    TaskValueSet 4,3,1
endon
I think that last rule should work.. haven't tested it myself though.

Re: sunrise/sunset to be used in rule

Posted: 10 Nov 2018, 11:26
by reliable
Thanks for the help. It really works as I need!

Re: sunrise/sunset to be used in rule

Posted: 10 Nov 2018, 11:42
by grovkillen
Great, thanks for the feedback.

Re: sunrise/sunset to be used in rule

Posted: 23 Mar 2021, 07:33
by Metatron
Hello,

I try to define a rule to open my shutters according to sunrise, but no earlier then 6am. Later during weekends.
However, it doeas not work. Can somebody see my mistake, please?

Code: Select all

On Clock#Time=All,%sunrise-15m% do 
 If %systime% > 06:00:00
   if %sysweekday%=1 or %sysweekday%=7
     timerSet,1,3600
   else 
     timerSet,1,1
   endif
 endif                               
endon

On Clock#Time=All,06:00:00 do
   If %systime% > %sunrise-15m%
    if %sysweekday%=1 or %sysweekday%=7
      timerSet,1,3600
    else 
      timerSet,1,1
    endif
   endif
endon
It is a bit annoying, to have only one chance a day for debugging! :?

Regards, Metatron

Re: sunrise/sunset to be used in rule

Posted: 23 Mar 2021, 08:25
by Ath
This condition won't work as expected:

Code: Select all

    If %systime% > %sunrise-15m%
this is a 'known issue', only the first %-encoded variable is expanded

Re: sunrise/sunset to be used in rule

Posted: 23 Mar 2021, 09:14
by TD-er
For now it is best to store "computations" in a variable using let.
I don't know if only "the first" %-encoded variable is expanded in rules parsing, but there seems to be something fishy happening here.

Code: Select all

let,1,%systime%
let,2,%sunrise-15m%
if [var#1] > [var#2]
  ...

Re: sunrise/sunset to be used in rule

Posted: 23 Mar 2021, 17:57
by Metatron
Thank you!
Does this expanding problem also apply to

Code: Select all

if %sysweekday%=1 or %sysweekday%=7
i.e. in general per line/expression?

Re: sunrise/sunset to be used in rule

Posted: 23 Mar 2021, 18:20
by TD-er
Well it was intended to work, but apparently there is an issue with it.
So you should try to know for sure and when in doubt store it in a variable.

Re: sunrise/sunset to be used in rule

Posted: 23 Mar 2021, 22:28
by Ath
Ath wrote: 23 Mar 2021, 08:25 This condition won't work as expected:

Code: Select all

    If %systime% > %sunrise-15m%
this is a 'known issue', only the first %-encoded variable is expanded
Did some more testing, but all should be working. I was probably misled by another issue, that I fixed today, and seemed applicable here too, but it wasn't.

You may want to check if your rules use Windows Cr/Lf on all lines, my test-script had some invalid line-ends, and after fixing that it all works as intended.

Re: sunrise/sunset to be used in rule

Posted: 23 Mar 2021, 23:11
by TD-er
If the rules are saved using the web interface (and thus not uploaded as rules file) then the CRLF should be correct as that's being dealt with in the JavaScript.

What can be an issue is that the rules may have some strange characters in them if you copied them from a website or used a program like Word as a notepad.
But that should result in errors in the log when parsing the rules.

Re: sunrise/sunset to be used in rule

Posted: 24 Mar 2021, 08:00
by Ath
TD-er wrote: 23 Mar 2021, 23:11 ...the rules may have some strange characters in them if you copied them from a website ...
But that should result in errors in the log when parsing the rules.
I did copy a part of that script from a forum post, so that was most likely the cause, nothing visible in the log though, just some script lines seemed to be ignored.
TD-er wrote: 23 Mar 2021, 23:11 ...or used a program like Word as a notepad.
You'll not catch me attempting that, ever :lol:

Re: sunrise/sunset to be used in rule

Posted: 24 Mar 2021, 18:41
by iron
Hey... Vanila Ice said it !!!

Re: sunrise/sunset to be used in rule

Posted: 24 Mar 2021, 19:17
by Metatron
Hello,

thank you so much for your feedback and support!
This morning, nothing happened - and then I got an idea:
My timezone was wrong! It did not play a role, while only dealing with sunrise/sunset, but for 6:00 it does!
Sorry, my very own fault!
I removed the variables, again. Now let's wait for the next morning!

Regards, Metatron

Re: sunrise/sunset to be used in rule

Posted: 24 Mar 2021, 19:49
by TD-er
Make sure the GPS coordinates are also set correct.
You can print the %sunrise% variable via something like the "logitem" command.
Or if you have a "normal" or "custom" build you can also chech the system variables page.

Re: sunrise/sunset to be used in rule

Posted: 24 Mar 2021, 19:52
by TD-er
Ath wrote: 24 Mar 2021, 08:00 [...]
You'll not catch me attempting that, ever :lol:
Wordpad? Write? WP6.0-for-Windows?
Or are you more like a vi/nano/emacs/elle/ted/edlin kinda guy? ;)

Re: sunrise/sunset to be used in rule

Posted: 24 Mar 2021, 20:33
by Ath
TD-er wrote: 24 Mar 2021, 19:52 Wordpad? Write? WP6.0-for-Windows?
Or are you more like a vi/nano/emacs/elle/ted/edlin kinda guy? ;)
Well, Word (and WP for Dos) is for writing letters and stuff like that, text files need to be done using a tool that doesn't mock them up, so Notepad++ and VSCode are my current go to's, but I used to be quite skilled in edlin, back in the day (when MS-Dos 3 was king :D), though I haven't started that for 20-odd years I think. Vi I also have quite some experience in using, as that was the only general available editor on HP-UX in my previous job, so I also use it out of habit on other *nix systems like RPi and Redhat that I maintain. The other editors you mention I have heard of, but (almost) never used.

Re: sunrise/sunset to be used in rule

Posted: 27 Mar 2021, 16:49
by Metatron
Hello,

sorry to bother you again, but I am still struggling with my rules for sunrise, currently, nothing happens.
Coordinates timezone etc. are fine. I also checked system variables - fine. Wiring is fine, manually all works.
These are the rules:

Code: Select all

On Clock#Time=All,%sunrise-15m% do 
 If %systime%>06:00:00
   if %sysweekday%=1 or %sysweekday%=7
     timerSet,1,3600
   else 
     timerSet,1,1
   endif
 endif                               
endon

On Clock#Time=All,06:00:00 do
//  let,1,%systime%
// let,2,
   If   %sunrise-15m%<06:00:00
    if %sysweekday%=1 or %sysweekday%=7
      timerSet,1,3690
    else 
      timerSet,1,90
    endif
   endif
endon

//When Timer1 expires,  <action>
On Rules#Timer=1 do  
   event,alleauf     
endon
Today I removed the spaces before and after ">", do they matter?
Do small or capital letters matter?

Regards, Metatron

Re: sunrise/sunset to be used in rule

Posted: 27 Mar 2021, 17:22
by Ath
I think you have some logical error, the event on Clock#Time=All,%sunrise-15m% do is fired exactly 1 time, every day. What if sunrise-15m is after 06:00?
Metatron wrote: 23 Mar 2021, 07:33 I try to define a rule to open my shutters according to sunrise, but no earlier then 6am. Later during weekends.
I'd go for something like this:
(Added a Dummy Device at task 12 and named that Shutters, first variable named Opened)

Code: Select all

On Clock#Time=All,**:** do // Check every minute
  If %systime% >= %sunrise-15m% and %systime% > 06:00:00 and [Shutters#Opened] = 0
    TaskValueSet,12,1,1 // Set Opened state (to avoid resetting the timer every minute)
    if %sysweekday%=1 or %sysweekday%=7 // Saturdays and Sundays
      timerSet,1,3600 // Delay for an hour
    else 
      timerSet,1,1 // Delay for 1 second
    endif
  endif
  if %systime% >= %sunset+2h% and [Shutters#Opened] = 1 // Close shutters 2h after sunset, and when not closed yet
    asyncevent,allenieder
  endif
endon

on CloseButton#State do // When the button changes state close or open the shutters
  if [Shutters#Opened] = 1
    asyncevent,allenieder
  else
    asyncevent,alleauf
  endif
endon

on Rules#Timer=1 do
  asyncevent,alleauf
endon

on alleauf do
  TaskValueSet,12,1,1 // Set opened
  // Open all shutters
endon

on allenieder do
  TaskValueSet,12,1,0 // Reset opened
  // Other actions to close the shutters
endon

Re: sunrise/sunset to be used in rule

Posted: 27 Mar 2021, 17:40
by Metatron
First of all, thank you for you reply. I will need some time to think it through.
I think you have some logical error, the event on Clock#Time=All,%sunrise-15m% do is fired exactly 1 time, every day. What if sunrise-15m is after 06:00?
Well, I wrote:

Code: Select all

On Clock#Time=All,%sunrise-15m% do 
 if %systime%>06:00:00
   if %sysweekday%=1 or %sysweekday%=7
     timerSet,1,3600
   else 
     timerSet,1,1
   endif
 endif                               
endon
If this is fired once a day 15min before sunrise, it checks if it is later then 6am with the second "if" and opens the shutter, no?
The the

Code: Select all

On Clock#Time=All,06:00:00 do
//  let,1,%systime%
// let,2,
   if   %sunrise-15m%<06:00:00
   ...
Checks every day at 6am, if sunrise-15m is before 6am, and if yes, opens the shutter at 6am. This is, what I am trying to do, but this does not work.

Re: sunrise/sunset to be used in rule

Posted: 28 Mar 2021, 19:40
by Metatron
Hallo,

something I just realized in the system variables, that %sunrise% and %sunset% don't have seconds, only hours and minutes.
Could the problem be that

Code: Select all

 If   %sunrise-15m%<06:00:00
does not work, but should be:

Code: Select all

 If   %sunrise-15m%<06:00
??

Regards, Metatron

Re: sunrise/sunset to be used in rule

Posted: 28 Mar 2021, 19:49
by Ath
The function that parses the time, calculates that back to seconds, and knows about the 2 and 3 parts variants of time, and (for obvious reasons) assumes the seconds part to be 00 when absent.
When comparing using '=' to a HH:MM:SS time, you can't be off-by-a-second, so changing that to 06:00 seams to be an easy improvement to make that test less exact.

Re: sunrise/sunset to be used in rule

Posted: 28 Mar 2021, 19:51
by Ath
NB: Have you tried to use my proposed rules?
You could add some LogEntry statements to see in the log what's going on, during testing.

Re: sunrise/sunset to be used in rule

Posted: 28 Mar 2021, 19:57
by Metatron
No, sorry, because I don't understand the task 12 device. Could you please send me a screenshot of the definition of that task?

Re: sunrise/sunset to be used in rule

Posted: 02 Apr 2021, 20:23
by Metatron
Hello,

my ESPEasy started to behave odd. Closing shutters at 6:21pm, far to early, like 30min before sunset, opening shutters at midnight.
I checked cr/lf, all fine. No reboots of the esp either (online since 11 days).
Is there any other thing/character, which could confuse my esp?
Should I use a different version, like a "stable" one?

I went back zu simple 7am shutters up, sunset+30m shuttern down.
Now I will observe what happens.

Ah, closing right now, that is fine.

Regards, Metatron

Re: sunrise/sunset to be used in rule

Posted: 02 Apr 2021, 21:15
by Ath
Can you post your complete set of rules here, AFAICS, there are currently only snippets available here, making it impossible to assess what's happening.

Re: sunrise/sunset to be used in rule

Posted: 02 Apr 2021, 21:32
by Metatron
Sure, I took a copy, before simplifying it, I hope it is not a stupid mistake!
But because there are only a few events ...

Code: Select all

On testpins do
GPIO,13,1
Delay 1000
GPIO,13,0
Delay 2000
GPIO,12,1
Delay 1000
GPIO,12,0
Delay 2000
GPIO,4,1
Delay 1000
GPIO,4,0
Delay 2000
GPIO,5,1
Delay 1000
GPIO,5,0
endon
On Clock#Time=All,%sunrise-30m% do
 if %systime%>06:00:00
   if %sysweekday%=1 or %sysweekday%=7
     timerSet,1,3600
   else
     timerSet,1,1
   endif
 endif
endon
On Clock#Time=All,06:30:00 do
  if %sysweekday%=5
   timerSet,1,1800
  else
   timerSet,1,1
  endif
endon
On Clock#Time=All,06:00:00 do
   if %systime%<%sunrise-15m%
    if %sysweekday%=1 or %sysweekday%=7
      timerSet,1,3690
    else
      timerSet,1,90
    endif
   endif
endon
//When Timer1 expires, <action>
On Rules#Timer=1 do
   event,alleauf
endon
// 30min nach untergang
On Clock#Time=All,%sunset+30m% do
    event,einszu
endon
On alleauf do
GPIO,13,1
Delay 1000
GPIO,13,0
endon
On allezu do
GPIO,12,1
Delay 500
GPIO,12,0
endon
On allestop do
GPIO,12,1
GPIO,13,1
Delay 500
GPIO,12,0
GPIO,13,0
endon
On einsauf do
GPIO,4,1
GPIO,13,1
Delay 500
GPIO,4,0
GPIO,13,0
endon
On einszu do
GPIO,4,1
GPIO,12,1
Delay 1000
GPIO,4,0
GPIO,12,0
endon
On einsstop do
GPIO,4,1
GPIO,12,1
GPIO,13,1
Delay 500
GPIO,4,0
GPIO,12,0
GPIO,13,0
endon
On zweiauf do
GPIO,5,1
GPIO,13,1
Delay 500
GPIO,5,0
GPIO,13,0
endon
On zweizu do
GPIO,5,1
GPIO,12,1
Delay 500
GPIO,5,0
GPIO,12,0
endon
On zweistop do
GPIO,5,1
GPIO,12,1
GPIO,13,1
Delay 500
GPIO,5,0
GPIO,12,0
GPIO,13,0
endon
On dreiauf do
GPIO,4,1
GPIO,5,1
GPIO,13,1
Delay 500
GPIO,4,0
GPIO,5,0
GPIO,13,0
endon
On dreizu do
GPIO,4,1
GPIO,5,1
GPIO,12,1
Delay 500
GPIO,4,0
GPIO,5,0
GPIO,12,0
endon
On dreistop do
GPIO,4,1
GPIO,5,1
GPIO,12,1
GPIO,13,1
Delay 500
GPIO,4,0
GPIO,5,0
GPIO,12,0
GPIO,13,0
endon
Thank you a lot for your will to help me!

Re: sunrise/sunset to be used in rule

Posted: 03 Apr 2021, 11:52
by Metatron
This night, all the shutters went up again. I think a will take out all "On"-Events, to make sure, it is not a hardware issue.

Re: sunrise/sunset to be used in rule

Posted: 03 Apr 2021, 12:03
by TD-er
First a little tip, as you can have multiple matches based on the day of the week.

Code: Select all

On Clock#Time=All,06:00:00 do
   if %systime%<%sunrise-15m%
    if %sysweekday%=1 or %sysweekday%=7
      timerSet,1,3690
    else
      timerSet,1,90
    endif
   endif
endon
I do notice the documentation is a bit limited here, but the "All" can be one of these:
all
sun
mon
tue
wed
thu
fri
sat
wrk (weekday/workday)
wkd (weekend

Also make sure to check your DST and timezone and whether you have set your latitude/longitude coordinates as those are used for computing the sunrise/sunset.
I can't see why your setup might perform actions at midnight while it should do them at sunrise.
The only thing I can think of is that the new sunrise are not yet computed for the new day when the rules are being evaluated, but I've never heard any issue with that. (or a reboot after which the unit did not (yet) get time synced via NTP)

Re: sunrise/sunset to be used in rule

Posted: 03 Apr 2021, 13:48
by Ath
@Metatron I tried to analyze your set of rules, but as I can't comprehend how the GPIO's are connected, c.q. what they are supposed to do/control, it is very hard to understand. Can you elaborate on that?

Also, I don't understand the use of the delay commands, where you set a couple of GPIO's to 1, wait a short time, and turn them back to 0.

Re: sunrise/sunset to be used in rule

Posted: 06 Apr 2021, 12:13
by Metatron
Very strange, since I don't have a "On Clock#Time=All,06:30:00 do" type of entry, no confusing opening of the shutters anymore. (For some days now)
Has anyone an idea, why?
I believe to have proven, that it is not a hardware issue.

I have set ntp, timezone, coordinates, DST correctly, closing at sunset is working very good.
As I stated, no reboots.

So what do the rules do? The GPIOs are soldered to a remote control called ZB40 of shutters called Superrollo GW60.
Details can be found here: https://forum.fhem.de/index.php/topic,17125.0.html
Lets say, I "press buttons".
All the commands work fine, i.e. mqtt or REST.

I simply thought, I could use the rules and have a simple, self-sufficient solution.

Thanks to the hint with the weekend, I am new to ESPEasy.

Regards, Metatron

Re: sunrise/sunset to be used in rule

Posted: 06 Apr 2021, 13:01
by TD-er
If you're having issues with (remote) switches triggering on the remote, you may also have a look at how the GPIO pin is connected.
Is there a long wire between a switch and the GPIO pin?
Then the wire may act as an antenna and sending false pulses to the connected remote.

Re: sunrise/sunset to be used in rule

Posted: 06 Apr 2021, 13:12
by Ath
Metatron wrote: 06 Apr 2021, 12:13 Very strange, since I don't have a "On Clock#Time=All,06:30:00 do" type of entry, no confusing opening of the shutters anymore. (For some days now)
You could re-enable that rule but then using only HH:MM for the check like:

Code: Select all

On Clock#Time=All,06:30 do
It is a possibillity that 2 rules are clashing when running at almost the same time.

You haven't reported yet on what release of ESPEasy you are running your ESP, the name of the .bin file used would be most informative.

Re: sunrise/sunset to be used in rule

Posted: 06 Apr 2021, 13:22
by Metatron
Hello,
thanks, I will try with 06:30, or later, we have vacation. ;)

I also had problems, where clashing was impossible, only one opening scenario active.

as I said, using MQTT, triggering the shutters works fine, in all the cases.
I use
ESP_Easy_mega_20210223_normal_ESP8266_4M1M.bin
I use a NodeMCU LoLin

Re: sunrise/sunset to be used in rule

Posted: 11 Apr 2021, 22:16
by Metatron
Hello,

so far, it is working fine with 7:30. Thanks!
Now I start to make it more complicated, again.