[solved] Calculate if today or tomorrow is a holiday

Posted on
Fri Apr 03, 2015 4:27 am
Nine offline
Posts: 78
Joined: Feb 15, 2015
Location: Zurich, Switzerland

[solved] Calculate if today or tomorrow is a holiday

I'm trying to check if today is a holiday to set a variable to true. For that I use the following python-script to detect:

Code: Select all
#! /usr/bin/env python2.5
# -*- coding: utf-8 -*-

#-----------------------------------------------------------------------------#
# Check if today is a holiday and return true
#
# Autor: Simon Betschmann
# Version: 1.0
# Date: 03.04.2015
#-----------------------------------------------------------------------------#

import datetime
from datetime import date
today = date.today()
tomorrow = today + datetime.timedelta(days=1)

def holidays(year):

 # reset the variable
 indigo.variable.updateValue(208372369, value="false") # "Feiertag_heute"
 indigo.variable.updateValue(857228602, value="false") # "Feiertag_morgen"
 indigo.variable.updateValue(671348986, value="") # "Feiertag_heute_text"
 indigo.variable.updateValue(1181470489, value="") # "Feiertag_morgen_text"

 hl = Holidays(year)
 holidays = hl.get_holiday_list()
 for h in holidays:
    print h[0]
    if today == h[0]:
        indigo.server.log(u'Today is a holiday: '+ h[1])
        indigo.variable.updateValue(208372369, value="true") # "Feiertag_heute"
        indigo.variable.updateValue(671348986, value=h[1])   # "Feiertag_heute_text"
    if tomorrow == h[0]:
        indigo.server.log(u'Today is a holiday: '+ h[1])
        indigo.variable.updateValue(857228602, value="true") # "Feiertag_morgen"
        indigo.variable.updateValue(1181470489, value=h[1])   # "Feiertag_morgen_text"

class Holidays:

 def __init__(self, year):
     self.year = int(year)
     easter_day = EasterDay(self.year)
     self.easter_date = easter_day.get_date()
     self.holiday_list = []
     self.general_public_holidays()

 def get_holiday_list(self):

     self.holiday_list.sort()
     return self.holiday_list

 def general_public_holidays(self):

     # Fix holidays
     self.holiday_list.append([datetime.date(self.year, 1, 1), u'Neujahr'])
     self.holiday_list.append([datetime.date(self.year, 1, 6), u'Heilige Drei Könige'])
     self.holiday_list.append([datetime.date(self.year, 5, 1), u'1. Mai'])
     self.holiday_list.append([datetime.date(today.year, 8, 1), u'Nationalfeiertag'])
     self.holiday_list.append([datetime.date(self.year, 12, 25), u'Erster Weihnachtsfeiertag'])
     self.holiday_list.append([datetime.date(self.year, 12, 26), u'Zweiter Weihnachtsfeiertag'])

     # Flexible holidays
     self.holiday_list.append([self.get_holiday(2, _type='minus'), u'Karfreitag'])
     self.holiday_list.append([self.easter_date, u'Ostersonntag'])
     self.holiday_list.append([self.get_holiday(1), u'Ostermontag'])
     self.holiday_list.append([self.get_holiday(49), u'Pfingstsonntag'])
     self.holiday_list.append([self.get_holiday(50), u'Pfingstmontag'])


 def get_holiday(self, days, _type='plus'):

     delta = datetime.timedelta(days=days)
     if _type == 'minus':
         return self.easter_date - delta
     else:
         return self.easter_date + delta

# ----------------------
# Calculate Easter date
# ---------------------
class EasterDay:

 def __init__(self, year):
     self.year = year

 def get_k(self):

     k = self.year / 100
     return k

 def get_m(self):

     k = self.get_k()
     m = 15 + (3 * k + 3) / 4 - (8 * k + 13) / 25
     return m

 def get_s(self):

     k = self.get_k()
     s = 2 - (3 * k + 3) / 4
     return s

 def get_a(self):

     a = self.year % 19
     return a

 def get_d(self):

     a = self.get_a()
     m = self.get_m()
     d = (19 * a + m) % 30
     return d

 def get_r(self):

     a = self.get_a()
     d = self.get_d()
     r = d / 29 + (d / 28 - d / 29) * (a / 11)
     return r

 def get_og(self):

     d = self.get_d()
     r = self.get_r()
     og = 21 + d - r
     return og

 def get_sz(self):

     s = self.get_s()
     sz = 7 - (self.year + self.year / 4 + s) % 7
     return sz

 def get_oe(self):

     og = self.get_og()
     sz = self.get_sz()
     oe = 7 - (og - sz) % 7
     return oe

 def get_os(self):

     og = self.get_og()
     oe = self.get_oe()
     os = og + oe
     return os

 def get_date(self):

     os = self.get_os()
     if os > 31:
         month = 4
         day = os - 31
     else:
         month = 3
         day = os
     easter_day = datetime.date(self.year, month, day)
     return easter_day


holidays(today.year)


If I execute this script in terminal, I get the output "Today is a holiday" (or not) correctly. How can I use this script in Indigo as a condition? It fails on "import datetime"... Expexted end of line , etc. but found identifier

Update: Updated my code due to the help of autolog. I use the script now not as the condition but as the action.
Update 2: Updated my code with the ability to check if tomorrow is a holiday and I store now the name of the holiday
Last edited by Nine on Fri Apr 03, 2015 6:26 am, edited 2 times in total.

Posted on
Fri Apr 03, 2015 5:23 am
autolog offline
Posts: 3990
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Using Python as a condition

Despite looking I can't find the relevant Indigo documentation but I suspect that it only allows Applescript and not Python. It could be an area that hasn't been updated yet?

I think this is the case because where you can use Python, Indigo normally lets you choose between Applescript and Python.

You don't say how you are trying to run this but assuming it is an a daily schedule, you might be better off including it as an action: Server Action > Script and File Actions > Execute Script.

You will also need to change your print statement to something like:
Code: Select all
indigo.server.log(u'Today is a holiday')

If you are trying to set a variable you could do that in your script :)

Example from Indigo variable documentation:
Code: Select all
indigo.variable.updateValue(123, value="New Value")

Posted on
Fri Apr 03, 2015 5:39 am
Nine offline
Posts: 78
Joined: Feb 15, 2015
Location: Zurich, Switzerland

[solved] Using Python as a condition

autolog wrote:
Despite looking I can't find the relevant Indigo documentation but I suspect that it only allows Applescript and not Python. It could be an area that hasn't been updated yet?

I think this is the case because where you can use Python, Indigo normally lets you choose between Applescript and Python.

You don't say how you are trying to run this but assuming it is an a daily schedule, you might be better off including it as an action: Server Action > Script and File Actions > Execute Script.

You will also need to change your print statement to something like:
Code: Select all
indigo.server.log(u'Today is a holiday')

If you are trying to set a variable you could do that in your script :)

Example from Indigo variable documentation:
Code: Select all
indigo.variable.updateValue(123, value="New Value")


Ahh... could be the solution. If I use the script in the 'Action' tab (yes, I use it in a daily shedule) and changed the print statement to indigo.server.log(u'Today is a holiday') it works :D

Thanks a lot!

Posted on
Sun Apr 05, 2015 4:14 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Using Python as a condition

autolog wrote:
Despite looking I can't find the relevant Indigo documentation but I suspect that it only allows Applescript and not Python. It could be an area that hasn't been updated yet?


Correct, Python scripts can't currently be used in Conditions, though as that post indicates you can accomplish the same thing by pushing the conditional logic down into a script action.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Sep 30, 2019 4:28 pm
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: [solved] Calculate if today or tomorrow is a holiday

Indigo 7.4 is now available and includes support for Python embedded conditional scripts (inside Triggers and Schedules).

Image

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 8 guests