Page 1 of 1

Setting a variable for Weekend

PostPosted: Sat Dec 13, 2014 4:51 pm
by miketeel
I'd like to run a script only on weekend. Is there easy way to set and maintain a boolean variable that would be true on weekends?

Re: Setting a variable for Weekend

PostPosted: Sat Dec 13, 2014 5:30 pm
by DaveL17
miketeel wrote:
I'd like to run a script only on weekend. Is there easy way to set and maintain a boolean variable that would be true on weekends?

Here's a copy of the code that I use. There's more here than you need, but what you want is in there.

Code: Select all
from datetime import datetime, time

# ==== Globals ====
day_of_week = datetime.today().weekday()
time_of_day = datetime.now().time()
month = datetime.today().strftime("%B")
day = datetime.today().strftime("%A")
dayOfMonth = datetime.today().strftime("%d")
year = datetime.today().strftime("%Y")
fancyDate = "%s %s, %s" % (month, dayOfMonth, year)
fancyTime = "%s" % datetime.now().time().strftime("%I:%M")

indigo.variable.updateValue(171889768, day)
indigo.variable.updateValue(120808817, month)
indigo.variable.updateValue(692601213, dayOfMonth)
indigo.variable.updateValue(1376792491, year)
indigo.variable.updateValue(1356967768, fancyDate)
indigo.variable.updateValue(1150196462, fancyTime)

# ==== Weekday or Weekend? ====
if 0 <= day_of_week <= 4:
   weekday = u'Weekday'
else:
   weekday = u'Weekend'
indigo.variable.updateValue(1663542915, weekday)

# ==== AM or PM? ====
if time(00,00) <= time_of_day <= time(11,59):       
   am_pm = u'AM'
else:
   am_pm = u'PM'
indigo.variable.updateValue(949417705, am_pm)
Dave

Re: Setting a variable for Weekend

PostPosted: Sat Dec 13, 2014 6:45 pm
by jay (support)
What causes the script to run exactly? Is it a trigger or schedule? The solution might be less complex than Dave's reply but I don't know until I get some context.

Re: Setting a variable for Weekend

PostPosted: Sat Dec 13, 2014 6:53 pm
by DaveL17
Here's the simpler version.

Code: Select all
from datetime import datetime, time

if 0 <= datetime.today().weekday() <= 4:
   weekday = u'False'
else:
   weekday = u'True'
indigo.variable.updateValue(VARIABLE ID, weekday)

Re: Setting a variable for Weekend

PostPosted: Sat Dec 13, 2014 11:26 pm
by berkinet
I may be missing something here... But why not just schedule an action for some time Friday night or Saturday AM with an action to set your "weekend" variable true, and another scheduled action for Sunday night/ monday AM to set it false again.

Re: Setting a variable for Weekend

PostPosted: Sun Dec 14, 2014 12:04 am
by jay (support)
There might be several different and easier approaches - depending on the exact nature of the problem being solved... :wink:

Re: Setting a variable for Weekend

PostPosted: Sun Dec 14, 2014 5:44 am
by DaveL17
I use my dateTime script to update variable values for several different things. The most important is HVAC logic which accounts for weekday/weekend, AM/PM, home/away, holidays, and vacations. I also create some formatted strings for control pages.

Dave

Re: Setting a variable for Weekend

PostPosted: Sun Dec 14, 2014 9:53 am
by roussell
Here's a snippet from a larger time/day applescript that I have that runs at midnight every night:

Code: Select all
set weekend to {Saturday, Sunday}
if weekday of (current date) is in weekend then
   set value of variable "Weekend" to "Yes"
else
   set value of variable "Weekend" to "No"
end if

Re: Setting a variable for Weekend

PostPosted: Sun Dec 14, 2014 10:38 pm
by miketeel
Thanks, Russell, your short script worked like a charm.