Need help converting data-logging AppleScript

Posted on
Thu Jun 25, 2020 8:16 pm
gmusser offline
Posts: 290
Joined: Feb 12, 2005
Location: New Jersey

Need help converting data-logging AppleScript

I've been dreading the script conversion process, but guess I can't put it off any longer! My most important script logs temperatures to an external file for analysis, and I don't see anything in the tutorial that pertains to this, so I'd be grateful for any help in Pythonizing or otherwise performing this task.

Code: Select all
tell application "IndigoServer"
try

-- set up log file name
set fileName to "TempData.txt"
set filePath to path to documents folder as string

-- set filePath to filePath & "House:"
set filePath to filePath & fileName

-- open log file
set fileRef to open for access filePath with write permission

-- write the time to the log file
write ((month of (current date) as integer) as string) & "/" to fileRef starting at eof
write ((day of (current date) as integer) as string) & "/" to fileRef starting at eof
write ((year of (current date) as integer) as string) & tab to fileRef starting at eof
write ((time of (current date)) / 86400 as string) & tab to fileRef starting at eof

--write the boiler status
if value of variable "boiler_laston_time" as integer is greater than 0 then
write "on" to fileRef starting at eof
else
write "off" to fileRef starting at eof
end if

-- include outdoors temperature
set temp to value of variable "Temperature_Front_Porch" as real
write tab & (temp as string) to fileRef starting at eof

-- include basement temperature
set temp to value of variable "Temperature_Basement" as real
write tab & (temp as string) to fileRef starting at eof

-- include attic temperature
set temp to value of variable "Temperature_Attic" as real
write tab & (temp as string) to fileRef starting at eof

-- include dining-room temperature
set temp to value of variable "Temperature_Dining" as real
write tab & (temp as string) to fileRef starting at eof

-- include dining-room temperature change
set degPerHour to value of variable "Temperature_Dining_rate" as real
write tab & (degPerHour as string) to fileRef starting at eof

-- include rear-bedroom temperature
set temp to value of variable "Temperature_Ellies" as real
write tab & (temp as string) to fileRef starting at eof

-- include rear-bedroom temperature change
set degPerHour to value of variable "Temperature_Ellies_rate" as real
write tab & (degPerHour as string) to fileRef starting at eof

-- Finish logging and close the file
write return to fileRef starting at eof
on error err
log "temperature monitor error: " & err & return
end try
close access fileRef
end tell

 

Posted on
Thu Jun 25, 2020 8:57 pm
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Need help converting data-logging AppleScript

Happy to help convert this to a Python script. What would make that easier is if you can also show what the output looks like that you want as a result (the data structure you want written to the file itself).

EDIT: I think this is pretty close (but obviously untested):
Code: Select all
import os

data_path = os.path.expanduser('~') + "/Documents/House/TempData.txt"

try:

    with open(data_path, 'a+') as outfile:
        now_time = indigo.server.getTime()
        outfile.write("{0}".format(now_time))
   
        # write the boiler status
        if indigo.variables['boiler_laston_time'].getValue(int) > 0:
            outfile.write("on")
        else:
            outfile.write("off")

        # include outdoors temperature
        temp = indigo.variables['Temperature_Front_Porch'].getValue(float)
        outfile.write("\t{0}".format(temp))
       
        # include basement temperature
        temp = indigo.variables['Temperature_Basement'].getValue(float)
        outfile.write("\t{0}".format(temp))

        # include attic temperature
        temp = indigo.variables['Temperature_Attic'].getValue(float)
        outfile.write("\t{0}".format(temp))

        # include dining-room temperature
        temp = indigo.variables['Temperature_Dining'].getValue(float)
        outfile.write("\t{0}".format(temp))

        # include dining-room temperature change
        temp = indigo.variables['Temperature_Dining_rate'].getValue(float)
        outfile.write("\t{0}".format(temp))

        # include rear-bedroom temperature
        temp = indigo.variables['Temperature_Ellies'].getValue(float)
        outfile.write("\t{0}".format(temp))

        # include rear-bedroom temperature change
        temp = indigo.variables['Temperature_Ellies_rate'].getValue(float)
        outfile.write("\t{0}".format(temp))

except Exception as err:
    indigo.server.log("temperature monitor error: {0}".format(err))
Last edited by DaveL17 on Fri Jun 26, 2020 4:38 am, edited 3 times in total.

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

[My Plugins] - [My Forums]

Posted on
Fri Jun 26, 2020 4:11 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Need help converting data-logging AppleScript

Dave's code looks close, but this line:
Code: Select all
    with open(data_path, 'w') as outfile:

Needs to be:
Code: Select all
    with open(data_path, 'a+') as outfile:

Since you want to append to the file, not start over.

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

Posted on
Fri Jun 26, 2020 4:29 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Need help converting data-logging AppleScript

Thanks Joe -- force of habit.

I also just spotted a typo which I have corrected as well.

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

[My Plugins] - [My Forums]

Posted on
Fri Jun 26, 2020 10:36 am
gmusser offline
Posts: 290
Joined: Feb 12, 2005
Location: New Jersey

Re: Need help converting data-logging AppleScript

Thanks! That works… almost. How do I write a carriage return to the file, and how do I format the date as "mm/dd/yyyy" and time as a decimal value between 0 and 1 (where 0 is 0h and 1 is 24h)?

Posted on
Fri Jun 26, 2020 10:47 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Need help converting data-logging AppleScript

gmusser wrote:
Thanks! That works… almost. How do I write a carriage return to the file, and how do I format the date as "mm/dd/yyyy" and time as a decimal value between 0 and 1 (where 0 is 0h and 1 is 24h)?


Carriage return is "\r" in the format strings, where needed. Like "\t" is a tab.

Here's a good summary of how to do time strings: https://www.programiz.com/python-progra ... e/strftime

If you actually want a decimal value for the time of day, you'll probably need to do the math yourself. Depending on the accuracy you need, it's probably going to be something like:

Code: Select all
decimal = (hour * 3600.0 + minute * 60.0 + seconds) / (3600.0*24.0)

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

Posted on
Fri Jun 26, 2020 7:46 pm
gmusser offline
Posts: 290
Joined: Feb 12, 2005
Location: New Jersey

Re: Need help converting data-logging AppleScript

Following the date-formatting directions on that page, I tried the following:
Code: Select all
now_time = indigo.server.getTime()
date_time = datetime.fromtimestamp(now_time)
outfile.write("{0}".date_time.strftime("%m/%d/%Y"))

But I get an error saying that a float is required.

Posted on
Fri Jun 26, 2020 8:31 pm
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Need help converting data-logging AppleScript

You don't need to convert the time object that Indigo gives you since it's already in the right form. Using Joe's example, is this what you're looking for?

Code: Select all
now = indigo.server.getTime()

decimal = (now.hour * 3600.0 + now.minute * 60.0 + now.second) / (3600.0*24.0)

outfile.write(u"{0:%m/%d/%Y} {1}".format(now, decimal))

>>>  06/26/2020 0.894108796296
Last edited by DaveL17 on Sat Jun 27, 2020 5:08 am, edited 2 times in total.

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

[My Plugins] - [My Forums]

Posted on
Fri Jun 26, 2020 9:00 pm
gmusser offline
Posts: 290
Joined: Feb 12, 2005
Location: New Jersey

Re: Need help converting data-logging AppleScript

Nice, thanks! A couple of typos aside, that works!

Posted on
Sat Jun 27, 2020 5:08 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Need help converting data-logging AppleScript

Cheers. I think I've corrected the typos in my post. Glad to help.

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