ThingSpeak

Yes I made the change of the key !!!

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?

[quote=“rhanson, post:22, topic:174415”]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?[/quote]

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.

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. :slight_smile:

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

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:

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:

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.

[ol][li]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.[/li]
[li]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.[/li][/ol]

Happy to hear any suggestions for further refinements, and please chime in if there are any problems with this implementation. Thanks.
Dave

So why not cut out the "extra" step?

This is what I call premature optimization … almost as bad as what it sounds like :slight_smile:
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.

Yikes! I’m so embarrassed. :slight_smile:

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. :slight_smile: 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

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.

[url=http://www.myzwave.net/index.php/my-sensor-data/]http://www.myzwave.net/index.php/my-sensor-data/[/url]

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”)

Hi,
I think need to put code on “startup lua” but i didn’t find it on the web…

Thanks for your help.
Ch.

Hi,
me again… ;D
i didn’t find the code to put in ‘Edit Startup Lua" on my vera… :’(
Someone help me ?
Thanks
Ch.

up up !!

Please, any help ?
Merci…

Here…

I had Thingspeak working a while back but its not working, here is my code

Any ideas how we can get ThingSpeak working again so it updates my channel? I handcraft the URL and put it in my browser and it updates the values…But won’t work in a scene in Vera anymore???

[code]-- Weather Plugin and temps

local 1_TEMP = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 565)
local 2_TEMP = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 674)
local 3_TEMP = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 620)
local 4_TEMP = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 513)
local 5_TEMP = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 81)
local 6_TEMP = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 668)
local 7_TEMP = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 445)
local 8_TEMP = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 282)

–local WIND = luup.variable_get(“urn:upnp-micasaverde-com:serviceId:Weather1”, “WindSpeed”, 84)
– Send data to channel 1
local http = require(“socket.http”)
http.TIMEOUT = 30
result, status = http.request(“http://api.thingspeak.com/update?key=xxxxxxxxxxxxxxx&field1=“..1_TEMP..”&field2=“..2_TEMP..”&field3=“..3_TEMP..”&field4=“..4_TEMP..”&field5=“..5_TEMP..”&field6=“..6_TEMP..”&field7=“..7_TEMP..”&field8=“..8_TEMP..””, “run=run”)
[/code]

You can send to thingspeak from altui plugin.

I have been using Thingspeak with AltUI for a while now but the problem is that it reports “zero” values so the Scene route seems to be the better way, for now.
For reference, look here: http://forum.micasaverde.com/index.php/topic,34343.msg259918.html#msg259918

If you get the zero values in the data stream, it will mean that visualizing the data via f.x. the excellent Highstock graphing will result in “broken” lines on the feeds.

I did initially get the reporting to work with the scenes 1 time only, but then I went back and stripped out unnecessary comments and other and the code ran fine after that.

This “scene” method never crossed my mind!!! I’ll give it a shot. The nice thing about using the scene is that theoretically, it could fire whenever a temperature variable changes and write ALL the channel’s data at that time, and I can put in a time trigger to update the chart every so often even if there are no changes with any of the variables.

I got this to work really nicely using this concept. I had to toggle between AltUI and the regular UI5 to get it done (for some reason, AltUI would not accept the LUA code and return "could not edit scene).

I now trigger the scene using variable watches on all of my temperature monitors which cause ALL the Thingspeak variables to be updated. I also added an update every 20 minutes for good measure.

I’m not a LUA coder, so for anyone else who copies/paste the code in posted by “myhomeserver” – you can’t start a variable name with a number.

Is anyone having success using the Scene method in UI7? Here’s my code (with my write API for my channel - originally I had my overall API key):

– Get Temps
local MBR_TEMP = luup.variable_get(“urn:schemas-micasaverde-com:device:TemperatureSensor:1”, “CurrentTemperature”, 121)
local BR1_TEMP = luup.variable_get(“urn:schemas-micasaverde-com:device:TemperatureSensor:1”, “CurrentTemperature”, 116)
local OFC_TEMP = luup.variable_get(“urn:schemas-micasaverde-com:device:TemperatureSensor:1”, “CurrentTemperature”, 106)
local IN_TEMP = luup.variable_get(“urn:schemas-micasaverde-com:device:TemperatureSensor:1”, “CurrentTemperature”, 53)
local OUT_TEMP = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature”, 47)

– Send data to ThingSpeak.com
local http = require(“socket.http”)
result, status = http.request(“http://api.thingspeak.com/update?key=MASKED&field1=“..MBR_TEMP..”&field2=“..BR1_TEMP..”&field3=“..OFC_TEMP..”&field4=“..IN_TEMP..”&field5=“..OUT_TEMP..””, “run=run”)