if graph_duration and not(weather_db == "") then
   pcall(function ()
       local dbdriver = require "luasql.sqlite3"
-      local env = assert(dbdriver.sqlite3())
+      env = assert(dbdriver.sqlite3())
       log_con = assert(env:connect(weather_db))
     end)  
 end
       local sensor = def["sensor"]
       local param  = def["param"]
       local pageid = sensor.."."..param
-      local cur = assert (log_con:execute(string.format("select hour,avg(value) val from (select strftime('%%Y-%%m-%%d %%H',time_stamp)||':00' hour,value from log where sensor='%s' and param='%s') group by hour order by hour desc limit 24",sensor,param)))
-      local row = cur:fetch ({}, "a")
+      local sqlcur = assert (log_con:execute(string.format("select hour,avg(value) val from (select strftime('%%Y-%%m-%%d %%H',time_stamp)||':00' hour,value from log where sensor='%s' and param='%s') group by hour order by hour desc limit 24",sensor,param)))
+      local row = sqlcur:fetch ({}, "a")
       local vals = {}
       local maxval = -99999999
       local minval =  99999999
       for k=width,1,-1 do
         if row then
           val = row["val"] * def["scale"]
-          row = cur:fetch ({}, "a")
+          row = sqlcur:fetch ({}, "a")
         else
           val = nil  
         end  
         if val and (val > maxval) then maxval = val; end
         if val and (val < minval) then minval = val; end
       end          
+      sqlcur:close()
       minval = math.floor(minval)
       maxval = math.ceil(maxval)
       if height>2 then
 
 while true do
 
-  vals = process_file()
-  process_vals(vals)
+--  pcall( function ()
 
-  if log_con then
-    process_graphs()
-  end  
+      vals = process_file()
+      process_vals(vals)
+
+      if weather_db then
+        process_graphs()
+      end  
       
-  os.execute("inotifywait -e MODIFY \""..weather_file.."\"")
-  socket.sleep(3)
+      os.execute("inotifywait -e MODIFY \""..weather_file.."\"")
+  
+--    end)
+
+    socket.sleep(3)
   
 end