Thingspeak Python script Help

Posted on
Fri Oct 04, 2013 4:09 pm
gbiski offline
Posts: 93
Joined: Dec 19, 2012

Thingspeak Python script Help

I have the following python script that sends 2 specific numbers to the thingspeak site.
How can i replace the numbers with 2 variables that i have created in Indigo ?


import httplib, urllib
params = urllib.urlencode({'field1': 24, 'field2': 18,'key':'my thingspeak api key'})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept":
"text/plain"}
conn = httplib.HTTPConnection("api.thingspeak.com:80")
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
print response.status, response.reason

data = response.read()
conn.close()


thanks for the help
George

Posted on
Fri Oct 04, 2013 5:13 pm
jay (support) offline
Site Admin
User avatar
Posts: 18224
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Thingspeak Python script Help

Check the Variable Examples in the scripting tutorial - it shows you how to get/set a variable value among other things.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Dec 14, 2013 9:39 pm
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Thingspeak Python script Help

Here's a copy of the script that I use. There are 10 variables total--two Thingspeak channels with five variables each.

Code: Select all
#!/usr/bin/env python2.5
# -- coding: utf-8 --

import httplib, urllib

foreHigh = indigo.devices[1718857577].states['temperature']
outTemp = indigo.devices[714601138].states['temperature']
outHum = indigo.variables[1798089659].value.rstrip('%')
outWind = indigo.variables[660204217].value.rstrip(' mph')
foreLow = indigo.devices[1212595323].states['temperature']

outLux = indigo.devices[758171076].states['sensorValue']
upTemp = indigo.devices[1002786935].states["temperatureInput1"]
downTemp = indigo.devices[1696201155].states["temperatureInput1"]
atticTemp = indigo.devices[766770682].states["temperature"]
bsmtTemp = indigo.devices[1497124715].states["temperature"]

params1 = urllib.urlencode({'field1': foreHigh, 'field2': outTemp, 'field3': outHum, 'field4': outWind, 'field5': foreLow, 'key':'API_KEY1'}) # Channel 1
params2 = urllib.urlencode({'field1': outLux, 'field2': upTemp, 'field3': downTemp, 'field4': atticTemp, 'field5': bsmtTemp, 'key':'API_KEY2'}) # Channel 2
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}

f = httplib.HTTPConnection("api.thingspeak.com:80")
f.request("POST", "/update", params1, headers)
response = f.getresponse()

f = httplib.HTTPConnection("api.thingspeak.com:80")
f.request("POST", "/update", params2, headers)
response = f.getresponse()

f.close()


Any suggestions for improvements would be most welcome.

Dave

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

[My Plugins] - [My Forums]

Posted on
Mon Dec 16, 2013 1:31 pm
Simon offline
Posts: 32
Joined: Aug 15, 2013
Location: Denmark

Re: Thingspeak Python script Help

Thank you gbiski & DaveL17

Was using Xively for graphing but ThingSpeak is way better.

Just implemented using your script.

Merry Christmas

Posted on
Mon Dec 16, 2013 6:09 pm
artpics offline
Posts: 232
Joined: Feb 24, 2009
Location: Calabasas CA

Re: Thingspeak Python script Help

Whoops done it, great stuff and very easy to implement.

here is my code which runs off my schedule for my house temp.

Code: Select all
#!/usr/bin/env python2.5
# -- coding: utf-8 --

import httplib, urllib
curTemp = indigo.variables[1613193514].value.rstrip('%')

params1 = urllib.urlencode({'field1': curTemp, 'key':'my api key change this'}) # Channel 1
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}

f = httplib.HTTPConnection("api.thingspeak.com:80")
f.request("POST", "/update", params1, headers)
response = f.getresponse()

f.close()

Posted on
Mon Dec 16, 2013 6:57 pm
Simon offline
Posts: 32
Joined: Aug 15, 2013
Location: Denmark

Re: Thingspeak Python script Help

I have a question as well.

I am having problems getting the HeatSetpoint (current set temp in a Danfoss Living Connect) to ThingSpeak

I have tried using the:
SetTemp = indigo.devices[714601138].states['temperature']

and different variations of this with no luck.

Can anyone please help.

Posted on
Mon Dec 16, 2013 8:35 pm
artpics offline
Posts: 232
Joined: Feb 24, 2009
Location: Calabasas CA

Re: Thingspeak Python script Help

Can you try putting the "state" into a variable first. then use this code

SetTemp = indigo.variables[161319***4].value.rstrip('%')

that should work

Posted on
Tue Dec 17, 2013 5:35 am
Simon offline
Posts: 32
Joined: Aug 15, 2013
Location: Denmark

Re: Thingspeak Python script Help

Hey artpics

That is what I ended up doing, but if it was possible it would be much "cleaner" to skip the variable.

Posted on
Tue Dec 17, 2013 10:02 am
artpics offline
Posts: 232
Joined: Feb 24, 2009
Location: Calabasas CA

Re: Thingspeak Python script Help

Simon wrote:
Hey artpics

That is what I ended up doing, but if it was possible it would be much "cleaner" to skip the variable.


Sorry i cannot help but it must be something quite simple.

here is my graph page.

https://www.thingspeak.com/channels/9213#

Posted on
Fri Dec 20, 2013 7:04 am
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Thingspeak Python script Help

Simon wrote:
Thank you gbiski & DaveL17

Was using Xively for graphing but ThingSpeak is way better.

Just implemented using your script.

Merry Christmas

You're most welcome.

Merry Christmas to you!

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

[My Plugins] - [My Forums]

Posted on
Fri Dec 20, 2013 7:09 am
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Thingspeak Python script Help

Simon wrote:
I have a question as well.

I am having problems getting the HeatSetpoint (current set temp in a Danfoss Living Connect) to ThingSpeak

I have tried using the:
SetTemp = indigo.devices[714601138].states['temperature']

and different variations of this with no luck.

Can anyone please help.


Sorry for the slow reply. The state I use for thermostats is not ['temperature'], but rather ['temperatureInput1']. That's for the current temp.

I'm not at my server to look at what I use for set points, but if I remember correctly, set points are not expressed as ['temperature']. Try ['setpointHeat'] instead.

Hopefully that will do the trick.

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 7 guests