Here is an example script, but it requires an external library which does not ship with OSX (called psycopg2) that will need to be installed in order for this to work. I don't know how to do what you want to do with bone-stock Python, unfortunately. If anyone else knows, please feel free to modify my example.
- Code: Select all
import psycopg2
try:
conn = psycopg2.connect(database="indigo_history", user="postgres", password="", host="127.0.0.1", port="5432")
cursor = conn.cursor()
cursor.execute("SELECT pressure FROM device_history_1899035475 ORDER BY ts DESC")
rows = cursor.fetchall()
except:
indigo.server.log(u"Unable to open database.")
foo = str(rows[0][0])
indigo.variable.updateValue(296635249, foo)
The above code assumes the default settings of the Indigo SQL Logger Plugin and creates a list where 'pressure' is the device state named 'pressure' from device number '1899035475'. The list in this example is called 'rows' and contains all of the values that Indigo has saved to the database for that device state. In addition, the script sorts the data in 'rows' to have the most recent entry in the list appear first. That way we know where the most recent value is located in our code. We then extract the most recent value from the data we've just pulled into the temporary variable named 'foo'. To be safe, we convert it to a string by using the str() method and we pull the first element from our list 'rows' which is located at position [0][0] (in Python the first thing in a list is the zeroth thing.) You don't have to use this step (saving the value to an interim variable), but it's common form to do it that way in Python. We then finally save the value of 'foo' to the Indigo variable '296635249'. Of course, your device number and state as well as variable number will all be different.
There's a lot going on here I realize and unfortunately, this is about as simple as I know how to make it. The code is tested and does work on my Indigo server. Since you are able to pull the value you want, you may only need the last part of the code above (and perhaps modify your 'SELECT' statement to match this example.) Please feel free to ask questions if you have them.
Cheers,
Dave