(Last Updated: September 3, 2020)

Lua: Report - Last Trip



  • I’m sure there is a better/more polished way to do this, but this remains one of my go to report urls on Vera.

    
    -- http://forum.micasaverde.com/index.php?topic=52025.msg334246#msg334246
    
    local d, n
    local file = io.open("/www/sb.html", "w")
       file:write("<head> <meta http-equiv='refresh' content='30' /> </head>\n")
       file:write("<style>.custom { font-size: 1em; font-family: Gill Sans Extrabold, sans-serif; padding:5px; border-collapse: collapse; border: 1px solid black; }</style><table class=custom><tr class=custom><th class=custom>Security Sensor</th><th class=custom>Last Tripped</th></tr>\n")
    
    local tt = {}
    for n,d in pairs(luup.devices) do
    
        if d.category_num == 4 then
        -- local lasttrip = tonumber(luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1", "LastTrip", n) or 0,10)
        local lasttrip = tonumber(luup.variable_get ("urn:micasaverde-com:serviceId:SecuritySensor1", "LastTrip", n) or os.time())
        local timeString = os.date(" %Y/%m/%d - %X", lasttrip)
        
        if lasttrip > 0 then
            table.insert(tt, { devnum=n, last=lasttrip, ltime=timeString })
        end
      end
    end
    
    table.sort(tt, function(a,b) return a.last > b.last end) -- sort highest to lowest
    
    for n,d in ipairs(tt) do
        
       file:write("<tr class=custom><td class=custom>" .. luup.devices[d.devnum].description .. "</td><td class=custom>".. d.ltime .. "</td></tr>\n")
    
        print (
        d.ltime ..
       ' : ' .. luup.devices[d.devnum].description .. " Sensor was last tripped")
    end
    file:write("</table>\n")
    file:close()
    
    

Log in to reply