module (...,package.seeall) require("xap") require("xap.bsc") info = {version="1.0", description="Log CurrentCost power data & xAP send house totals"} load = { } -- array of CurrentCost latest phase 1..3 values temp = 0 -- Currentcost temperature logtime = os.time() -- using this we decide if current second event is already being sent logpath = "/mnt/usb/livebox/currentcost" -- folder on usb stick oktolog = false -- set to true when load and temp values all get initialised -- Augment CurrentCost with a new endpoint, that will some all the phases local cctotal = bsc.Endpoint{source="dbzoo.livebox.CurrentCost:ch.total", direction=bsc.INPUT, type=bsc.STREAM} function init() fp = xap.Filter() fp:add("xap-header", "source", "dbzoo.livebox.CurrentCost:ch.*") fp:callback(saveload) ft = xap.Filter() ft:add("xap-header", "source", "dbzoo.livebox.CurrentCost:temp") -- ft:add("xap-header", "class", "xAPBSC.info") ft:callback(savetemp) xap.Timer(delaylogstart, 2):start() end -- Wait until all values initialized function delaylogstart(self) if table.getn(load) < 3 or temp == 0 then return end oktolog = true self:delete() end function saveload() local source = xap.getValue("xap-header","source") local ch = tonumber(source:sub(-1)) if ch == nil then print('Type mismatch, cc chnum.') return end load[ch] = xap.getValue("input.state","text") -- print(string.format("%s,%s,%s,%s", os.date("%Y-%m-%d %X"), ch, xap.getValue("input.state","text"), temp)) if oktolog and os.time() ~= logtime then -- one log line/sum event for same second logtime = os.time() -- delaying send for 1 second, xap.Timer(logandsend, 1):start() -- other possible events from same second are saved until then end end function savetemp() temp = xap.getValue("input.state","text") end function logandsend(self) local sumload = load[1] + load[2] + load[3] local cmd = string.format("echo %s,%s,%s,%s,%s,%s>>%s/%s", os.date("%Y-%m-%d %X"), sumload, load[1], load[2], load[3], temp, logpath, os.date("%Y%m%d")) os.execute(cmd) cctotal:setText(sumload) -- Hmm missing a method here... I'll fix that in an update. cctotal.displaytext = string.format("%s Watts", sumload) cctotal:sendEvent() self:delete() end