require ('xap') require('xap.bsc') require("string") local io = require("io") local http = require("socket.http") local ltn12 = require("ltn12") module(...,package.seeall) info={ version="1.0", description="PVoutput poster" } CurrentTemp = 0 Logtime = os.time() TempCumulativeKwH = 0 CumulativeKwH = 0 CurrentKwH = 0 watts = 0 elapsed = 0 ConvFactor = 0.95 Api = "2c8c----cut-----ee538540" -- put your PVoutput api key here Feed = "29XX" -- put your PVoutput feed ID here Postinterval = 5 -- The number of minutes between posts to PVoutput url='://pvoutput.org/service/r2/addstatus.jsp?key=' Power = 1 function init() local f = xap.Filter() f:add("xap-header", "source", "dbzoo.livebox.CurrentCost:ch.1") f:add("xap-header","class","xAPBSC.event") f:callback(powerlog) local f = xap.Filter() f:add("xap-header", "source", "dbzoo.livebox.CurrentCost:temp") f:add("xap-header","class","xAPBSC.event") f:callback(GetTemp) end function post(self) local ckwh = math.floor(CumulativeKwH *1000 ) if CumulativeKwH ~= 0 then if tonumber(Power) ~= 0 then b, c, h = http.request("http" ..url ..Api .. "&sid="..Feed .. "&d=" ..os.date("%Y%m%d") .. "&t=" ..os.date("%H:%M") .. "&v1=" ..ckwh.. "&v2=" ..Power .."&v5=" ..CurrentTemp) end end end function powersave(self) local file = io.open("/etc/plugboard/CumulativeKwH","r") CumulativeKwH = file:read("*all") file:close() CumulativeKwH = tonumber(CumulativeKwH) + TempCumulativeKwH TempCumulativeKwH = 0 local cmd = string.format("echo '%s'>/etc/plugboard/CumulativeKwH", CumulativeKwH) os.execute(cmd) self:reset() post() end function GetTemp(frame) CurrentTemp = frame:getValue("input.state","text") end function powerlog(frame) local CurrentPeriod = os.difftime(os.time(), Logtime) Logtime = os.time() local CurrentKwH = Power * CurrentPeriod / 3600000 TempCumulativeKwH = TempCumulativeKwH + CurrentKwH Power = frame:getValue("input.state","text") if tonumber(Power) < 45 then Power = 0 -- Ignore values of less than 45 elseif tonumber(Power) < 300 then Power = tonumber(Power) * 0.5 -- At very low outputs the CC tends to significantly over read else Power = tonumber(Power) * ConvFactor -- The conversion factor can be adjusted to make the readings more accurate end end function filereset(self) local Logtime = os.date("%H") if tonumber(Logtime) > 22 then local cmd = string.format("echo 0 >/etc/plugboard/CumulativeKwH") os.execute(cmd) end self:reset() end xap.Timer(powersave, Postinterval*60):start() xap.Timer(filereset, 1800):start() -- reset the cumulative file after 11 pm. Check the time about every half hour