Page 1 of 2

DzVents meets python to get holiday

Posted: Friday 20 July 2018 0:34
by waaren
triggered by a question @EdwinK asked in another topic on this forum. I played a bit with python to get an answer on the question "is today a public holiday ? "
Because I am kind of addicted to dzVents for the interaction between domoticz and the outside world, I tried to combine these two in the attached script.

Explanation is inside the script. Tested with the default python version (2.7) on my debian stretch PI3 system with domoticz V4.9788 and dzVents 2.4.7 but expect it to work with older / newer versions as well.

Have fun !

Code: Select all

--[[ pythonHoliday.lua 
    
    get Holiday string or "None" based on current date.
    
    Before the dzVents script can work the python holidays module needs to be imported on your system.
    Script is tested on dzVents 2.4.7 but will most likely also work on earlier versions
    
    This script use the result string from the python call to determine how to set a user-variable and 
    to update a text device but you can make other choices 
]]--

return {
    on      =   {   timer        = {"at 00:01"}},               -- Once a day right after midnight   
                    
    logging =   {   level     =   domoticz.LOG_ERROR,           -- change to LOG_DEBUG when needed
                    marker    =   "pythonHoliday"    },

    execute = function(dz, _)                           -- dz is short for domoticz; _ because we don't need trigger object
        -- Enter your settings below this line
        local country               = "NL"              --  other country abbreviations to be found at https://pypi.org/project/holidays/
        local holidayVarName        = "Holiday"         -- This var should be defined as type string
        local holidayTextDeviceIDX  =  992              -- This virtual text device should be be defined 
        local none                  = "geen feestdag."  -- Localized text
        
        -- local testDate           = "2018-12-25"      -- Use christmas as a test; uncomment to use this date 
        
        -- Enter your settings above this line
        
        function quote(str)                             -- Add double quotes around string
            return "\""..str.."\""
        end
        
        local mydate  = testDate or os.date('%F')  -- yyyy-MM-DD
        local shellCommand = "echo 'import holidays;holiday=holidays." ..      -- compose shellCommand
                             country ..
                             "(); print (holiday.get(" .. 
                             quote(mydate) .. 
                             "))' | python "
                             
        local filePointer  = io.popen(shellCommand, 'r')                        -- execute command and output to filePointer
        local resultString = filePointer:read('*a'):gsub("\n", "")              -- readvar and remove line break
        filePointer:close()                                                     
        
        dz.log("Return from python call: " .. mydate .. "  " .. resultString,dz.LOG_DEBUG)
        
        -- Just for lay-out purposes
        if resultString == "None" then
           resultString = none
        end   
        
        local fullText = os.date(" %A %d %B, %Y"):gsub(" 0"," ") .. " is " .. resultString 
        if testDate then
            fullText = mydate .. " is " .. resultString 
        end
        
        -- Update textdevice and uservariable
        dz.devices(holidayTextDeviceIDX).updateText(fullText) 
        dz.variables(holidayVarName).set(fullText)
    end
}

Re: DzVents meets python to get holiday

Posted: Friday 20 July 2018 2:02
by elmortero
Hi Waaren!

I haven't looked much into it as my country is not supported but might want to have a look here
https://kayaposoft.com/enrico/json/

Seems to be free, and this way you can go 100% dzvents :-)

Re: DzVents meets python to get holiday

Posted: Friday 20 July 2018 2:49
by waaren
elmortero wrote: Friday 20 July 2018 2:02 Hi Waaren!

I haven't looked much into it as my country is not supported but might want to have a look here
https://kayaposoft.com/enrico/json/

Seems to be free, and this way you can go 100% dzvents :-)
@elmortero, thx. I used this site before but decided to move to the python method over a year ago. ( I don't remember why) Since then I used the python holiday module and curl to update domoticz devices both wrapped in a bash script triggered by cron.
Today i revisited that setup and came up with this dzVents script.
For me it is part of the fun that many different ways are available to get things done.

If your country is Spain then it is supported in the python holiday module (even with many provinces:)
Spain ES prov = AND, ARG, AST, CAN, CAM, CAL, CAT, CVA, EXT, GAL, IBA, ICA, MAD, MUR, NAV, PVA, RIO

Just tested on the python CLI

Code: Select all

python3

Python 3.6.4 (default, Dec 25 2017, 18:06:53)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import holidays
>>> for date, name in sorted(holidays.Spain(years=2014).items()):
...     print(date, name)
...
2014-01-01 Año nuevo
2014-01-06 Epifanía del Señor
2014-04-18 Viernes Santo
2014-05-01 Día del Trabajador
2014-08-15 Asunción de la Virgen
2014-10-12 Día de la Hispanidad
2014-11-01 Todos los Santos
2014-12-06 Día de la constitución Española
2014-12-08 La Inmaculada Concepción
2014-12-25 Navidad

>>> for date, name in sorted(holidays.Spain(prov="cat", years=2019).items()):
...     print(date, name)
...
2019-01-01 Año nuevo
2019-01-06 Epifanía del Señor
2019-04-18 Jueves Santo
2019-04-19 Viernes Santo
2019-05-01 Día del Trabajador
2019-08-15 Asunción de la Virgen
2019-10-12 Día de la Hispanidad
2019-11-01 Todos los Santos
2019-12-06 Día de la constitución Española
2019-12-08 La Inmaculada Concepción
2019-12-25 Navidad
>>>

Re: DzVents meets python to get holiday

Posted: Friday 20 July 2018 8:16
by elmortero
waaren wrote: Friday 20 July 2018 2:49 If your country is Spain then it is supported in the python holiday module (even with many provinces:)
Spain ES prov = AND, ARG, AST, CAN, CAM, CAL, CAT, CVA, EXT, GAL, IBA, ICA, MAD, MUR, NAV, PVA, RIO
I know, I posted a script using the holiday module in the other thread :D

But now I also came to realise that you posted one kayaposoft :lol:
Still might be interesting, they've changed their API last spring.

Re: DzVents meets python to get holiday

Posted: Friday 20 July 2018 8:45
by emme

Code: Select all

local mydate  = testDate or os.date('%F')  -- yyyy-MM-DD
:o :o :o :o you changed my day with this OR in local var definition..... :lol: :lol:

Re: DzVents meets python to get holiday

Posted: Friday 20 July 2018 9:30
by bjacobse
waaren wrote: Friday 20 July 2018 2:49

Code: Select all

python3

Python 3.6.4 (default, Dec 25 2017, 18:06:53)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import holidays
>>> for date, name in sorted(holidays.Spain(years=2014).items()):
...     print(date, name)
...
2014-01-01 Año nuevo
2014-01-06 Epifanía del Señor
2014-04-18 Viernes Santo
2014-05-01 Día del Trabajador
2014-08-15 Asunción de la Virgen
2014-10-12 Día de la Hispanidad
2014-11-01 Todos los Santos
2014-12-06 Día de la constitución Española
2014-12-08 La Inmaculada Concepción
2014-12-25 Navidad

>>> for date, name in sorted(holidays.Spain(prov="cat", years=2019).items()):
...     print(date, name)
...
2019-01-01 Año nuevo
2019-01-06 Epifanía del Señor
2019-04-18 Jueves Santo
2019-04-19 Viernes Santo
2019-05-01 Día del Trabajador
2019-08-15 Asunción de la Virgen
2019-10-12 Día de la Hispanidad
2019-11-01 Todos los Santos
2019-12-06 Día de la constitución Española
2019-12-08 La Inmaculada Concepción
2019-12-25 Navidad
>>>
Thank you Waaren for the Python CLI example

Re: DzVents meets python to get holiday

Posted: Friday 20 July 2018 10:06
by bjacobse
Just curious where shall I put this code? I have added this to the event inside Domoticz, and put this as dzVent is that correct?
holiday.png
holiday.png (19.7 KiB) Viewed 3557 times

Re: DzVents meets python to get holiday

Posted: Friday 20 July 2018 10:23
by bjacobse
Hi waaren, I think you have missed the ]] to mark where text are ending

Code: Select all

-[[ pythonHoliday.lua 
    
    get Holiday string or "None" based on current date.
    
    Before the dzVents script can work the python holidays module needs to be imported on your system.
    Script is tested on dzVents 2.4.7 but will most likely also work on earlier versions
    
    This script use the result string from the python call to determine how to set a user-variable and 
    to update a text device but you can make other choices 
]]
]] shall be added so text is like above

Re: DzVents meets python to get holiday

Posted: Friday 20 July 2018 10:30
by waaren
bjacobse wrote: Friday 20 July 2018 10:23 Hi waaren, I think you have missed the ]] to mark where text are ending

]] shall be added so text is like above
Thx, copy/paste seems not te be my strongest point :oops: I updated the codeblock

Re: DzVents meets python to get holiday

Posted: Friday 20 July 2018 10:51
by waaren
bjacobse wrote: Friday 20 July 2018 10:06 Just curious where shall I put this code? I have added this to the event inside Domoticz, and put this as dzVent is that correct?
holiday.png
Yes. that is a correct way. The domoticz event editor will then write the file on save to: "domoticz_dir"/scripts/dzVents/generated_scripts/
from where it will be picked up by dzVents.

Other option is to save the script in "domoticz_dir"/scripts/dzVents/scripts/
dzVents also execute lua scripts from that location.

Please also have a look at Quickstart where this is described in more detail.

Re: DzVents meets python to get holiday

Posted: Friday 20 July 2018 16:10
by EdwinK
Well... Thanks. Going to see if I can get this work as well :)

Even *I* got it to work.

Re: DzVents meets python to get holiday

Posted: Friday 20 July 2018 18:38
by bjacobse
I just wanted to ensure it was correct to save the scripts as dzVents type of script.
I actually get some weird error from Domoticz log
The attachment log_error.png is no longer available
when I execute the CLI on my RPI as the above example:
log_error.png
log_error.png (7.66 KiB) Viewed 3497 times
python -V
2.7.9

Re: DzVents meets python to get holiday

Posted: Friday 20 July 2018 18:42
by elmortero
That is a syntax error in your script.
You will have to post it here if you want us to help you troubleshoot it.

Re: DzVents meets python to get holiday

Posted: Friday 20 July 2018 18:53
by bjacobse
elmortero wrote: Friday 20 July 2018 18:42 That is a syntax error in your script.
You will have to post it here if you want us to help you troubleshoot it.
Thanks, instead I copied the script from first topic again, this seemed to help, as I must have apparently unaware have written some garbage in the scripts

Re: DzVents meets python to get holiday

Posted: Friday 20 July 2018 19:15
by bjacobse
Still got an error, somehow I had not read that it was required to create a uservariable name Holiday as String
user_var.png
user_var.png (8.55 KiB) Viewed 3487 times
Now it's working - thank you guys for your support

Re: DzVents meets python to get holiday

Posted: Friday 20 July 2018 19:22
by bjacobse
This leads to my next question, how can I use this in a blockly scripts? I thought that Holiday would be a boolean, either true or false,
Holiday_var.png
Holiday_var.png (7.42 KiB) Viewed 3487 times

Re: DzVents meets python to get holiday

Posted: Friday 20 July 2018 19:59
by waaren
bjacobse wrote: Friday 20 July 2018 19:22 ... how can I use this in a blockly scripts? I thought that Holiday would be a boolean, either true or false,
domoticz does not support boolean type user variables but you can simulate this by defining one as an integer and set it to 1 (true) if it is a holiday and to 0 (false) otherwise.
Blockly.PNG
Blockly.PNG (47.22 KiB) Viewed 3478 times

Re: DzVents meets python to get holiday

Posted: Saturday 21 July 2018 22:24
by bjacobse
Thank you again Waaren,
I thought this wasn't possible as the Holiday variable is a string and the only way was to string compare the content, so I'm glad your example shows how easy it is

Re: DzVents meets python to get holiday

Posted: Sunday 22 July 2018 21:25
by waaren
elmortero wrote: Friday 20 July 2018 8:16 But now I also came to realise that you posted one kayaposoft :lol:
Still might be interesting, they've changed their API last spring.
Had another look at it. See the new method at http://www.domoticz.com/forum/viewtopi ... 72&t=24238
Now hope that there are XML coders that will take on the challenge of implementing the XML for Spain and its provinces.
They would only have to migrate that specific part of the python holiday module :)

Re: DzVents meets python to get holiday

Posted: Monday 23 July 2018 10:05
by elmortero
waaren wrote: Sunday 22 July 2018 21:25 Now hope that there are XML coders that will take on the challenge of implementing the XML for Spain and its provinces.
Been looking for that for a while now. I now understand why there is no support from Enrico/kayaposoft. So far I have not been able to find any usable table with these holidays for mor that 2 years ahead.
Which is a bit strange, some (or most) holidays are on fixed dates --> the easy part.
And also not found a way yet to calculate the variable ones.

If I ever figure that out, I might throw them in an xml myself :-D

Thanks for the effort, waaren