capture / save data to a txt file

Posted on
Tue Jan 12, 2021 5:26 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

capture / save data to a txt file

I would like to periodically save a device's data to a file. In particular, I would like to record the temperature measured by a device by running a python script every 15 minutes.
I have one line, but don't know where to go next, - and my one line is probably defective.
I'd be very grateful for a suggestion as to the proper coding.
Thanks,
Bob
Code: Select all
data.rtf = open('mac_HD ▸ Users ▸ myName ▸ OneDrive ▸ Documents', 'w')

Posted on
Tue Jan 12, 2021 5:55 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: capture / save data to a txt file

Code: Select all
dev = indigo.devices[1909647006]
data = dev.states['temperature']

with open('/Users/Dave/Downloads/file.txt', 'a') as outfile:
    output = u"{0}, {1}\n".format(indigo.server.getTime(), data)
    outfile.write(output)

You'll need to change the device ID number, the device state name (maybe), and the path to the file.

EDIT: MAY have to change the state name.

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

[My Plugins] - [My Forums]

Posted on
Tue Jan 12, 2021 6:04 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: capture / save data to a txt file

Thanks.
Curious - what does "u"{0}, {1}\n" do?

Posted on
Tue Jan 12, 2021 6:06 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: capture / save data to a txt file

That converts all the data to a Unicode string. That will (at least theoretically) handle all kinds of fancy characters and codes--like degree symbols and such.

https://pyformat.info

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

[My Plugins] - [My Forums]

Posted on
Tue Jan 12, 2021 6:27 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: capture / save data to a txt file

and {0} & {1} refer to time and data respectively? and "\n" is ?

Posted on
Tue Jan 12, 2021 6:33 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: capture / save data to a txt file

That's right. The '/n' is the new line character. This ensures that the next entry will start on the subsequent line.

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

[My Plugins] - [My Forums]

Posted on
Tue Jan 12, 2021 6:34 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: capture / save data to a txt file

very cool. thanks.

Posted on
Tue Jan 12, 2021 6:43 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: capture / save data to a txt file

No problem whatsoever. This is essentially what the Matplotlib plugin CSV Engine device does.

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

[My Plugins] - [My Forums]

Posted on
Tue Jan 12, 2021 6:49 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: capture / save data to a txt file

When I test the script below, the file date is changing, but there are no data. Does there need to be a save command?
Code: Select all
dev = indigo.devices[883519697]
data = dev.states['state']

with open('/Users/rss/OneDrive/Documents/GBK/G lRm Temp Data.rtf', 'a') as outfile:
    output = u"{0}, {1}\n".format(indigo.server.getTime(), data)
    outfile.write(output)

Posted on
Tue Jan 12, 2021 7:14 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: capture / save data to a txt file

It's likely that the state is not called 'state'. It's likely called 'temperature' or something similar. (That's also why I edited my post to say that you *may* have to change the state name.)

If you can't figure out the right name, we have more tools at our disposal.

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

[My Plugins] - [My Forums]

Posted on
Tue Jan 12, 2021 7:16 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: capture / save data to a txt file

'state' is the name I used to get the temperature with better email. I have forgotten how I came to that, but I suspect you may have helped.

Posted on
Tue Jan 12, 2021 7:23 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: capture / save data to a txt file

Hah! Run this, and let's look at the states.

Code: Select all
dev = indigo.devices[1909647006]
data = dev.states
indigo.server.log(u"{}".format(data))

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

[My Plugins] - [My Forums]

Posted on
Tue Jan 12, 2021 7:33 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: capture / save data to a txt file

lastUpdate : 2021-01-12 14:53:01 (string)
state : 6.2 (real)
temperature : 6.2 (real)


That's curious. Maybe 'real'?
Thinking ahead, instead of '\n', getting a pair of CSV values on one line might be more useful.

Posted on
Tue Jan 12, 2021 7:58 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: capture / save data to a txt file

Real is the type of value that the state contains. What we want is the name of the state. Try this and see what you get:

Code: Select all
dev = indigo.devices[883519697]
data = dev.states['temperature']

with open('/Users/rss/OneDrive/Documents/GBK/G lRm Temp Data.rtf', 'a') as outfile:
    output = u"{0}, {1}\n".format(indigo.server.getTime(), data)
    outfile.write(output)
The output will be a time stamp and the temperature value. If that's not what you want, we can work on the final format once we know it's logging properly.

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

[My Plugins] - [My Forums]

Posted on
Tue Jan 12, 2021 9:09 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: capture / save data to a txt file

the only change I made was substituting "temperature' for 'state'.
No change in result - file date is updated, but actual document is blank.

Who is online

Users browsing this forum: No registered users and 5 guests