DzVents meets python to get holiday

Moderator: leecollings

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

DzVents meets python to get holiday

Post 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
}
Last edited by waaren on Friday 20 July 2018 10:27, edited 1 time in total.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: DzVents meets python to get holiday

Post 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 :-)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DzVents meets python to get holiday

Post 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
>>>
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: DzVents meets python to get holiday

Post 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.
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: DzVents meets python to get holiday

Post 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:
The most dangerous phrase in any language is:
"We always done this way"
bjacobse
Posts: 85
Joined: Tuesday 06 September 2016 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sorø, DK
Contact:

Re: DzVents meets python to get holiday

Post 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
bjacobse
Posts: 85
Joined: Tuesday 06 September 2016 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sorø, DK
Contact:

Re: DzVents meets python to get holiday

Post 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 3555 times
bjacobse
Posts: 85
Joined: Tuesday 06 September 2016 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sorø, DK
Contact:

Re: DzVents meets python to get holiday

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DzVents meets python to get holiday

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DzVents meets python to get holiday

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: DzVents meets python to get holiday

Post by EdwinK »

Well... Thanks. Going to see if I can get this work as well :)

Even *I* got it to work.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
bjacobse
Posts: 85
Joined: Tuesday 06 September 2016 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sorø, DK
Contact:

Re: DzVents meets python to get holiday

Post 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 3495 times
python -V
2.7.9
Last edited by bjacobse on Friday 20 July 2018 18:44, edited 1 time in total.
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: DzVents meets python to get holiday

Post 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.
bjacobse
Posts: 85
Joined: Tuesday 06 September 2016 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sorø, DK
Contact:

Re: DzVents meets python to get holiday

Post 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
bjacobse
Posts: 85
Joined: Tuesday 06 September 2016 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sorø, DK
Contact:

Re: DzVents meets python to get holiday

Post 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 3485 times
Now it's working - thank you guys for your support
bjacobse
Posts: 85
Joined: Tuesday 06 September 2016 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sorø, DK
Contact:

Re: DzVents meets python to get holiday

Post 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 3485 times
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DzVents meets python to get holiday

Post 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 3476 times
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
bjacobse
Posts: 85
Joined: Tuesday 06 September 2016 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sorø, DK
Contact:

Re: DzVents meets python to get holiday

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DzVents meets python to get holiday

Post 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 :)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: DzVents meets python to get holiday

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest