Energie und Kostenberechnung

Hi pblacky,
ich denke nicht das die 4.180 das Problem ist. Eher habe ich hier einen Bug eingebaut. Werde es mir heute Abend anschauen.

Hatte nur EIN Event in der Variable
local tabmd = { [1] = "30"} – [1 - 31]

Habe jetzt mal die Formel aus dem Original Skript genommen und etwas verkleinert, mal sehen, vielleicht war das die Ursache …

Ich stelle mal ein Update zur Verfügung. Vermute das unter gewissen Umständen die Ausgabe nicht der Tabelle entsprach. @pblacky, nur das wir nicht aneinander vorbei sprechen, der Output im Debug-Fenster soll täglich um “checktime” kommen, aber die Email nur an den Tagen in der “tabmd”. Oder hattest Du was anderes gemeint?

Changelog für Version 0.3.1
– Möglicher Bug beim berechnen der Mailausgabe

--[[
%% autostart
%% properties
%% events
--]]
--
-- Determine and calculate the power consumption in a given period of time.
-- File name	: Energyreport.lua 
-- Version      : 0.3.1
-- Created by   : pblacky, jeep
-- Release date : 14 May 2018
--
-- If you use manuel mode, save your IDs before updating !
--
local sourceTrigger = fibaro:getSourceTrigger(); 
local version   = "0.3.1"
local manually  = false
local cent      = 28.50 -- Strompreis per KWh
local userIDs   = {2}   -- 266 Email Users
local checktime = "21:10" -- Daily trigger time
local maildays  = {1,7,13,14,15,}  --Achtung, das ist der Tag des Monats, [1-31]  
local excludeDev= {71,242,157,248} -- Diese devices werden nicht beruecksichtigt 
local subject   = "Fibaro-Info: Verbrauchsstatistik"
local devcount  = 0  -- Device counter
local devpmail  = 25 -- Number of devices per email
local colhtml   = "skyblue" --yellowgreen" 
local debugout  = false -- false|true
local debughtml = true  -- false|true
local defDays   = 30 -- Wenn keine Übereinstimmung in "tabmd" wird dieser Wert genommen
--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 
  local mday  = tonumber(os.date('%d'))
  local tabmd = { [1] = "30", [7] = "7", [14] ="14", [15] = "365" } -- [1 - 31]
  local days  = (tabmd[mday]) 
  if days == nil then
     days = defDays 
  end
  return 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,endmail)
  if (userIDs[1] ~= nil) then
    local subject= subject .. '-Nr: ' .. mailNr 
     if mailNr > 0 and endmail == nil then
        status = status .. 'Weitere Geräte in nächster Mail '
     end
    for m=1, #userIDs do
      if userIDs[m] ~= nil then
         fibaro:call(userIDs[m], "sendEmail", subject, status)
         Debug('lightgreen', '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
   status = 'Energieverbrauchs Report für die/den letzten ' .. ndays .. ' Tag(e).'
   htmlStat = "<pre><h3>"..status.."</h3></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)
  Debug('lightblue',status)
  Debug('lightblue', "Erstellt am "..os.date('%c')) 
  local kilowatts = 0
  local mailNr = 0
  local sum = 0
  for i=1,#IDs do
    local DeviceID = IDs[i] 
    if not excluded(DeviceID) then
       local DeviceName  = fibaro:getName(IDs[i])
       local DeviceRoom  = fibaro:getRoomNameByDeviceID(IDs[i])
       local total = string.format("%.2f",energieverbrauch(DeviceID))
       local euro  = string.format("%.2f", total * (cent / 100))
       local dots  = formatString(DeviceName, DeviceRoom)
       devcount    = string.format('%03d',devcount + 1)    kilowatts   = kilowatts + total
       sum  = sum + total * (cent / 100) 
       local output  = devcount.. ') '.. DeviceID ..' ' ..DeviceRoom.." - "..DeviceName .. dots .. total .." kWh = € ".. euro      
       local htmlout = "<pre><h4>".. output .. "</h4></pre>"
       if i==1 then
          status = '\n'..status .. '\n'..output ..'\n'
          htmlStat = htmlStat .. htmlout
       else  
          status = status .. output ..'\n'
          htmlStat = htmlStat .. htmlout
       end
       if debugout then Debug('orange', output) end
       if (i % devpmail == 0) then
          if contains(maildays, mday) then
             mailNr = mailNr+1
             sendMail(mailNr) 
             status = ' '
             fibaro:sleep(500)
          end
       end  
	end --if
  end -- for
  local sum = string.format("%.2f", sum)
  local verbrauch= 'Verbrauch total----------------: '.. kilowatts .. ' kWh'
  local summe    = 'Summe gesamt-------------------: '.. sum .. ' €'
 
  status = status .. verbrauch ..'\n'.. summe
  status = status .. "\n" .. "Erstellt am "..os.date('%c')
  htmlStat = htmlStat .. "<pre><h3>".. verbrauch.."</h3></pre>" .."<pre><h3>".. summe .. "</pre>"

  Debug("yellow",'Verbrauch total-------------: '.. kilowatts .. ' kWh')
  Debug("yellow",'Summe gesamt----------------: '.. sum .. ' €')
  if debughtml then Debug(colhtml, 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
   setTimeout(main, 60*1000)
end

if (sourceTrigger["type"] == "autostart") then
    main()
else
    local currentDate = os.date("*t");
    local startSource = fibaro:getSourceTrigger();
    if (startSource["type"] == "other")	then
      days =DofM()
      pwr(pwrIDs)
    end
end

Ich hab das so verstanden:
Wenn ich

local maildays  = {1}
 local tabmd = { [1] = "30"}

eingestellt hab, dann muss ich am 1. des Monats eine Verbauchsliste über die letzten 30 Tage bekommen.
Sonst passiert außer dem täglichen Debug nichts weiter, stimmt das??

Das ist korrekt. :wink:

Ich hab jetzt mal das 0.3.1 er Skript eingebaut und entsprechend angepasst, melde mich falls es nicht klappt :wink:

Ich hatte in der letzten Version tatsächlich einen Bug im Mailversand eingebaut, was dazu führte dass keine Mail versendet wurde.
Hier ist die korrigierte Version die diesen Bug behebt.

Changelog für Version 0.3.3
– Fehler im Mailversand behoben

--[[
%% autostart
%% properties
%% events
--]]
--
-- Determine and calculate the power consumption in a given period of time.
-- File name	: Energyreport.lua 
-- Version      : 0.3.3
-- Created by   : pblacky, jeep
-- Release date : 27 May 2018
--
-- If you use manuel mode, save your IDs before updating !
--
local sourceTrigger = fibaro:getSourceTrigger(); 
local version   = "0.3.3"
local manually  = false
local cent      = 28.50 -- Strompreis per KWh
local userIDs   = {2}   -- 266 Email Users
local checktime = "22:10" -- Daily trigger time
local maildays  = {1,7,15,}  --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 devcount  = 0  -- Device counter
local devpmail  = 25 -- Number of devices per email
local colhtml   = "skyblue" --yellowgreen" 
local debugout  = false -- 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", [31] ="365" } -- [1 - 31]
  local days  = (tabmd[mday]) 
  if days == nil then
     days = defDays 
  end
  return 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,endmail)
  if (userIDs[1] ~= nil) then
    local subject= subject .. '-Nr: ' .. mailNr 
     if mailNr > 0 and endmail == nil then
        status = status .. 'Weitere Geräte in nächster Mail '
     end
    for m=1, #userIDs do
      if userIDs[m] ~= nil then
         fibaro:call(userIDs[m], "sendEmail", subject, status)
         Debug('lightgreen', '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
   status = 'Energieverbrauchs Report für die/den letzten ' .. ndays .. ' Tag(e).'
   htmlStat = "<pre><h3>"..status.."</h3></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)
  Debug('lightblue',status)
  Debug('lightblue', "Erstellt am "..os.date('%c')) 
  local kilowatts = 0
  local mailNr = 0
  local sum = 0
  for i=1,#IDs do
    local DeviceID = IDs[i] 
    if not excluded(DeviceID) then
       local DeviceName  = fibaro:getName(IDs[i])
       local DeviceRoom  = fibaro:getRoomNameByDeviceID(IDs[i])
       local total = string.format("%.2f",energieverbrauch(DeviceID))
       local euro  = string.format("%.2f", total * (cent / 100))
       local dots  = formatString(DeviceName, DeviceRoom)
       devcount    = string.format('%03d',devcount + 1)    kilowatts   = kilowatts + total
       sum  = sum + total * (cent / 100) 
       local output  = devcount.. ') '.. DeviceID ..' ' ..DeviceRoom.." - "..DeviceName .. dots .. total .." kWh = € ".. euro      
       local htmlout = "<pre><h4>".. output .. "</h4></pre>"
       if i==1 then
          status = '\n'..status .. '\n'..output ..'\n'
          htmlStat = htmlStat .. htmlout
       else  
          status = status .. output ..'\n'
          htmlStat = htmlStat .. htmlout
       end
       if debugout then Debug('orange', output) end
       if (i % devpmail == 0) then
          if contains(maildays, mday) then
             mailNr = mailNr+1
             sendMail(mailNr) 
             status = ' '
             fibaro:sleep(500)
          end
       end  
	end --if
  end -- for
  local sum = string.format("%.2f", sum)
  local verbrauch= 'Verbrauch total----------------: '.. kilowatts .. ' kWh'
  local summe    = 'Summe gesamt-------------------: '.. sum .. ' €'
 
  status = status .. verbrauch ..'\n'.. summe
  status = status .. "\n" .. "Erstellt am "..os.date('%c')
  htmlStat = htmlStat .. "<pre><h3>".. verbrauch.."</h3></pre>" .."<pre><h3>".. summe .. "</pre>"

  Debug("yellow",'Verbrauch total-------------: '.. kilowatts .. ' kWh')
  Debug("yellow",'Summe gesamt----------------: '.. sum .. ' €')
  if debughtml then Debug(colhtml, 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
   setTimeout(main, 60*1000)
end

if (sourceTrigger["type"] == "autostart") then
    main()
else
    local currentDate = os.date("*t");
    local startSource = fibaro:getSourceTrigger();
    if (startSource["type"] == "other")	then
      days =DofM()
      pwr(pwrIDs)
    end
end

Hallo Jeep, habe Dein Skript zum Energieverbrauch verwendet - abner ich schaffe es nicht dieses per Email an mich zu versenden. In welcher Zeile muss ich denn di ID meines Iphones einttragen ? Herzlichen Dank !

Hallo @Lst,

die IDs der iPhones kommen natürlich in der Zeile 19 in der Variablen ‘local userIDs = {2, 155,}’ usw.
Die Tage an denen eine Mail verschickt wird, steht hier: ‘local maildays = {1,7,15,}’ --Achtung, das ist der Tag des Monats, [1-31]
Wenn Du heute eine Mail sehen willst, dann musst Du noch die 24 eintragen, also ‘local maildays = {1,7,15,24}’.
Die Tabelle ‘tabmd’ enthält paarweise den Mailtag und den Zeitraum der Auswertung. Laut
Script-Zeile 43, also Mail am 1. für die letzten 30 Tage,…, am 31, Mail mit Auswertung der letzten 365 Tage.
Eine Beschreibung aller Variablen findest Du im angehängten PDF.
Viel Erfolg.

Hallo Jeep,

herzlichen Dank - es funktioniert wunderbar !

Schönen Abend und beste Grüße

LS

Hallo zusammen,

die neueste Version des Scripts gibt es hier: Energie Report
Die Änderungen stehen im angehängten PDF.

Hallo zusammen,
nachdem ich nun schon eine ganze Weile still und leise in diesem Forum mitlese hab ich mir gedacht ich melde mich mal endlich mal an. Und gleich kommt schon eine erste Frage:

Ich hatte mal das Rolladenscript aus diesen Forum von @jeep auf mein HC3 und meine Bedürfnisse geändert. So komplett ohne LUA-Programmier-Kenntniss fand ich mich nicht schlecht. Deshalb hatte ich mich an die Umschreibung dieser Szene gewagt. Leider erfolglos.
Hat schon jemand versucht die Energie- und Kostenberechnung für das HC3 „umzuschreiben“?

@KaWi, da das Script für eine schön formatierte Debug-Ausgabe und Mail ausgelegt ist, das HC3 aber beides nicht kann, habe ich von einer Portierung Abstand genommen. Sonst habe ich alle meine Scripte portiert. Schön das Du das Rollladenscript anpassen konntest. Da bis jetzt noch kein Interesse da war habe ich es hier noch nicht hochgeladen.

Moin zusammen, @jeep hattest du auch schon ein Script umgeschrieben, welches das ZWAVE-Mesh zeigt ?

Gruß Sven

@sven_gomann, ja hab ich, funktioniert auch gut allerdings ist die Ausgabe nicht HTML formatiert. Das kann das HC3 (noch?) nicht oder ich habs noch nicht gecheckt. Nutze das HC3 mehr zum testen und rumspielen. Ausgabe ist rein debug. .
Werde es im Laufe des Tages hochladen.

Hallo @jeep,
Mich würden alle deine Skripte interessieren, die du für die HC3 portiert hast. Ich bin gerade dabei ganz vorsichtig einen Umstieg zu planen.
Hab die HC3 schon, werde aber erst dann umsteigen, wenn wirkllich alles passt!
Dazu gehören:

  • Formatierte Debug Ausgabe
  • Emails mit Zeilenumbruch
  • alle guten Skripte verfügbar, wie zum Beispiel dein Batteriecheck Skript, welches ich immer noch genial finde :wink:

möglicherweise kannst du das (Function batterietest) hier verwenden,

--Batterietest
--------------------------------------------
function batterietest()
    --print("Start Main-Batterietest")
    local resultStr = ""
    local batlevel = 30 -- Bei <= 30% soll eine Meldung erfolgen
    local devs = api.get("/devices/?interface=battery&enabled=true&visible=true")
    for i = 1, #devs do
        if devs[i].parentId ~= 1 then
            local level = tonumber(devs[i].properties.batteryLevel)
            -- print(level, batlevel)
            if level < batlevel then
                local roomName = fibaro.getRoomName(devs[i].roomID)
                resultStr = resultStr .. roomName..'/'..devs[i].name .. " - " .. tostring(level) .. "% "
            elseif level == 255 then --255 = 100% Leer
                local roomName = fibaro.getRoomName(devs[i].roomID)
                resultStr = resultStr .. roomName..'/'..devs[i].name .. " - " ..  "Leer "
            end
        end
    end
    -- Wenn eine Batterie schwach ist dieses an das Handy senden
    if resultStr ~= "" then
        fibaro.alert("email", {2,20,586},resultStr)
    end
end -- function batterietest()
--------------------------------------------

Das habe ich mal von einem HC2 Script auf meinen HC3 umgeschrieben.
Von wem das ist kann ich nicht mehr sagen aber das Teil funktioniert gut und ist ein Bestandteil meines Main ablaufes.
Gruss Frank

1 „Gefällt mir“

Doch, Interesse ist da - wusste ja nicht, das Du schon fleißig mit dem HC 3 zu Gange bist - Rolladenscript bin ich ganz bei Dir :stuck_out_tongue_winking_eye:

@macschimmi, fleißig ist ein wenig übertrieben. Ich bastele damit einfach rum. Mein Haus läuft noch auf HC2. Nur in meinem Büro im UG sind alle Geräte in HC3. So ca. 20 Stück.
Das Rollladenscript kommt die Tage.

1 „Gefällt mir“

Vielen Dank für die Information :+1: