Every time I add code, I break my Vera Lite

Hi all

I have been trying the last few days to use the Spark Core https://www.spark.io/ to replace a whole lot of things in my garage. This includes four switches to open garage doors and switch lights on and off. The switches I got going easy enough.

I also want to use the digitalread on the rest of the pins to read the state of the garage doors. The simplest implementation with my limited knowledge was to read the state of the digital pins from two simple Megatronix-Magnetic-Switch-Sensors on the doors and to write it to a variable in the Variable container plugin. While testing I did this using the code below:

local socket = require("socket")
local https = require("ssl.https")
local ltn12 = require("ltn12")

https.TIMEOUT = 5

-- Read the state of Pin A3 from the body by finding then isolating the return value in the body

local body, code, headers, status = https.request("https://api.spark.io/v1/devices/[MY_DEVICE_ID]/analogread", "access_token=[MY_ACCESS_TOKEN]&args=A3")
luup.log("A3 status:" .. status)

local rvlocation = string.find(body, "return_value")
local state = string.sub(body,rvlocation+15,rvlocation+16)

-- Write the value to the variable container device 143

luup.variable_set("urn:upnp-org:serviceId:VContainer1","Variable1",state,143)

It worked well in the Test Luup code section so I made a scene that runs this every minute… On refresh, the dreaded “Luup engine is taking longer to reload” message. So it took me about 4 hours to get back to where I was. The solution by the way was this:

Find the password in a previous backup file, SSH using Putty and run the following commands:

cd /etc/cmh
cp user_data.json.lzo user_data.json.lzo-1
cp user_data.json.lzo.1 user_data.json.lzo-2
cp user_data.json.lzo.1 user_data.json.lzo

See if that fixes it … If not then try:
cp user_data.json.lzo.2 user_data.json.lzo-3
cp user_data.json.lzo.2 user_data.json.lzo

So, back to square one.
Then, since I thought that every minute might be too much I made the scene run every 10 minutes. This worked a while (maybe twice) and then again, “Luup engine is taking longer to reload”…

The second time round the solution above did not work. I basically had to factory reset the device doing the following:

  1. Plug all out of the device
  2. Plug in the power plug
  3. Hold down the reset button for something like 30 seconds.
  4. Wait (5-10 minutes) One by one the Power light then the ZWave light and then the internet light will go on
  5. Then restore the last backup.

Ok, so here is my question: Am I doing something stupid in the code above? Is there a better way to do this? How can I do this without destroying my Vera and subsequently get more sleep?

Any help would be appreciated.
PS. I’m typing this all to try and help the next guy with this problem.

Hi,

What I found is that the LUUP engine can go bananas when the return values are not what you expected and functions fail. So make sure that ‘status == 200’ and ‘body ~= nil’ before you try the string.find
local rvlocation = string.find(body, “return_value”)
next make sure rvlocation is a number before you try a string.sub
local state = string.sub(body,rvlocation+15,rvlocation+16)

On errors lot’s of the return values of the LUA and LUUP functions are of a different type then when they succeed. Bit me several times, and you basically must include code to handle the fail conditions. The wiki helps and the LUA manuals do as well. This list of useful links has proven very useful for me to get started http://forum.micasaverde.com/index.php/topic,17300.0.html

Cheers Rene

Thank you Rene. Very good advice. I shall be spending a lot more time in the Useful links forum.