Page 1 of 1
Handling Holidays
Posted:
Mon Sep 04, 2017 7:37 am
by Different Computers
Who has a clever way of handling holidays like today--ones that aren't on a certain date every year. While my dog will only let me sleep so late regardless, I could get another 30 minutes in if my "People are up now so I'm changing the house mode" trigger knew to ignore someone moving around at 7 am on a holiday.
Ways I want to avoid using:
pressing a "Tomorrow is a holiday" button.
manually adding a bunch of date conditions to triggers, unless any trigger working that way is referring to some script magic.
Re: Handling Holidays
Posted:
Mon Sep 04, 2017 8:44 am
by FlyingDiver
Run this script every night just after midnight. I created the two variables in advance. Creating them at run time is an exercise for the student.
- Code: Select all
# indigo.variables[1980497029] # "isHoliday"
# indigo.variables[1479852422] # "whatHoliday"
from datetime import date
import holidays
indigo.variable.updateValue(1980497029, value="False")
us_holidays = holidays.UnitedStates() # or holidays.US()
if date.today() in us_holidays:
indigo.variable.updateValue(1980497029, value="True")
indigo.variable.updateValue(1479852422, us_holidays.get(date.today()))
Re: Handling Holidays
Posted:
Mon Sep 04, 2017 9:25 am
by jay (support)
I don't think holidays is a standard python module is it? You'll probably have to install it first.
Re: Handling Holidays
Posted:
Mon Sep 04, 2017 9:28 am
by FlyingDiver
jay (support) wrote:I don't think holidays is a standard python module is it? You'll probably have to install it first.
Could be. It's been a while since I wrote that.
Re: Handling Holidays
Posted:
Mon Sep 04, 2017 11:34 am
by racarter
If you don't want to install the holidays module you could make a variable called 'Holiday' or whatever you'd like to call it, then run this script once per day:
- Code: Select all
import urllib2
import requests
import simplejson as json
from datetime import datetime
bhVar = indigo.variables[77648381] #Change this to your Holiday variable ID
thisDay = datetime.today().strftime('%Y-%m-%d')
url = 'http://www.webcal.fi/cal.php?id=435&format=json&start_year=current_year&end_year=current_year&tz=Europe%2FLondon'
resp = requests.get(url=url)
data = json.loads(resp.text)
holDesc = ""
for i in data:
if i['date'] == thisDay:
holDesc = i['name']
break
indigo.variable.updateValue(bhVar, holDesc)
The url shown is for UK but a visit to
http://www.webcal.fi/en-GB/other_file_formats.php will enable you to create a url for anywhere in the world. (Be sure to select the json option.) If today is a holiday the variable will be set to its name, otherwise it will be left an empty string. A trigger could be created to do something when the variable is not empty.
Re: Handling Holidays
Posted:
Mon Sep 04, 2017 12:36 pm
by jay (support)
racarter wrote:If you don't want to install the holidays module you could make a variable called 'Holiday' or whatever you'd like to call it, then run this script once per day:
[SNIP]
The url shown is for UK but a visit to
http://www.webcal.fi/en-GB/other_file_formats.php will enable you to create a url for anywhere in the world. (Be sure to select the json option.) If today is a holiday the variable will be set to its name, otherwise it will be left an empty string. A trigger could be created to do something when the variable is not empty.
Excellent! I always like solutions that don't require installing Python modules. I found that website somewhat confusing, so to help avoid that here are the steps I took (if they don't redesign the website):
- Click on Other file formats in the navigation menu to the left
- Select Holidays from the Calendar popup (or adjust as necessary depending on what you want)
- Select JSON - JavaScript Object Notation from the File format popup
- Select your timezone from the Time zone popup
- Click the Show download URL button
- Copy the URL shown in red and paste it into the script in place of the url variable in the script (2nd line of my script below)
I simplified the script somewhat - both things that need customization are at the top of the script:
- Code: Select all
#### Replace this URL with the one you got from the website above and replace the variable ID with the ID of your variable
url='http://www.webcal.fi/cal.php?id=52&format=json&start_year=current_year&end_year=current_year&tz=America%2FChicago'
variable_id=REPLACE_WITH_VARIABLE_ID
####
import requests
from datetime import date
resp = requests.get(url)
data = resp.json()
holDesc = ""
for i in data:
if i['date'] == str(date.today()):
holDesc = i['name']
break
indigo.variable.updateValue(variable_id, value=holDesc)
I love this, thanks @racarter!
Re: Handling Holidays
Posted:
Mon Sep 04, 2017 1:00 pm
by Different Computers
Very cool stuff! Gotta be careful though: Lots of people don't get things like Columbus Day off. Maybe there's a way to make it look at a custom web calendar URL.
Also shame on me for asking such a question on Labor Day, when Jay should be relaxing!
Re: Handling Holidays
Posted:
Mon Sep 04, 2017 1:13 pm
by racarter
You could always modify the script to ignore specific holidays you don't want flagging...
Re: Handling Holidays
Posted:
Wed Jun 13, 2018 1:42 pm
by noel1983
Just getting around to implementing this.
Ideally I want to run this in the evening, before bedtime to check if its a bank holiday the following day. Is it easy to amend it for 'tomorrow'?
Thanks
Noel
Re: Handling Holidays
Posted:
Wed Jun 13, 2018 2:13 pm
by FlyingDiver
Change:
- Code: Select all
if i['date'] == str(date.today()):
to:
- Code: Select all
if i['date'] == str(date.today() + timedelta(days=1)):
and
- Code: Select all
from datetime import date
to:
- Code: Select all
from datetime import date, timedelta
Should do it. Not tested at all....
Re: Handling Holidays
Posted:
Wed Jun 13, 2018 2:14 pm
by noel1983
Awesome thank you!
Sent from my iPhone using Tapatalk