Get monthly returned Kwh from P1 meter

Moderator: leecollings

Post Reply
banne
Posts: 5
Joined: Wednesday 14 July 2021 16:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Get monthly returned Kwh from P1 meter

Post by banne »

For people who want to know what they returned in total trough their smart meter... Here is the solution. It adds up the value for return1 and return2 (high and low) The base from the code I borrowed here on the forum, no idea who made the original... But this one is adapted to calculate the total monthly return instead of the total monthly consumption.

Just a tip for future users, It costed me 4 nights to find out... the r1 and r2 in the code are return1 and return2... In the original code it said v and v2 which are the numbers for the power consumption high and low...

NOTE! This is the ACTUAL returned value, not what you used right away but what went through the smart meter and is deducted from your total monthly consumption (at least in the Netherlands it works like that for how long it will last)

The return value will look like this:
Schermafbeelding 2021-07-14 om 17.19.49.png
Schermafbeelding 2021-07-14 om 17.19.49.png (21.03 KiB) Viewed 900 times

Code: Select all

local httpResponses = "monthTotal"

return {
    on      =   {   
                    timer           =   { "every 1 minutes" },
                    httpResponses   =   { httpResponses .. "*" } 
                },

    logging =   {   
                    level           =   domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
                    marker          =   httpResponse   
                },
                
    execute = function(dz, item)
        -- ****************************** Your settings below this line ***************************************************
        usageDelivered = dz.devices(1)          -- Replace 1 with ID of energyDevice you want to track
        monthTotal = dz.devices(65)        -- Create as virtual managed counter (energy) and change 65 to the ID of the new device
        -- ****************************** No changes required below this line *********************************************
        
        local function logWrite(str,level)
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
        
        local function triggerJSON(id, period, delay)
            local delay = delay or 0
            local  URLString   =    dz.settings['Domoticz url'] .. "/json.htm?type=graph&sensor=counter&range=" .. 
                                    period .. "&idx=" .. id 
            dz.openURL({    url = URLString,
                            method = "GET",
                            callback = httpResponses .. "_" .. period}).afterSec(delay)                      
        end
        
        local function calculateMonthTotal(rt)
            local monthTotal = 0
            local currentMonth = dz.time.rawDate:sub(1,7)
            for id, result in  ipairs(rt) do 
                if rt[id].d:sub(1,7) == currentMonth then
                    logWrite(rt[id].d .. " ==>> " .. rt[id].r2)
                    monthTotal = monthTotal + rt[id].r2 + rt[id].r1
                end
            end
            return ( ( monthTotal ) * 1000 )
        end    
        
        if not item.isHTTPResponse then
            triggerJSON(usageDelivered.id, "month")
        elseif item.ok then                                      -- statusCode == 2xx
            monthTotal.updateCounter(calculateMonthTotal(item.json.result))
        else
            logWrite("Could not get (good) data from domoticz. Error (" .. (item.statusCode or 999) .. ")"  ,dz.LOG_ERROR)
            logWrite(item.data)
        end
    end
}
        
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest