Erinnere mich das vor kurzem jemand das Programm angesprochen hat. Habe jetzt so weit optimiert
das man auf dem HC3 eine vernünftige Ausgabe bekommt. Ist noch nicht perfekt aber man kann es nutzen.
Und die Mail ist natürlich für die Katz solange Fibaro seine Fehler nicht behebt.
--[[
-- HC3
--]]
--
-- Determine and calculate the power consumption in a given period of time.
-- File name : Energyreport.lua
-- Version : 0.1.1-de HC3
-- Created (c) by: jwi, pblacky
-- Release date : 21 Feb 2021
--
-- If you use manuel mode, save your IDs before updating !
--
local version = "0.1.1-de"
local manually = false
local pkwh = 30.50 -- price per kilowat hour in cent, rappen, pence
local currSymbol= "€" -- €, US$, CHF, £
local userIDs = {2,166} -- id email Users
local checktime = "22:15" -- Daily trigger time
local maildays = {1,7,15,20} --Achtung, das ist der Tag des Monats, [1-31]
local excludeDev= {71,242,157,248} -- this devices are ignored
local subject = "Fibaro-Info: Verbrauchsstatistik"
local dtformat = '%d.%m.%Y - %H:%M' --Datumsformat
local devcount = 0 -- Device counter
local devpmail = 25 -- Number of devices per email
local colhtml = "skyblue" --yellowgreen"
local debugout = true -- false|true
local debughtml = true -- false|true
local defDays = 30 -- if no accordancde in "tabmd" this value is taken
--local ndays
if manually then
-- Manuelle Eingabe der Geräte-IDs zur Auswertung.
pwrIDs = {213,224,256,264,285,305,322,339,383,391}
else
-- Automatische Ermittlung der Stromverbraucher.
pwrIDs = fibaro.getDevicesID({interfaces = {"power"}, visible = true, enabled = true})
end
local function DofM() -- Ermitteln des Tages im Monat
mday = tonumber(os.date('%d'))
local tabmd = { [1] = "30", [7] = "7", [14] ="14", [15] = "15", [18] ="100" } -- [1 - 31]
local days = (tabmd[mday])
if days == nil then
days = defDays
end
return tonumber(days)
end
--local Debug = function ( color, message )
-- fibaro.debug(string.format('<%s style="font-family:Courier new;font-size: 12px;color:%s;">%s', "span", color, message, "span"))
--end
local function sendMail(mailNr,lastmail)
if (userIDs[1] ~= nil) then
local subject= subject .. '-Nr: ' .. mailNr
if mailNr > 0 and lastmail == nil then
mstat = mstat .. 'Weitere Geräte in nächster Mail '
end
for m=1, #userIDs do
if userIDs[m] ~= nil then
fibaro.alert("email",{userIDs[m]},mstat)
--fibaro.call(userIDs[m], "sendEmail", subject, mstat)
print('Mail gesendet an User-Id: ' .. userIDs[m])
end
end
end
end
local function contains(maildays, val)
for i=1,#maildays do
if maildays[i] == val then
return true
end
end
return false
end
local function formatString(name, room)
local slength = string.len(name) + string.len(room)
local dlength = 35 - slength
local dots = string.rep ("-", dlength)
return dots
end
local function calcperiod(ndays)
sek = ndays * 86400
hc3info= api.get("/settings/info/")["hcName"]
status = '<font size="2"><font color="skyblue">'..'Energieverbrauchs Report für die/den letzten ' .. ndays .. ' Tag(e), für '..hc3info..'!'
mstat = status
htmlStat = "<pre><h1>"..status.."</h1></pre>"
StartEnergiemessung = (os.time()-sek)
EndEnergiemessung = os.time()
end
local function energieverbrauch(DeviceID)
local jsonListe = api.get("/energy/"..StartEnergiemessung.."/"..EndEnergiemessung.."/compare/devices/power/"..DeviceID)
return (jsonListe[1].kWh)
end
local function excluded(id)
for i=1,#excludeDev do
if excludeDev[i] == id then
return true
end
end
return false
end
--
local function pwr(IDs)
print('<font color="lightblue">'..status)
print('<font color="lightblue">'.. "Erstellt am "..os.date(dtformat))
local kilowatts = 0
local mailNr = 0
local sum = 0
for i=1,#IDs do
local DeviceID = IDs[i]
if not excluded(DeviceID) then
--local linkID = createlink(DeviceID )
local DeviceName = fibaro.getName(IDs[i])
local DeviceRoom = fibaro.getRoomNameByDeviceID(IDs[i])
local total = string.format("%.2f",energieverbrauch(DeviceID))
local costs = string.format("%.2f", total * (pkwh / 100))
local dots = formatString(DeviceName, DeviceRoom)
devcount = string.format('%03d',devcount + 1)
kilowatts = kilowatts + total
sum = sum + total * (pkwh / 100)
local moutput = devcount.. ') '.. DeviceID ..' ' ..DeviceRoom.." - "..DeviceName .. dots .. total .." kWh = "..currSymbol..' '.. costs
local output = devcount.. ') ' ..DeviceRoom.." - "..DeviceName .. dots .. total .." kWh = "..currSymbol..' '.. costs
local htmlout = "<pre><h4>".. output .. "</h4></pre>"
if i==1 then
status = '\n'..status .. '\n'..output ..'\n'
mstat= '\n'..mstat .. '\n'..moutput ..'\n'
htmlStat = htmlStat .. htmlout
else
status = status .. output ..'\n'
mstat = mstat .. moutput ..'\n'
htmlStat = htmlStat .. htmlout
end
--print(output)
if debugout then print('<font color="orange">'.. output) end
if (i % devpmail == 0) then
if contains(maildays, mday) then
mailNr = mailNr+1
sendMail(mailNr)
mstat = ' '
fibaro.sleep(500)
end
end
end --if
end -- for
local sum = string.format("%.2f", sum)
local verbrauch= '<font color="skyblue">'.."Verbrauch total----------------: ".. kilowatts .. ' kWh'..'</br>'
local summe='<font color="skyblue">'.. "Summe gesamt-------------------: ".. sum .. ' '..currSymbol
--local summe = 'Summe gesamt-------------------: '.. sum .. ' '..currSymbol
status = status .. verbrauch ..'\n'.. summe
--status = status .. "\n" .. "Erstellt am "..os.date(dtformat)
mstat = mstat .. verbrauch ..'</br>\n'
mstat = mstat .. summe
mstat = mstat .. "\n" .. "Erstellt am "..os.date(dtformat)
htmlStat = htmlStat .. '<font color="skyblue"><font size="2">'..verbrauch.." ".. summe .. "</font>"
if debugout then
print('<font color="orange">'.."Verbrauch total-------------: ".. kilowatts .. ' kWh')
print('<font color="orange">'.."Summe gesamt----------------: ".. sum .. ' '..currSymbol)
end
if debughtml then print('<fontcol="skyblue">'.. htmlStat) end
if contains(maildays, mday) then
mailNr = mailNr + 1
sendMail(mailNr, true)
end
end
days = DofM()
calcperiod(days)
function main()
local currentDate = os.date("*t");
local currenthour = string.format("%02d", currentDate.hour) .. ":" .. string.format("%02d", currentDate.min)
local startSource = fibaro.getSourceTrigger();
if currenthour == checktime then
days = DofM()
calcperiod(days)
pwr(pwrIDs)
end
fibaro.setTimeout(60*1000,main)
end
if (sourceTrigger["type"] == "autostart") then
main()
else
local currentDate = os.date("*t");
days =DofM()
pwr(pwrIDs)
end