Convert script to Set Time Date and Season to Python

Posted on
Sat Aug 24, 2019 9:34 am
wideglidejrp offline
User avatar
Posts: 555
Joined: Jan 15, 2012
Location: Danbury, CT

Convert script to Set Time Date and Season to Python

The following is the last Apple Script in my system. It was cobbled together from various sources but, surprisingly, it has worked well for years. Any suggestions to simplify this into Python greatly appreciated.

Code: Select all
set value of variable "pennwood_date" to (items 1 thru -7 of (date string of (current date)) as text) as string
# The text on the next line came from Niel
# (items 1 thru -7 of (date string of (current date)) as text) as string
# at https://discussions.apple.com/message/31262688?ac_cid=op123456#31262688
set value of variable "pennwood_day" to (weekday of (current date)) as text
set value of variable "pennwood_month" to (month of (current date)) as text
# The following lines were obtained from an Apple discussion group
# https://discussions.apple.com/message/31262688?ac_cid=op123456#31262688
set theDate to (current date)
set isoDate to theDate as «class isot» as string -- get current iso date/time
log isoDate
set value of variable "pennwood_time" to isoTimeTo12Hour(isoDate)
isoTimeTo12Hour(isoDate)
on isoTimeTo12Hour(isoDateString) -- convert 24-hour time to AM/PM
   set meridiem to " AM"
   set theHour to text 12 thru 13 of isoDateString as number
   if theHour = 12 then set meridiem to " PM"
   if theHour = 0 then set theHour to 12
   if theHour > 12 then set {theHour, meridiem} to {theHour - 12, " PM"}
   return "" & theHour & text 14 thru 16 of isoDateString & meridiem
end isoTimeTo12Hour
# end of script from Apple discussion group
set spring to {April, May}
set summer to {June, July, August}
set fall to {September, October, November}
set winter to {December, January, February, March}
if month of (current date) is in spring then
   set value of variable "pennwood_season" to "Spring"
else if month of (current date) is in summer then
   set value of variable "pennwood_season" to "Summer"
else if month of (current date) is in fall then
   set value of variable "pennwood_season" to "Fall"
else if month of (current date) is in winter then
   set value of variable "pennwood_season" to "Winter"
end if

John R Patrick
Author of
Home Attitude

Posted on
Sun Aug 25, 2019 4:01 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Convert script to Set Time Date and Season to Python

That script will take some effort to grok. Can you just tell us what you want the end result of the script to be (I can see some of it, but I don't really want to try and figure out what all the ISO stuff is about). My suspicion is that all it'll take is using the Python date/time formatters rather than parsing strings (dates are much easier to deal with in Python).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Aug 25, 2019 6:49 pm
wideglidejrp offline
User avatar
Posts: 555
Joined: Jan 15, 2012
Location: Danbury, CT

Re: Convert script to Set Time Date and Season to Python

I think you are exactly right. The solution I currently have is much more complex than needed. The purpose of the script is to produce variables which I use for setting thermostats and providing input to Polly "good morning and good evening" announcements. The specific goal of the script is to produce the needed variables as show in the following screenshot. The format of the variables needs to be as shown.
Attachments
Screenshot 2019-08-25 18.37.22.png
Screenshot 2019-08-25 18.37.22.png (23.75 KiB) Viewed 4479 times

John R Patrick
Author of
Home Attitude

Posted on
Sun Aug 25, 2019 7:30 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Convert script to Set Time Date and Season to Python

Code: Select all
from datetime import datetime

now = datetime.now()
pennwood_date  = now.strftime("%A, %B %-d")
print pennwood_date
pennwood_day   = now.strftime("%A")
print pennwood_day
pennwood_month = now.strftime("%B")
print pennwood_month
pennwood_time  = now.strftime("%-I:%M %p")
print pennwood_time
 
seasons = {
    "January": "Winter",
    "February": "Winter",
    "March": "Winter",
    "April": "Spring",
    "May": "Spring",
    "June": "Summer",
    "July": "Summer",
    "August": "Summer",
    "September": "Fall",
    "October": "Fall",
    "November": "Fall",
    "December": "Winter"
    }
   
pennwood_season = seasons[pennwood_month]
print pennwood_season


Replace the print statements with lines to set the appropriate Indigo variable. You have that from previous scripts. I used print to check the output.

See https://docs.python.org/2/library/datet ... e.datetime for the definitions of the formatters.

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

Posted on
Mon Aug 26, 2019 8:03 am
wideglidejrp offline
User avatar
Posts: 555
Joined: Jan 15, 2012
Location: Danbury, CT

Re: Convert script to Set Time Date and Season to Python

Thanks very much. The Python script makes mince meat out of the Apple Script. It works perfectly. I looked through the reference link for Basic date and time types. Now I see the superiority of Python vs. Apple Script. AS is very simple but Python is incredibly comprehensive. I think I am AS free now but I will look through my action groups. There may be one or two lurking in there. Thanks again for the assistance.

John R Patrick
Author of
Home Attitude

Posted on
Tue Jul 14, 2020 10:46 am
wideglidejrp offline
User avatar
Posts: 555
Joined: Jan 15, 2012
Location: Danbury, CT

Re: Convert script to Set Time Date and Season to Python

I have a converted Apple script for handling my thermostats. It works fine, but I want to streamline it with the addition of defined function. I am getting an indentation error on line six. I don't see what is wrong. It is the same as how I learned in tutorial. Any comments appreciated...

Code: Select all
# Set thermostats to Heat if dining room temperature is less than 60 else set them to Cool
 
def finish_up():
    current_temp = float(indigo.variables[1451762256].value)
    indigo.server.log("Current temperature outdoors: " + str(current_temp))   
    if current_temp < 60:
        indigo.thermostat.setHvacMode(1383655983, value=indigo.kHvacMode.Heat)
        indigo.thermostat.setHvacMode(1229008572, value=indigo.kHvacMode.Heat)
        indigo.thermostat.setHvacMode(1125058629, value=indigo.kHvacMode.Heat)   
    else:
        indigo.thermostat.setHvacMode(1383655983, value=indigo.kHvacMode.Cool)
        indigo.thermostat.setHvacMode(1229008572, value=indigo.kHvacMode.Cool)
        indigo.thermostat.setHvacMode(1125058629, value=indigo.kHvacMode.Cool)
#

John R Patrick
Author of
Home Attitude

Posted on
Tue Jul 14, 2020 2:43 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Convert script to Set Time Date and Season to Python

I don't see any syntax error - of course I can't execute it exactly as yours. Also, that script by itself does nothing - you actually have to call the function. Questions:

  1. Where is this function defined?
  2. Where are you calling it from?
  3. When do you get told that there's a syntax error?

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Jul 15, 2020 6:44 pm
wideglidejrp offline
User avatar
Posts: 555
Joined: Jan 15, 2012
Location: Danbury, CT

Re: Convert script to Set Time Date and Season to Python

This is a test of a function. It will be part of a much larger script. I can't get past this error about an indentation problem. I tried the script on my tutorial workspace and got the same error. I cannot find anything wrong with it, but Indigo and tutorial space flag the error.

Code: Select all
# Set thermostats to Heat if dining room temperature is less than 60 else set them to Cool
 
def finish_up():
    current_temp = 60
    print(current_temp)   
    if current_temp < 60:
        mode = "Heat"
    else:
        mode = "Cool"
finish_up
print(mode)
print("The function has completed its job")
#

John R Patrick
Author of
Home Attitude

Posted on
Wed Jul 15, 2020 7:56 pm
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Convert script to Set Time Date and Season to Python

By making a few subtle changes to get it to run, this script runs without error for me.

Code: Select all
# Set thermostats to Heat if dining room temperature is less than 60 else set them to Cool
 
def finish_up():
    current_temp = 60
    indigo.server.log(str(current_temp))   
    if current_temp < 60:
        mode = "Heat"
    else:
        mode = "Cool"
mode = finish_up()
indigo.server.log(mode)
indigo.server.log("The function has completed its job")
#

Hope this helps.

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

[My Plugins] - [My Forums]

Posted on
Thu Jul 16, 2020 8:03 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Convert script to Set Time Date and Season to Python

Dave, your script doesn't return anything, so mode will always be None... ;)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jul 16, 2020 8:27 am
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Convert script to Set Time Date and Season to Python

You're right Jay - I was just trying to replicate the OP's error (which I couldn't). This returns from the method.

Code: Select all
# Set thermostats to Heat if dining room temperature is less than 60 else set them to Cool
 
def finish_up():
    current_temp = 60
    indigo.server.log(str(current_temp))   
    if current_temp < 60:
        mode = "Heat"
    else:
        mode = "Cool"
    return mode
mode = finish_up()
indigo.server.log(mode)
indigo.server.log("The function has completed its job")
#

Code: Select all
   Script                          60
   Script                          Cool
   Script                          The function has completed its job

EDIT: Fixes typo.
Last edited by DaveL17 on Thu Jul 16, 2020 9:08 am, edited 1 time in total.

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

[My Plugins] - [My Forums]

Posted on
Thu Jul 16, 2020 8:45 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Convert script to Set Time Date and Season to Python

You sure you don't want:
Code: Select all
# Set thermostats to Heat if dining room temperature is less than 60 else set them to Cool
 
def finish_up():
    current_temp = 60
    indigo.server.log(str(current_temp))   
    if current_temp < 60:
        mode = "Heat"
    else:
        mode = "Cool"
    return mode

mode = finish_up()
indigo.server.log(mode)
indigo.server.log("The function has completed its job")
#

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

Posted on
Thu Jul 16, 2020 9:08 am
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Convert script to Set Time Date and Season to Python

Yep, Joe's fixes my edit snafu. My post should be corrected now, too.

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

[My Plugins] - [My Forums]

Posted on
Thu Jul 16, 2020 2:16 pm
wideglidejrp offline
User avatar
Posts: 555
Joined: Jan 15, 2012
Location: Danbury, CT

Re: Convert script to Set Time Date and Season to Python

Thanks for that. With your changes, the script runs without error, but it does not print the mode. I made a few changes as shown here and it runs fine with no errors. My question, why did it work? The variables mode and current_temp are inside the function. I thought they had to be either global or returned in order to use them outside of the function. What am I missing?

Code: Select all
# Set thermostats to Heat if dining room temperature is less than 60 else set them to Cool
 
def finish_up():
    current_temp = 50
    if current_temp < 60:
        mode = "Heat"
    else:
        mode = "Cool"
finish_up()
indigo.server.log(str(current_temp)) 
indigo.server.log(mode)
indigo.server.log("The function has completed its job")
#

John R Patrick
Author of
Home Attitude

Posted on
Thu Jul 16, 2020 3:00 pm
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Convert script to Set Time Date and Season to Python

If you're not using the last version that Joe posted, you're not getting everything as it should be.

Typically, variables defined in a method are exclusive to that method, so you need to "return" everything you need.

[*] as I understand Python--that is to say, incompletely--it is possible to create global variables within a method, but I think convention would say it's discouraged.

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