Condition: Day of Week AND Month of Year

Posted on
Tue Dec 01, 2015 9:17 pm
Ramias offline
Posts: 272
Joined: Nov 24, 2015

Condition: Day of Week AND Month of Year

The default conditions allow for "if date is between Dec 1 and Dec 31"

But I only want "If Day is a Sunday in December"

How would I do this as a script?

A similar ask is how would I script:

If Day is Monday - thru - Friday
and If Time is between 8am and 5pm then TRUE

If somebody can point me to a self-study or examples on this I'll try to learn myself, but figured I'd reach out here as well.

Thanks

Posted on
Tue Dec 01, 2015 10:05 pm
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Condition: Day of Week AND Month of Year

I did this kind of quickly, but it runs without errors and will give you an idea of how you might approach your task (there are many different ways that you could approach it--this is just a sample). Time and date functions can be a little tricky to get your head around, but once you get the hang of it, it's pretty much just like any other math.

Feel free to come back with any questions!

Dave

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

from datetime import datetime, time

day_of_week  = datetime.today().weekday()
time_of_day  = datetime.now().time()
month        = datetime.today().strftime("%B")
day          = datetime.today().strftime("%A")
day_of_month = datetime.today().strftime("%d")
year         = datetime.today().strftime("%Y")
fancy_date   = "%s %s, %s" % (month, day_of_month, year)
fancy_time   = "%s" % datetime.now().time().strftime("%I:%M")
hour         = "%s" % datetime.now().time().strftime("%I")
mil_hour     = "%s" % datetime.now().time().strftime("%H")
minute       = "%s" % datetime.now().time().strftime("%M")

print day_of_week
print time_of_day
print month
print day
print day_of_month
print year
print fancy_date
print fancy_time
print hour
print mil_hour
print minute
print

if day == "Sunday" and month == "December":
   print "Today is a Sunday in December"
else:
   print "Today is not a Sunday in December"
   
print
   
if day in ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday") and (8 <= mil_hour <= 17):
   x = True
else:
   x = False
   
print x


and the output:
Code: Select all
================================================================================
Dec 1, 2015, 10:03:46 PM
untitled text
--------------------------------------------------------------------------------
1
22:03:46.447755
December
Tuesday
01
2015
December 01, 2015
10:03
10
22
03

Today is not a Sunday in December

False

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Dec 01, 2015 10:26 pm
Ramias offline
Posts: 272
Joined: Nov 24, 2015

Re: Condition: Day of Week AND Month of Year

That's great. Definitely shows the various properties of datetime.

I get "Expected end of line, etc. but found "from". when I paste that code into the "If Script returns true" box of the conditions tab of my trigger.

How would I format a script for that section to return a true/false value?

Thanks again.

Posted on
Wed Dec 02, 2015 1:40 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Condition: Day of Week AND Month of Year

Try deleting the first two lines, so the first word is 'from', then replace the last line with 'return x'.

Peter


Sent from my iPhone using Tapatalk

Posted on
Wed Dec 02, 2015 4:25 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Condition: Day of Week AND Month of Year

Ramias wrote:
That's great. Definitely shows the various properties of datetime.

I get "Expected end of line, etc. but found "from". when I paste that code into the "If Script returns true" box of the conditions tab of my trigger.

How would I format a script for that section to return a true/false value?

Thanks again.

I've not used this feature in Indigo before (conditional scripts) but from the wiki, it appears that this feature may only support AppleScript. If that's true (sorry, no pun intended), you have two choices:

1. Run your conditional Python script as an embedded script under the Actions tab of your Trigger. This is what I would do.
2. Find someone that can turn your conditional script into an AppleScript (I'm not your guy for that I'm afraid.)

I can help you with option 1 if that's what you decide to do. Cheers,
Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests