adafruit / Adafruit_IO_Python

Adafruit IO Python Client Library
Other
225 stars 99 forks source link

MQTT retain and /get functionality #77

Closed matt-desmarais closed 6 years ago

matt-desmarais commented 6 years ago

I was hoping to use the functionality described here

What I am trying to do is to be able to return all the DATA_FIELDS from the most recent feed entry, I want to use either the created_epoch or created_at field to be able to see if my 2 (soon to be 6) IO clients have stopped uploading, they normally upload every minute.

Example

from Adafruit_IO import Client
aio = Client('USER NAME', 'IO KEY')

feed1 = aio.feeds('freezer5')
feed2 = aio.feeds('freezer6')

# Print out the metadata of last retained entry.
print("Freezer5: "+str(feed1.get()))
print("\nFreezer6: "+str(feed2.get()))

Would output the last retained entry from each feed: Freezer5: Data(created_epoch=1537685617, created_at='2018-09-23T06:53:37Z', updated_at=None, value='5.899999999999999', completed_at=None, feed_id=869316, expiration='2018-10-23T06:53:37Z', position=None, id='0DZGT0SPKF92RADE5FQ20W3GN7', lat=None, lon=None, ele=None) Freezer6: Data(created_epoch=1537685676, created_at='2018-09-23T06:53:36Z', updated_at=None, value='6.350000000000001', completed_at=None, feed_id=869316, expiration='2018-10-23T06:53:36Z', position=None, id='0DZGT1BJ2S2VDS7ZAP5N0SGBPN', lat=None, lon=None, ele=None)

brentru commented 6 years ago

If you want to obtain the last created_epoch or created_at, you can call aio.receive(feed.key), which will output

Data(created_epoch=None, created_at='2018-09-25T16:35:47Z', updated_at='2018-09-25T16:35:47Z', value='2', completed_at=None, feed_id=880875, expiration='1543077347.0', position=None, id='0DZJQXVVZQBDMAV0QTCPJVBR6B', lat=None, lon=None, ele=None)

Then, access the data object properties directly: freezer.created_at '2018-09-25T16:35:47Z' freezer.created_epoch print(freezer.created_epoch) None

matt-desmarais commented 6 years ago

Thank you! I used the code below to test it and got the latest upload time. I appreciate the help!

aio = Client('user name', 'key')

feed1 = aio.receive('freezer5')
feed2 = aio.receive('freezer6')

#returns latest upload time
print("Freezer5: "+str(feed1.created_at))
print("Freezer6: "+str(feed2.created_at))
brentru commented 6 years ago

No problem! Closing...