Page 1 of 1
Exists a way to know if the day is holiday or business day?
Posted: Thursday 11 January 2018 10:05
by bigpea
Hi all,
I need to know if [today] is an holiday or business day because I want to change the behavior of my scripts in base of it.
I know that in DzVents doesn't exists a way (something like "domoticz.isHoliday", it's correct?), but exists an API (like google maps, WU, etc) or any other way?
Obviously, i'd like to have the holiday in my local calendar (italian).
Thanks.
Re: Exists a way to know if the day is holiday or business day?
Posted: Thursday 11 January 2018 10:20
by emme
as a workaround... what about a table like:
Code: Select all
isHoliday = {}
isHoliday['0101'] = 'New Year'
isHoliday['0104'] = 'Easter'
isHoliday['0105'] = 'Labour Day'
...
(maybe into an external file so all scripts can use it)
then, in your code
Code: Select all
if isHoliday[<today's date formatted as DDMM>] ~= nil then
domoticz.log('Hey... no rush...today is '..isHoliday[<today's date formatted as DDMM>]..'!!!!!')
else
domoticz.log('Come on Men... wake up!!')
end if
of course you will need to update it every year
it would be nice to include this into domoticz
Re: Exists a way to know if the day is holiday or business day?
Posted: Thursday 11 January 2018 10:37
by bigpea
yes, nice, but inconvenient to do this job every year..
Re: Exists a way to know if the day is holiday or business day?
Posted: Thursday 11 January 2018 10:45
by waaren
I use a bash script (triggered daily by cron) to do this.
relevant parts of the script:
Code: Select all
today=$(date --date="0 days ago " +"%-d-%-m-%Y")
json_return=$(curl -s http://www.kayaposoft.com/enrico/json/v1.0/?action=isPublicHoliday\&date=$today\&country=ita )
if $(echo $json_return | jq '.[]') ;then
curl -i -s -H "Accept: application/json" "http://domoticz-IP:domoticz-port/json.htm?type=command¶m=updateuservariable&vname=MyVarName&vtype=0&vvalue=1"
fi
you can change the json to domoticz to do other stuff eg change state of a virtual switch or change to a different timerplan as well
Re: Exists a way to know if the day is holiday or business day?
Posted: Thursday 11 January 2018 11:00
by bigpea
I like this, I will try it! Thanks.
Re: Exists a way to know if the day is holiday or business day?
Posted: Thursday 11 January 2018 11:05
by Siewert308SW
There are so many ways to get this job done.
I know some countries have variable days which are called as (bank) holiday.
Haven't took a look at that for my own country but some days are a easy calculation as they for example occur on a fixed day after some other day like Easter.
For fixed days i use a function which can be called like below:
edit: @waaren didn't know that url, thx
Code: Select all
function IsHoliday()
local monthNow = tonumber(os.date("%m"));
local dayNow = tonumber(os.date("%d"));
local holiday
if (dayNow == 27 and monthNow == 04) -- Koningsdag
or (dayNow == 04 and monthNow == 05) -- Bevrijdingdag
or (dayNow == 05 and monthNow == 05) -- Doden Herdenking
or (dayNow == 11 and monthNow == 11) -- Sint Maarten
or (dayNow == 05 and monthNow == 12) -- Sinterklaas
or (dayNow == 25 and monthNow == 12) -- 1e Kerstdag
or (dayNow == 26 and monthNow == 12) -- 2e kerstdag
or (dayNow == 31 and monthNow == 12) -- Oud jaarsdag
or (dayNow == 01 and monthNow == 01) -- Nieuw jaarsdag
then
holiday = 1
else
holiday = 0
end
return holiday
end
-- Call function holiday
holiday = IsHoliday()
commandArray = {}
if devicedchanged['someonehome'] == 'On'
and otherdevices['light'] == 'Off'
and otherdevices['holidaylight'] == 'Off'
and holiday == 0
then
commandArray['light']='On'
end
if devicedchanged['someonehome'] == 'On'
and otherdevices['light'] == 'Off'
and otherdevices['holidaylight'] == 'Off'
and holiday == 1
then
commandArray['light']='On'
commandArray['holidaylight']='On'
end
return commandArray
Re: Exists a way to know if the day is holiday or business day?
Posted: Thursday 11 January 2018 11:14
by emme
also found this:
https://holidayapi.com/
which require a registration
Re: Exists a way to know if the day is holiday or business day?
Posted: Friday 12 January 2018 15:58
by elmortero
Hi,
I have been looking into that a bit. Haven't found a LUA way yet but came up with this in Python
Based on
https://pypi.python.org/pypi/holidays
This looks up if the current date is a Public Holiday and prints the result to screen.
Following Python libraries need to be installed : datetime, calendar and holidays
Code: Select all
from datetime import date
#library datetime has to be installed
import holidays
#set your country
festivos = holidays.ES()
#get today's date (YYYY-MM-DD)
hoy = str(date.today())
dia = datetime.weekday(hoy)
#look up today
fiesta = str((festivos.get(hoy)))
msg = "Hoy es " + dia + hoy
print(msg)
if fiesta == "None":
print("No es festivo")
else:
print(fiesta)
After the weekend I will finish the rest:
The print command have to be replaced with a curl command to update a switch
And in the else-part I will include a nested IF to check if the current day of week is not a Saturday or Sunday because that might of course influence if we want to activate a virtual switch.
Re: Exists a way to know if the day is holiday or business day?
Posted: Saturday 13 January 2018 20:44
by elmortero
Update.
This is what I came up with.
You will also need the urllib2 so required are
* from Python: datetime, holidays, calendar and urllib2
* a virtual switch that will reflect if current day is a holiday or not
* optional, a virtual text device that will show what Holiday it is (if current day is a holiday)
* have crontab run the script once a day
I had a look at holiday.py to see if I could convert it to LUA (dzvents) but 2227 lines does not make me very eager
Code: Select all
from datetime import date
import holidays
import calendar
import urllib2
#set your country here. In this example ES for Spain
festivos = holidays.ES()
Hswitch=1127
Hname=1128
domoticzserver="192.168.1.6:8080"
#get today's date (YYYY-MM-DD)
hoy = date.today() #Comment this line when testing with following line Uncommented
#hoy = date(2018, 3, 30) #uncomment this line and put date that is a holiday (format = YYYY, M, D) to check if switch is updated
dia = calendar.day_name[hoy.weekday()]
#look up today in holidays
fiesta = str((festivos.get(hoy)))
fiesta = fiesta.replace(" ", "%20") #if holday name contains a space replace it with %20
if fiesta == "None":
fiesta = ""
print("No es festivo") #only visible in command line, for testing
urllib2.urlopen("http://" + str(domoticzserver) + "/json.htm?type=command¶m=switchlight&idx=" + str(Hswitch) + "&switchcmd=Off")
urllib2.urlopen("http://" + str(domoticzserver) + "/json.htm?type=command¶m=udevice&idx=" + str(Hname) + "&nvalue=0&svalue=" + str(fiesta))
else:
urllib2.urlopen("http://" + str(domoticzserver) + "/json.htm?type=command¶m=switchlight&idx=" + str(Hswitch) + "&switchcmd=On")
urllib2.urlopen("http://" + str(domoticzserver) + "/json.htm?type=command¶m=udevice&idx=" + str(Hname) + "&nvalue=0&svalue=" + str(fiesta))
print(fiesta) #only visible in command line, for testing
Re: Exists a way to know if the day is holiday or business day?
Posted: Saturday 13 January 2018 22:37
by bigpea
This is fantastic! I have to try it as soon as possible! Thanks.
Re: Exists a way to know if the day is holiday or business day?
Posted: Wednesday 25 April 2018 23:06
by TheCondor
Very nice plugin! I adapted for python 3.5 and for update a variable (string) instead a switch. Only holidays module is necessary, the other are already built with python35:
pip3.5 install holidays
Code: Select all
#!/usr/local/bin/python3.5
from datetime import date
import holidays
import calendar
import urllib.request, urllib.error, urllib.parse
#set your country here. In this example ES for Spain
festivos = holidays.IT()
#get today's date (YYYY-MM-DD)
hoy = date.today() #Comment this line when testing with following line Uncommented
#hoy = date(2018, 3, 30) #uncomment this line and put date that is a holiday (format = YYYY, M, D) to check if switch is updated
dia = calendar.day_name[hoy.weekday()]
#look up today in holidays
fiesta = str((festivos.get(hoy)))
fiesta = fiesta.replace(" ", "%20") #if holday name contains a space replace it with %20
if fiesta == "None":
fiesta = ""
print("No es festivo") #only visible in command line, for testing
urllib.request.urlopen("http://192.168.99.51:8080/json.htm?type=command¶m=updateuservariable&vname=Giorno&vtype=2&vvalue=Feriale")
else:
urllib.request.urlopen("http://192.168.99.51:8080/json.htm?type=command¶m=updateuservariable&vname=Giorno&vtype=2&vvalue=Festivo")
print(fiesta) #only visible in command line, for testing
Re: Exists a way to know if the day is holiday or business day?
Posted: Thursday 26 April 2018 1:16
by sion
I’m sure I read aboit (some time ago) a way to do this with ifttt - linked to google calendar.
Basically, if the day contains holiday, then switch a switch in domoticz. So it would also work if you had your own holidays / annual leave etc..
not looked into it further yet but it’s on my list
Re: Exists a way to know if the day is holiday or business day?
Posted: Sunday 29 April 2018 10:05
by TheCondor
Changed to default english and added a check. Now also if it's a normal sunday it appear as holiday.
Code: Select all
#!/usr/local/bin/python3.5
from datetime import date
import holidays
import calendar
import urllib.request, urllib.error, urllib.parse
#set your country here. In this example ES for Spain
holiday = holidays.IT()
#get today's date (YYYY-MM-DD)
today = date.today()
day = calendar.day_name[today.weekday()]
#look up today in holidays
bankholiday = str((holiday.get(today)))
bankholiday = bankholiday.replace(" ", "%20") #if holday name contains a space replace it with %20
if bankholiday == "None":
bankholiday = ""
if day == "Sunday":
urllib.request.urlopen("http://192.168.99.51:8080/json.htm?type=command¶m=updateuservariable&vname=Giorno_Festivo&vtype=2&vvalue=Si")
else:
urllib.request.urlopen("http://192.168.99.51:8080/json.htm?type=command¶m=updateuservariable&vname=Giorno_Festivo&vtype=2&vvalue=No")
else:
urllib.request.urlopen("http://192.168.99.51:8080/json.htm?type=command¶m=updateuservariable&vname=Giorno_Festivo&vtype=2&vvalue=Si")
Re: Exists a way to know if the day is holiday or business day?
Posted: Thursday 19 July 2018 17:02
by EdwinK
Can it update a text-box with the name of the holiday? Something like 'Today is Easter'.
Re: Exists a way to know if the day is holiday or business day?
Posted: Friday 20 July 2018 0:36
by waaren
EdwinK wrote: ↑Thursday 19 July 2018 17:02
Can it update a text-box with the name of the holiday? Something like 'Today is Easter'.
Please find an answer on your question
In this topic
Re: Exists a way to know if the day is holiday or business day?
Posted: Friday 20 July 2018 16:34
by EdwinK
Found it, did it, and works
Re: Exists a way to know if the day is holiday or business day?
Posted: Thursday 02 August 2018 15:15
by zak45
Re: Exists a way to know if the day is holiday or business day?
Posted: Sunday 24 February 2019 17:17
by ryanc9c
Calendarific allows you to get bank and public holidays information for over 230 countries via a simple json api. I think for what you want to do, you should be able to use it.
https://calendarific.com
https://calendarific.com/api-documentation