We have moved at community.getvera.com

Author Topic: ThingSpeak  (Read 29753 times)

Offline spudjr

  • Full Member
  • ***
  • Posts: 133
  • Karma: +2/-0
Re: ThingSpeak
« Reply #15 on: April 12, 2013, 07:35:55 pm »
What is the most reliable way to have the scene run every X minutes to report to ThingSpeak? Timer plugin, schedule ......????
« Last Edit: April 12, 2013, 07:51:08 pm by spudjr »

Offline DaveL17

  • Full Member
  • ***
  • Posts: 186
  • Karma: +0/-2
Re: ThingSpeak
« Reply #16 on: April 12, 2013, 08:49:09 pm »
no success with this line:

result, status = http.request("http://api.thingspeak.com/update?key=RECWNP1QXT8C3CMJ&field1="..USAGE_LEVEL..", "&run=run")

You're getting close.

result, status = http.request("http://api.thingspeak.com/update?key=RECWNP1QXT8C3CMJ&field1="..USAGE_LEVEL.."", "run=run")

Note that there's two double-quotes after the last variable in the URL, and there is no ampersand '&' before 'run'.

Think of it this way....  There are double quotes around each variable name, and there are double quotes around the entire URL.
« Last Edit: April 12, 2013, 08:53:37 pm by DaveL17 »

Offline DaveL17

  • Full Member
  • ***
  • Posts: 186
  • Karma: +0/-2
Re: ThingSpeak
« Reply #17 on: April 12, 2013, 08:50:53 pm »
What is the most reliable way to have the scene run every X minutes to report to ThingSpeak? Timer plugin, schedule ......????

I have found that using a scene is very solid.

Offline Piwtorak

  • Hero Member
  • *****
  • Posts: 988
  • Karma: +4/-0
Re: ThingSpeak
« Reply #18 on: April 15, 2013, 08:00:12 am »
ok Guys !

is working fine !!

the code is:

 local USAGE_LEVEL = luup.variable_get("urn:upnp-org:serviceId:Dimming1", "LoadLevelStatus", 30)

 local http = require("socket.http")
 http.TIMEOUT = 5

result, status = http.request("http://api.thingspeak.com/update?key=RECWNP1QXT8C3CMJ&field1="..USAGE_LEVEL.."", "run=run")


and is registering the usage in percent of one light.

thanks for help !!!



Vera3 (1), Airport Extreme (2), Apple TV (3), Sqblaster (1), GE Switch (3), GE Dimmer (1), Leviton VRCSZ2 (2), GE 45601 (1), Intermatic HA03 (2), GE Zwave Outlet (1), Remote Control Curtain (1) and growing.

Offline DaveL17

  • Full Member
  • ***
  • Posts: 186
  • Karma: +0/-2
Re: ThingSpeak
« Reply #19 on: April 15, 2013, 08:04:58 am »
ok Guys !

is working fine !!

[snip]

thanks for help !!!

Great news. Congratulations!  Don't forget to change your API key. Your existing one is now in the open.

Offline Piwtorak

  • Hero Member
  • *****
  • Posts: 988
  • Karma: +4/-0
Re: ThingSpeak
« Reply #20 on: April 15, 2013, 03:33:06 pm »
Yes I made the change of the key !!!

Vera3 (1), Airport Extreme (2), Apple TV (3), Sqblaster (1), GE Switch (3), GE Dimmer (1), Leviton VRCSZ2 (2), GE 45601 (1), Intermatic HA03 (2), GE Zwave Outlet (1), Remote Control Curtain (1) and growing.

Offline rhanson

  • Sr. Newbie
  • *
  • Posts: 22
  • Karma: +0/-0
Re: ThingSpeak
« Reply #21 on: April 15, 2013, 08:38:08 pm »
Wouldn't this run more efficiently as a plug-in? Same lua code, just buried in the xml instead of relying on the scene.

Maybe it's bad memories from having only 5 hDCs back in the day, but it seems like creating new http sockets every 15 minutes would create problems?

Or are my fears misguided?

Offline DaveL17

  • Full Member
  • ***
  • Posts: 186
  • Karma: +0/-2
Re: ThingSpeak
« Reply #22 on: April 15, 2013, 10:33:47 pm »
Wouldn't this run more efficiently as a plug-in? Same lua code, just buried in the xml instead of relying on the scene.

Maybe it's bad memories from having only 5 hDCs back in the day, but it seems like creating new http sockets every 15 minutes would create problems?

Or are my fears misguided?

It's a great question, and I have no idea.  It could make for a really great plugin--but I understand that there's lots of system overhead with plugins.  That said, I'd be interested in hearing the opinions of those with more knowledge than I (which isn't asking for much, trust me.)

Speaking as one with barely anything running on a Vera2, and hardly any free resources, system resources are at a premium for me. (I'm sure that it's unrelated to this code as I've been using it for weeks with no problems whatsoever. I recently had a plugin installation go south and I haven't be able to recover from that.)

I'd be all for whatever approach is the least taxing overall.

Offline rhanson

  • Sr. Newbie
  • *
  • Posts: 22
  • Karma: +0/-0
Re: ThingSpeak
« Reply #23 on: April 16, 2013, 01:14:24 am »
Hey, if it works, let's not mess with it! If Vera's not rebooting all day long, then this approach will be just fine. :-)

Offline DaveL17

  • Full Member
  • ***
  • Posts: 186
  • Karma: +0/-2
Re: ThingSpeak
« Reply #24 on: April 16, 2013, 07:51:57 am »
Hey, if it works, let's not mess with it! If Vera's not rebooting all day long, then this approach will be just fine. :-)

I'd still be interested in hearing from folks with knowledge of such things.  I would think (perhaps wrongly) that this would be no more taxing than loading a web page--my set up is only sending two POSTs every 15 minutes which I expect is less data than loading this thread.

But if this method is leaving junk behind, then maybe a plugin is the better option. Of course, there's no reason why we can't have both available and let folks choose their poison. Perhaps this will be my first plugin...

Speaking of that, should this code block have a "return true" or "end" to finish it off?

Dave

Offline DaveL17

  • Full Member
  • ***
  • Posts: 186
  • Karma: +0/-2
ThingSpeak
« Reply #25 on: April 24, 2013, 10:05:42 pm »
Still thinking about ways to improve the Luup code for this method.  I got to thinking that converting the device states into variables was at best unnecessary and at worst inefficient (see post #4 above).  So why not cut out the "extra" step?

This construction puts the Luup variable directly into the URL:

Code: [Select]
local http = require("socket.http")
result, status = http.request("http://api.thingspeak.com/update?key=API_KEY
&field1="..luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", 51).."
&field2="..luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", 41).."
&field3="..luup.variable_get("urn:micasaverde-com:serviceId:HumiditySensor1", "CurrentLevel", 44).."
&field4="..luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1", "WindSpeed", 40).."
&field5="..luup.variable_get("urn:upnp-org:serviceId:TemperatureSetpoint1_Heat", "CurrentSetpoint", 51).."
&field6="..luup.variable_get("urn:upnp-org:serviceId:TemperatureSetpoint1_Cool", "CurrentSetpoint", 51).."", "run=run")

local http = require("socket.http")
result, status = http.request("http://api.thingspeak.com/update?key=API_KEY
&field1="..luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", 37).."
&field2="..luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", 55).."
&field3="..luup.variable_get("urn:upnp-org:serviceId:TemperatureSetpoint1_Heat", "CurrentSetpoint", 55).."
&field4="..luup.variable_get("urn:upnp-org:serviceId:TemperatureSetpoint1_Cool", "CurrentSetpoint", 55).."
&field5="..luup.variable_get("urn:micasaverde-com:serviceId:HumiditySensor1", "CurrentLevel", 56).."
&field6="..luup.variable_get("urn:micasaverde-com:serviceId:LightSensor1", "CurrentLevel", 60).."", "run=run")

Note that Vera (and probably Lua, I'm guessing) doesn't like line breaks in the URL.  The code above is line-broken just to make it easy to recognize the construction. 

The actual construction would look like this:

Code: [Select]
local http = require("socket.http")
result, status = http.request("http://api.thingspeak.com/update?key=API_KEY&field1="..luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", 51).."&field2="..luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", 41).."&field3="..luup.variable_get("urn:micasaverde-com:serviceId:HumiditySensor1", "CurrentLevel", 44).."&field4="..luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1", "WindSpeed", 40).."&field5="..luup.variable_get("urn:upnp-org:serviceId:TemperatureSetpoint1_Heat", "CurrentSetpoint", 51).."&field6="..luup.variable_get("urn:upnp-org:serviceId:TemperatureSetpoint1_Cool", "CurrentSetpoint", 51).."", "run=run")

local http = require("socket.http")
result, status = http.request("http://api.thingspeak.com/update?key=API_KEY&field1="..luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", 37).."&field2="..luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", 55).."&field3="..luup.variable_get("urn:upnp-org:serviceId:TemperatureSetpoint1_Heat", "CurrentSetpoint", 55).."&field4="..luup.variable_get("urn:upnp-org:serviceId:TemperatureSetpoint1_Cool", "CurrentSetpoint", 55).."&field5="..luup.variable_get("urn:micasaverde-com:serviceId:HumiditySensor1", "CurrentLevel", 56).."&field6="..luup.variable_get("urn:micasaverde-com:serviceId:LightSensor1", "CurrentLevel", 60).."", "run=run")

A couple of notes here.
  • The reason that each URL is limited to six field values is that this is the limit of each Thingspeak channel.  You must start a second channel to include the next six variables.  To track twenty five variables, you'd need (surprise) five channels.
  • Earlier versions of the code block included a 'TIMEOUT' value.  This *may* have been causing occasional problems with other http I/O operations that my Vera was running (like the Nest plugin needs to make).  I'm not positive about this, but the LuaSocket reference states "TIMEOUT: sets the timeout for all I/O operations" and the code seems to run fine without the TIMEOUT value. I'm not sure if "all" means local I/O or global.

Happy to hear any suggestions for further refinements, and please chime in if there are any problems with this implementation.  Thanks.
Dave
« Last Edit: April 25, 2013, 07:43:45 am by DaveL17 »

Offline RichardTSchaefer

  • Community Beta
  • Master Member
  • ******
  • Posts: 10091
  • Karma: +764/-143
Re: ThingSpeak
« Reply #26 on: April 24, 2013, 11:01:37 pm »
Quote
So why not cut out the "extra" step?

This is what I call premature optimization ... almost as bad as what it sounds like :)
Your Post #4 is readable ... That means a lot for maintainability.

Your Post #25 is hard to read and maintain.
This code is not executed in a loop and even if it did the amount of CPU cycles in this code vs a single call to http.request is insignificant. Similarly the amount of memory saved is just a handful of local variable objects holding a reference to a string ... maybe a couple hundred bytes ...

If you really wanted to save memory you would get rid of all of the .. operators and replace them with string.format (formatstring, ???)
Every time you call .. You create another String with a few more characters added to the previous one. 

i.e.   result = "1" .. "2" .. "3" .. "4"
Would create 2 intermediate strings that are used only for an instance and thrown away. Only the final concatenation is useful.
But even here I would not worry about it ... LUA has reference counting variables and cleans up nicely.


Offline DaveL17

  • Full Member
  • ***
  • Posts: 186
  • Karma: +0/-2
Re: ThingSpeak
« Reply #27 on: April 25, 2013, 07:39:46 am »

This is what I call premature optimization ... almost as bad as what it sounds like :)

Yikes!  I'm so embarrassed. :)

So if I understand, the only down side of this revised code is lack of readability. Personally, that's something I can live with because I don't see it changing much. But I totally see your point about recommending it to others.

Part of my motivation is that I'm on Vera2, and every free byte counts. :)  I look at it this way: I've reduced the size of the code by 50% as well...

Thanks Richard, I really appreciate the advice. I'm still crawling.

Dave

Offline myhomeserver

  • Hero Member
  • *****
  • Posts: 874
  • Karma: +3/-5
  • http://www.MyZwave.net
Re: ThingSpeak
« Reply #28 on: January 18, 2015, 01:29:18 pm »
Sorry to revive such an old thread but this has helped me gather my data and store it, something that the local datamine plungin wasn't doing well, my USB would somehow become dismounted and caused me to bail on it.  I found this thread looking for an external method to store and chart data, so thanks again

I added some of my collection data on my Z-Wave site for others to check out how its done.  I may add a step by step tutorial with screenshots in the future if people want to see something like that.

http://www.myzwave.net/index.php/my-sensor-data/
MyZWave.net - See Our Z-Wave product Reviews
(formerly MyHomeServer)

Offline cderibas

  • Newbie
  • *
  • Posts: 4
  • Karma: +0/-0
Re: ThingSpeak
« Reply #29 on: September 01, 2015, 10:48:23 am »
Hello everybody,
i tried to setup my VeraLite with this but nothing received on my ThingSpeak Graphics, can you help me ?

local TExterieur = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 61)
local TSalon = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 106)
local TCuisine = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 83)

local http = require("socket.http")
http.TIMEOUT = 5
result, status = http.request("https://api.thingspeak.com/update?key=MYAPIKEY&field1="..TExterieur.."&field2="..TSalon.."&field3="..TCuisine.."", "run=run")
« Last Edit: September 01, 2015, 01:19:14 pm by cderibas »