Begin- and endTime of some astro events  [SOLVED]

Moderator: leecollings

User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Begin- and endTime of some astro events

Post by waaren »

Have fun !

Code: Select all

-- collect begin- and endtimes of some astro events. dzVents >= 2.4.19 (domoticz V4.10717)
local scriptVar = 'astro dzVents'

return {
        on =    {
                    timer = { 'at 00:03', 'at 06:03' }, -- twice a day to be sure and 3 min past the hour because at the hour already quite busy 
                    httpResponses = { scriptVar }
                },

     logging =  {
                    level   = domoticz.LOG_ERROR,
                    marker  = scriptVar
                },

    execute = function(dz, item)
        -- domoticz text devices. Comment lines for devices you don't have / want
        local devKeys = {   -- Device names                  -- Key  in data
                            ['Civil Twilight End']          = 'civil_twilight_end',
                            ['Civil Twilight Start']        = 'civil_twilight_begin',
                            ['Nautical Twilight End']       = 'nautical_twilight_begin',
                            ['Nautical Twilight Start']     = 'nautical_twilight_end',
                            ['Astronomical Twilight End']   = 'astronomical_twilight_begin',
                            ['Astronomical Twilight Start'] = 'astronomical_twilight_end',
                            ['Sunset']                      = 'sunset',
                            ['Sunrise']                     = 'sunrise',
                        } 

        local function TZOffset()  -- in seconds
            return os.difftime(os.time(), os.time(os.date('!*t'))) + (dz.time.isdst and 3600 or 0)
        end

        local function makeTime(str)
            local pattern = '(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)'
            local runyear, runmonth, runday, runhour, runminute, runseconds = str:match(pattern)
            local timestamp = os.time({year = runyear, month = runmonth, day = runday, hour = runhour, min = runminute, sec = runseconds})
            return (os.date('%X',timestamp + TZOffset())):sub(1,5)
        end    

        local function getAstroData()
            url = 'https://api.sunrise-sunset.org/json' ..
                            '?lat=' .. dz.settings.location.latitude ..
                            '&lng=' .. dz.settings.location.longitude ..
                            '&formatted=0'
                dz.openURL({ url = url, callback = scriptVar })
        end

        local function procesResults(t)
            for dv, field in pairs(devKeys) do
                if dz.devices(dv) and t[field] then
                    dz.devices(dv).updateText(makeTime(t[field]))
                end
            end
        end

        if item.isHTTPResponse then
            if item.ok then 
                procesResults(item.json.results)
            else
                dz.log('problem retrieving information from sunrise-sunset.org \n' .. item.data, dz.LOG_ERROR)
            end
        else
            getAstroData()
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Jubbes
Posts: 33
Joined: Wednesday 24 October 2018 14:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Begin- and endTime of some astro events

Post by Jubbes »

I get error warn:

2019-05-24 17:40:01.392 Status: dzVents: Error (2.4.19): astro dzVents: problem retrieving information from sunrise-sunset.org
2019-05-24 17:40:01.392
2019-05-24 17:40:01.288 Error: Error opening url: https://api.sunrise-sunset.org/json?lat ... ormatted=0
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Begin- and endTime of some astro events

Post by waaren »

Jubbes wrote: Friday 24 May 2019 17:45 I get error warn:

2019-05-24 17:40:01.392 Status: dzVents: Error (2.4.19): astro dzVents: problem retrieving information from sunrise-sunset.org
2019-05-24 17:40:01.392
2019-05-24 17:40:01.288 Error: Error opening url: https://api.sunrise-sunset.org/json?lat ... ormatted=0
Probably because you entered 0 in your settings for your location in the domoticz settings page. [SETUP] {SYSTEM} location
Please read this in the wiki
To enable domoticz and dzVents (and sunrise-sunset.org) calculating the right astro Event times they need your location (lon / lat)
If you don't want them in your system you have to enter them manually in the script.

But.. I did find a bug in the script (used the word 'start' where it should have been 'begin') and also found room for improvement by preventing a for loop. So please get a fresh copy from the (edited) first post.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Jubbes
Posts: 33
Joined: Wednesday 24 October 2018 14:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Begin- and endTime of some astro events

Post by Jubbes »

Hello waaren ,
the script doesn't run anymore. Can you please have a Look?
Installed ist the Last stable 2020.2

Thank you
Jubbes
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Begin- and endTime of some astro events

Post by waaren »

Jubbes wrote: Sunday 31 May 2020 17:55 the script doesn't run anymore. Can you please have a Look?
Installed ist the Last stable 2020.2
Works fine for me (last successful execution 06:03 this morning)

So
  • what do you see (also in the domoticz log) and what do you expect?
  • when did it still worked OK for you and what changed since then?
  • does other (dzVents) scripts still work?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Jubbes
Posts: 33
Joined: Wednesday 24 October 2018 14:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Begin- and endTime of some astro events

Post by Jubbes »

OK, thank you.
I will test it again tomorrow....
Jubbes
Posts: 33
Joined: Wednesday 24 October 2018 14:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Begin- and endTime of some astro events

Post by Jubbes »

Here is the error message:

2020-05-31 19:56:01.933 Error: Error opening url: https://api.sunrise-sunset.org/json?lat ... ormatted=0
2020-05-31 19:56:02.163 Error: dzVents: Error: (3.0.2) astro dzVents: HTTP/2 response: 400 ==>> No reason returned!
2020-05-31 19:56:02.164 Error: dzVents: Error: (3.0.2) astro dzVents: problem retrieving information from sunrise-sunset.org
2020-05-31 19:56:02.164 {"results":"","status":"INVALID_REQUEST"}
Jubbes
Posts: 33
Joined: Wednesday 24 October 2018 14:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Begin- and endTime of some astro events

Post by Jubbes »

Other dzVents scripts works fine
Jubbes
Posts: 33
Joined: Wednesday 24 October 2018 14:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Begin- and endTime of some astro events

Post by Jubbes »

Position:

Sonnenaufgang: 05:25, Sonnenuntergang: 21:36

Name:
Domoticz
Breitengrad:
50.758067
Längengrad:
6.840787

Settings for Location in Settings are entered.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Begin- and endTime of some astro events

Post by waaren »

Jubbes wrote: Sunday 31 May 2020 20:22 Settings for Location in Settings are entered.
it looks like dzVents cannot see your set lat / lon values. Can you please double check if they are set correctly and can you add the line

Code: Select all

dz.utils.dumpTable(dz.settings)
as first line after

Code: Select all

execute = function(dz, item)
And show the resulting loglines?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Jubbes
Posts: 33
Joined: Wednesday 24 October 2018 14:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Begin- and endTime of some astro events

Post by Jubbes »

2020-05-31 20:43:00.502 Status: dzVents: > Log level: 1
2020-05-31 20:43:00.502 Status: dzVents: > location:
2020-05-31 20:43:00.502 Status: dzVents: > longitude: 0
2020-05-31 20:43:00.503 Status: dzVents: > name: Domoticz
2020-05-31 20:43:00.503 Status: dzVents: > latitude: 0
2020-05-31 20:43:00.503 Status: dzVents: > dzVentsVersion: 3.0.2
2020-05-31 20:43:00.503 Status: dzVents: > domoticzVersion: 2020.2
2020-05-31 20:43:00.503 Status: dzVents: > webRoot:
2020-05-31 20:43:00.503 Status: dzVents: > serverPort: 8080
2020-05-31 20:43:00.503 Status: dzVents: > Domoticz url: http://127.0.0.1:8080
2020-05-31 20:43:02.072 Status: dzVents: > serverPort: 8080
2020-05-31 20:43:02.072 Status: dzVents: > Domoticz url: http://127.0.0.1:8080
2020-05-31 20:43:02.072 Status: dzVents: > Log level: 1
2020-05-31 20:43:02.072 Status: dzVents: > webRoot:
2020-05-31 20:43:02.072 Status: dzVents: > dzVentsVersion: 3.0.2
2020-05-31 20:43:02.073 Status: dzVents: > location:
2020-05-31 20:43:02.073 Status: dzVents: > name: Domoticz
2020-05-31 20:43:02.073 Status: dzVents: > longitude: 0
2020-05-31 20:43:02.073 Status: dzVents: > latitude: 0
2020-05-31 20:43:02.073 Status: dzVents: > domoticzVersion: 2020.2
Jubbes
Posts: 33
Joined: Wednesday 24 October 2018 14:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Begin- and endTime of some astro events

Post by Jubbes »

Longitude and latitude = 0
Jubbes
Posts: 33
Joined: Wednesday 24 October 2018 14:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Begin- and endTime of some astro events

Post by Jubbes »

2020-05-31 20:49:02 ☀▲05:25 ▼21:36

The times in the dashboard are correct
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Begin- and endTime of some astro events

Post by waaren »

Jubbes wrote: Sunday 31 May 2020 20:50 The times in the dashboard are correct
You probably have not met this requirement yet.
Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz.
local.png
local.png (13.1 KiB) Viewed 2950 times
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Jubbes
Posts: 33
Joined: Wednesday 24 October 2018 14:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Begin- and endTime of some astro events

Post by Jubbes »

2020-05-31 21:44:00.652 Status: dzVents: > serverPort: 8080
2020-05-31 21:44:00.652 Status: dzVents: > Domoticz url: http://127.0.0.1:8080
2020-05-31 21:44:00.652 Status: dzVents: > location:
2020-05-31 21:44:00.652 Status: dzVents: > latitude: 50.758067
2020-05-31 21:44:00.652 Status: dzVents: > longitude: 6.840787
2020-05-31 21:44:00.652 Status: dzVents: > name: Domoticz
2020-05-31 21:44:00.652 Status: dzVents: > webRoot:
2020-05-31 21:44:00.653 Status: dzVents: > domoticzVersion: 2020.2
2020-05-31 21:44:00.653 Status: dzVents: > Log level: 1
2020-05-31 21:44:00.653 Status: dzVents: > dzVentsVersion: 3.0.2
2020-05-31 21:44:02.247 Status: dzVents: > webRoot:
2020-05-31 21:44:02.247 Status: dzVents: > serverPort: 8080
2020-05-31 21:44:02.247 Status: dzVents: > Domoticz url: http://127.0.0.1:8080
2020-05-31 21:44:02.247 Status: dzVents: > dzVentsVersion: 3.0.2
2020-05-31 21:44:02.248 Status: dzVents: > location:
2020-05-31 21:44:02.248 Status: dzVents: > name: Domoticz
2020-05-31 21:44:02.248 Status: dzVents: > latitude: 50.758067
2020-05-31 21:44:02.248 Status: dzVents: > longitude: 6.840787
2020-05-31 21:44:02.248 Status: dzVents: > domoticzVersion: 2020.2
2020-05-31 21:44:02.248 Status: dzVents: > Log level: 1
Jubbes
Posts: 33
Joined: Wednesday 24 October 2018 14:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Begin- and endTime of some astro events  [SOLVED]

Post by Jubbes »

That it was waaren. Thank you very much.
Viele Grüße
Jubbes
Nefsolive
Posts: 69
Joined: Monday 04 September 2017 17:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Begin- and endTime of some astro events

Post by Nefsolive »

Hello waaren,

The script works great! Thank u.

Is there a possibility in the script to get 'day_length' from the api? I tried but it gives error! As it does not recognize the AM or PM, it gives an error.

Ty
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Begin- and endTime of some astro events

Post by waaren »

Nefsolive wrote: Thursday 10 December 2020 17:27 Is there a possibility in the script to get 'day_length' from the api? I tried but it gives error! As it does not recognize the AM or PM, it gives an error.
You could use something like

Code: Select all

local dayLengthInMinutes = dz.time.sunsetInMinutes - dz.time.sunriseInMinutes
No AM or PM in there..
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Nefsolive
Posts: 69
Joined: Monday 04 September 2017 17:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Begin- and endTime of some astro events

Post by Nefsolive »

waaren wrote: Thursday 10 December 2020 17:59
Nefsolive wrote: Thursday 10 December 2020 17:27 Is there a possibility in the script to get 'day_length' from the api? I tried but it gives error! As it does not recognize the AM or PM, it gives an error.
You could use something like

Code: Select all

local dayLengthInMinutes = dz.time.sunsetInMinutes - dz.time.sunriseInMinutes
No AM or PM in there..
Thanks for the answer!
But i still have this error:
Error: dzVents: Error: (3.0.2) astro dzVents: ...icz/scripts/dzVents/generated_scripts/Eventos do Dia.lua:36: attempt to index a number value (local 'str')

Ty
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Begin- and endTime of some astro events

Post by waaren »

Nefsolive wrote: Thursday 10 December 2020 18:19 But i still have this error:
Error: dzVents: Error: (3.0.2) astro dzVents: ...icz/scripts/dzVents/generated_scripts/Eventos do Dia.lua:36: attempt to index a number value (local 'str')
I assume you changed the script because in the version I posted there is no variable "str" in line 36.
If so there is no way I can help without you sharing the script as it is now.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest