Handling Holidays

Posted on
Mon Sep 04, 2017 7:37 am
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Handling Holidays

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.

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Mon Sep 04, 2017 8:44 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Handling Holidays

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()))


joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Sep 04, 2017 9:25 am
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Handling Holidays

I don't think holidays is a standard python module is it? You'll probably have to install it first.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Sep 04, 2017 9:28 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Handling Holidays

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.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Sep 04, 2017 11:34 am
racarter offline
User avatar
Posts: 468
Joined: Jun 18, 2016
Location: North Yorkshire, UK

Re: Handling Holidays

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.

Posted on
Mon Sep 04, 2017 12:36 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Handling Holidays

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):

  1. Click on Other file formats in the navigation menu to the left
  2. Select Holidays from the Calendar popup (or adjust as necessary depending on what you want)
  3. Select JSON - JavaScript Object Notation from the File format popup
  4. Select your timezone from the Time zone popup
  5. Click the Show download URL button
  6. 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!

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Sep 04, 2017 1:00 pm
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Re: Handling Holidays

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!

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Mon Sep 04, 2017 1:13 pm
racarter offline
User avatar
Posts: 468
Joined: Jun 18, 2016
Location: North Yorkshire, UK

Re: Handling Holidays

You could always modify the script to ignore specific holidays you don't want flagging...

Posted on
Wed Jun 13, 2018 1:42 pm
noel1983 offline
Posts: 446
Joined: Oct 17, 2014

Re: Handling Holidays

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

Posted on
Wed Jun 13, 2018 2:13 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Handling Holidays

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....

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Jun 13, 2018 2:14 pm
noel1983 offline
Posts: 446
Joined: Oct 17, 2014

Re: Handling Holidays

Awesome thank you!


Sent from my iPhone using Tapatalk

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 5 guests