Calculation of daylight hours

Moderators: grovkillen, Stuntteam, TD-er

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

Calculation of daylight hours

#1 Post by marstu » 29 May 2021, 02:25

Hi all,

I am relatively new to ESPs and ESPEASY, but have already 3 ESP8266 incl. some small OLEDs and BMx280 running, which forward their information to Domoticz. This works quite well, with exceptions for one wrt WiFi connection issues since last update. Anyway this is not my topic, I'm recently trying to calculate the daylight hours either via rule or dummy, but can't find a working solution. My rule looks like this:

Code: Select all

On System#Boot do 
  timerSet,1,40 
endon

on Rules#Timer=1 do
 TaskValueSet 4,1,(%sunset%-%sunrise%)
endon
but is definitively not working as expected. Can someone give me a hint how to solve?

Thanks in advance

marstu

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

Re: Calculation of daylight hours

#2 Post by Ath » 29 May 2021, 09:49

Welcome to the wonderful world of ESPEasy :)
marstu wrote: 29 May 2021, 02:25 but is definitively not working as expected. Can someone give me a hint how to solve?
Please describe what is not working as expected...
/Ton (PayPal.me)

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

Re: Calculation of daylight hours

#3 Post by TD-er » 29 May 2021, 10:29

You can't use the time notation for calculations.
It isn't automatically converted to an int.

Would be a nice feature though.
Right now, I can't think of a very simple way to do so, as the %syssec_d% variable does not convert it.

As you can see on the bottom of the sysvars page on your ESP (if not running "testing" or "minimal" build), there are some conversion methods, but not yet one for %c_syssec_d%(12:34:56) for example.

One tricky way to do it, is to use the datetime command, but that's also messing up other functionality.

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

Re: Calculation of daylight hours

#4 Post by marstu » 29 May 2021, 10:41

TD-er wrote: 29 May 2021, 10:29 You can't use the time notation for calculations.
It isn't automatically converted to an int.

Would be a nice feature though.
Right now, I can't think of a very simple way to do so, as the %syssec_d% variable does not convert it.
That's a pity, then I will look for a solution in Domoticz.
TD-er wrote: 29 May 2021, 10:29 One tricky way to do it, is to use the datetime command, but that's also messing up other functionality.
Sounds not that easy, and with being new in scripting and rules it would take me a lot of time, but thanks for the hint.
--
ESP8266, ESPEasy (always latest mega release in use :D ), and Domoticz (only beta versions :) )

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

Re: Calculation of daylight hours

#5 Post by TD-er » 29 May 2021, 12:12

Well it might be relatively easy to add such a converter in the rules parser, but maybe even better to adjust the time notation to ints Will make the rules parsing a lot more consistent.

I will take it into account (unless someone like Ath may suddenly already implement the converter code as it is a oneliner in the ESPEasy code :)

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

Re: Calculation of daylight hours

#6 Post by Ath » 29 May 2021, 13:12

TD-er wrote: 29 May 2021, 12:12 Well it might be relatively easy to add such a converter in the rules parser, but maybe even better to adjust the time notation to ints Will make the rules parsing a lot more consistent.

I will take it into account (unless someone like Ath may suddenly already implement the converter code as it is a oneliner in the ESPEasy code :)
That's not what I'm currently working on :D though I probably could :lol:

For the time being I did a parser/converter in rules:

Code: Select all

on Time#Set do // Called when NTP updates the time
  logentry,"Sunrise: %sunrise% Sunset: %sunset%"
  let,20,{substring:0:1:"%sunrise%"}*60 // Assuming here that sunrise is always before 10:00
  let,20,[var#20]+{substring:2:5:"%sunrise%"}
  let,21,{substring:0:2:"%sunset%"}*60
  let,21,[var#21]+{substring:3:6:"%sunset%"}
  let,22,[var#21]-[var#20]
  let,23,[var#22]*60
  logentry,"Sun up time: [var#22] min. or [var#23] sec."
  taskvalueset,4,1,[var#23] // Sun up time in seconds, use #22 instead of #23 for minutes
endon
The quotes around %sunrise%/%sunset% are required as they contain a colon, and that is also the argument delimiter for substring...
/Ton (PayPal.me)

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

Re: Calculation of daylight hours

#7 Post by marstu » 29 May 2021, 16:44

Ath wrote: 29 May 2021, 13:12
That's not what I'm currently working on :D though I probably could :lol:

For the time being I did a parser/converter in rules:

Code: Select all

on Time#Set do // Called when NTP updates the time
  logentry,"Sunrise: %sunrise% Sunset: %sunset%"
  let,20,{substring:0:1:"%sunrise%"}*60 // Assuming here that sunrise is always before 10:00
  let,20,[var#20]+{substring:2:5:"%sunrise%"}
  let,21,{substring:0:2:"%sunset%"}*60
  let,21,[var#21]+{substring:3:6:"%sunset%"}
  let,22,[var#21]-[var#20]
  let,23,[var#22]*60
  logentry,"Sun up time: [var#22] min. or [var#23] sec."
  taskvalueset,4,1,[var#23] // Sun up time in seconds, use #22 instead of #23 for minutes
endon
The quotes around %sunrise%/%sunset% are required as they contain a colon, and that is also the argument delimiter for substring...
This works for me, well done, thanks.
--
ESP8266, ESPEasy (always latest mega release in use :D ), and Domoticz (only beta versions :) )

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

Re: Calculation of daylight hours

#8 Post by TD-er » 29 May 2021, 20:07

Ath wrote: 29 May 2021, 13:12 [...]
For the time being I did a parser/converter in rules:
[...]
Jij hackert :)

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

Re: Calculation of daylight hours

#9 Post by Ath » 30 May 2021, 15:38

Ath wrote: 29 May 2021, 13:12
TD-er wrote: 29 May 2021, 12:12 (unless someone like Ath may suddenly already implement the converter code as it is a oneliner in the ESPEasy code :)
That's not what I'm currently working on :D though I probably could :lol:
Well, I did :lol:, and opened PR #3661 to add 4 new system variables: %s_sunrise%, %m_sunrise%, %s_sunset% and %m_sunset%.
/Ton (PayPal.me)

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

Re: Calculation of daylight hours

#10 Post by marstu » 30 May 2021, 21:29

Ath wrote: 30 May 2021, 15:38
Well, I did :lol:, and opened PR #3661 to add 4 new system variables: %s_sunrise%, %m_sunrise%, %s_sunset% and %m_sunset%.
These new variables lead me to the next question. Would these then "solve" my problem in this rule?

Code: Select all

On System#Boot do 
  timerSet,1,40 
endon

on Rules#Timer=1 do
 TaskValueSet 4,1,(%sunset%-%sunrise%)
endon
--
ESP8266, ESPEasy (always latest mega release in use :D ), and Domoticz (only beta versions :) )

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

Re: Calculation of daylight hours

#11 Post by Ath » 30 May 2021, 21:51

marstu wrote: 30 May 2021, 21:29 These new variables lead me to the next question. Would these then "solve" my problem in this rule?
Your rules would then turn out into something like this:

Code: Select all

On System#Boot do 
  timerSet,1,40 
endon

on Rules#Timer=1 do
 TaskValueSet 4,1,%m_sunset%-%m_sunrise%
endon
Edit: If you want to present that value on an OLED or other display, you could add something like this: (displays "Sun up time 16h22m" for me, today)

Code: Select all

  ...
  let,24,%m_sunset%-%m_sunrise%
  oledframedcmd,1,"Sun up time: {substring:2:10:%c_m2dhm%([var#24])}" // Or use [taskname#varname] from your taskvalueset,4,1,... instead of [var#24]
  ...
But that will only work after the PR is merged into the mega (master) branch, and a new release is built from that.

What .bin file have you installed on your ESP's? (filename please), then I'll build and attach an updated one here for you to test.
/Ton (PayPal.me)

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

Re: Calculation of daylight hours

#12 Post by marstu » 30 May 2021, 22:02

Ath wrote: 30 May 2021, 21:51
What .bin file have you installed on your ESP's? (filename please), then I'll build and attach an updated one here for you to test.
I can wait until the next release. All of my ESPs are running currently on ESP_Easy_mega_20210503_normal_ESP8266_4M1M.
--
ESP8266, ESPEasy (always latest mega release in use :D ), and Domoticz (only beta versions :) )

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

Re: Calculation of daylight hours

#13 Post by Ath » 30 May 2021, 22:11

marstu wrote: 30 May 2021, 22:02 I can wait until the next release. All of my ESPs are running currently on ESP_Easy_mega_20210503_normal_ESP8266_4M1M.
Just to feed your curiosity please find attached that build, updated with this feature :)
Attachments
ESP_Easy_mega_20210530_normal_ESP8266_4M1M.zip
(580.19 KiB) Downloaded 164 times
/Ton (PayPal.me)

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

Re: Calculation of daylight hours

#14 Post by marstu » 30 May 2021, 22:34

Ath wrote: 30 May 2021, 22:11 Just to feed your curiosity please find attached that build, updated with this feature :)
Well done, thanks and too many good ideas. This one is working very well:

Code: Select all

on Time#Set do
 TaskValueSet 4,1,%m_sunset%-%m_sunrise%
endon
My initial rule doesn't work, i.e. nothing was sent to TaskValueSet 4,1.
Furthermore, I saw that in your version pressing the reboot button in tools lead to a not working (blank page) GUI after reboot. OLED is showing all information, but nothing is displayed in the browser window.
--
ESP8266, ESPEasy (always latest mega release in use :D ), and Domoticz (only beta versions :) )

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

Re: Calculation of daylight hours

#15 Post by TD-er » 30 May 2021, 22:41

How much free memory do you have? (RAM)

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

Re: Calculation of daylight hours

#16 Post by Ath » 30 May 2021, 23:22

That build is based on 'mega', not your latest PR
/Ton (PayPal.me)

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

Re: Calculation of daylight hours

#17 Post by marstu » 30 May 2021, 23:27

TD-er wrote: 30 May 2021, 22:41 How much free memory do you have? (RAM)
Issue solved after several reboots, but here is the requested information:

Sketch Size: 898 kB (2172 kB free)
Max. OTA Sketch Size: 1019 kB (1044464 bytes)
SPIFFS Size: 934 kB (843 kB free)
Page size: 256
Block size: 8192

I am fine with this now, and will keep this version for the moment on one of ESP's. For the moment it is fine, not to display on the OLED, because this "%m_sunset%-%m_sunrise%" is not working in the setup section of the device section. I tried something like: "Sun up time: %m_sunset%-%m_sunrise%" and it doesn't work like "Sunrise: %sunrise%". But not an issue, I would like to see it in Domoticz, and that's working.
--
ESP8266, ESPEasy (always latest mega release in use :D ), and Domoticz (only beta versions :) )

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

Re: Calculation of daylight hours

#18 Post by Ath » 31 May 2021, 07:33

The requested information is in the 'Memory' section of that same page, not the 'Storage' section.
/Ton (PayPal.me)

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

Re: Calculation of daylight hours

#19 Post by marstu » 31 May 2021, 12:05

Ath wrote: 31 May 2021, 07:33 The requested information is in the 'Memory' section of that same page, not the 'Storage' section.
Sorry, here you are:

Free RAM: 8760 (8256 - sendContentBlocking)
Heap Max Free Block: 7840
Heap Fragmentation: 11%
Free Stack: 3632 (880 - sendContentBlocking)
--
ESP8266, ESPEasy (always latest mega release in use :D ), and Domoticz (only beta versions :) )

Post Reply

Who is online

Users browsing this forum: No registered users and 34 guests