How to determine previous year?  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
Kranendijk
Posts: 60
Joined: Wednesday 29 May 2019 14:39
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Zwolle
Contact:

How to determine previous year?

Post by Kranendijk »

So now that all is finally scripted (for me as a non-programmer quite a challenge, but doable) using dzvents, there is only one (for now ;) ) question left.
I have the year totals of my energy consumption and solar panels, but I'd like to know the totals for the previous year. They are in the P1 meter as a json call spits them on screen. Google and the forum did not provide me with an answer, or the question was incorrect. Could be.

Below is what I use to get the data for this year. The previous year shouldn't be that hard to code, but how?? I hijacked waarens code a little bit :mrgreen:

Help :D

Code: Select all

        local function calculateYearTotal(rt)
            local yearTotal = 0
            local currentYear = dz.time.rawDate:sub(1,7)
            for id, result in  ipairs(rt) do 
                if rt[id].d:sub(1,7) == currentYear then
                    logWrite(rt[id].d .. " ==>> " .. rt[id].r1)
                    yearTotal = yearTotal + rt[id].r1
                end
            end
            for id, result in  ipairs(rt) do 
                if rt[id].d:sub(1,7) == currentYear then
                    logWrite(rt[id].d .. " ==>> " .. rt[id].r2)
                    yearTotal = yearTotal + rt[id].r2
                end
            end
            return yearTotal * 1000
        end    
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to determine previous year?

Post by waaren »

Kranendijk wrote: Thursday 28 January 2021 21:00 Below is what I use to get the data for this year. The previous year shouldn't be that hard to code, but how?? I hijacked waarens code a little bit
It will make it much less error prone if you would share the complete script but in a nutshell is the previous year the current year - 1. Or in dzVents code:

Code: Select all

	    local currentYear = dz.time.year
	    dz.log("Current year: " .. currentYear , dz.LOG_FORCE)
	    
	    local previousYear = dz.time.year - 1 
	    dz.log(" Previous year: " .. previousYear , dz.LOG_FORCE)
Your code

Code: Select all

dz.time.rawDate:sub(1,7)
does not take the year but the year and the month in the yyyy-mm format
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Kranendijk
Posts: 60
Joined: Wednesday 29 May 2019 14:39
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Zwolle
Contact:

Re: How to determine previous year?

Post by Kranendijk »

Ok, better to share the complete code then, that obviously makes more sense!

Code: Select all

-- Hier de managed counter vullen, die komt niet op het dashboard
-- Per week/maand/jaar

local httpResponses = "yearTotal"

return {
    on      =   {   
                    timer           =   { "every 8 hours" },
                    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 ***************************************************
        usageDevice = dz.devices(1)          -- Replace xxxx with ID of energyDevice you want to track
        yearTotal = dz.devices(536)        -- Create as virtual managed counter (energy) and change yyyy 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 calculateYearTotal(rt)
            local yearTotal = 0
            local currentYear = dz.time.rawDate:sub(1,7)
            for id, result in  ipairs(rt) do 
                if rt[id].d:sub(1,7) == currentYear then
                    logWrite(rt[id].d .. " ==>> " .. rt[id].r1)
                    yearTotal = yearTotal + rt[id].r1
                end
            end
            for id, result in  ipairs(rt) do 
                if rt[id].d:sub(1,7) == currentYear then
                    logWrite(rt[id].d .. " ==>> " .. rt[id].r2)
                    yearTotal = yearTotal + rt[id].r2
                end
            end
            return yearTotal * 1000
        end    

        if not item.isHTTPResponse then
            triggerJSON(usageDevice.id, "year")
        elseif item.ok then                                      -- statusCode == 2xx
            yearTotal.update(0,calculateYearTotal(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
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to determine previous year?

Post by waaren »

Kranendijk wrote: Thursday 28 January 2021 21:35 Ok, better to share the complete code then, that obviously makes more sense!
below script does collect the data of current- and last year for returned energy as collected from the history of a P1 meter and updates two managed counters with the year sums.

Code: Select all

-- Hier de managed counter vullen, die komt niet op het dashboard
-- Per week/maand/jaar

local scriptVar = 'yearTotal'

return 
{
    on = 
    {   
        timer = 
        { 
            'every 8 hours', 
        },
        
        httpResponses =
        { 
            scriptVar .. '*',
        } 
    },

    logging =   
    {   
        level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
        marker = scriptVar,   
    },
                
    execute = function(dz, item)
        -- ****************************** Your settings below this line ***************************************************
        usageDevice = dz.devices(35)     -- Replace with ID of P1 device you want to track
        currentYear = dz.devices(3487)    -- Create as virtual managed counter (energy returned) and change to the ID of the new device
        lastYear = dz.devices(3488)       -- Create as virtual managed (energy returned) and change to the ID of the new device
        -- ****************************** No changes required below this line *********************************************

        local function triggerJSON(id, period, delay)
            local  URLString = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=' .. period .. '&idx=' .. id 
            dz.openURL(
            {
                url = URLString,
                callback = scriptVar .. '_' .. period
            }).afterSec(delay or 0)                      
        end
        
        local function calculateYearsTotal(rt)
            local totals = {}
            
            for id, result in  ipairs(rt) do 
                dz.log(rt[id].d .. ' ==>> ' .. ( rt[id].r1 + rt[id].r2 ), dz.LOG_DEBUG)
                totals[rt[id].d:sub(1,4)] = ( totals[rt[id].d:sub(1,4)] or 0 ) + rt[id].r1 + rt[id].r2 
            end
            
            return totals
        end    

        if not item.isHTTPResponse then
            triggerJSON(usageDevice.id, 'year')
        elseif item.ok then                                      -- statusCode == 2xx
           local totals = calculateYearsTotal(item.json.result)
           dz.utils.dumpTable(totals)
           currentYear.updateCounter(totals[tostring(dz.time.year)] * 1000 )
           lastYear.updateCounter(totals[tostring(dz.time.year - 1)] * 1000 )
        else
            dz.log('Could not get (good) data from domoticz. Error (' .. (item.statusCode or 999) .. ')'  ,dz.LOG_ERROR)
            dz.log(item,dz.LOG_DEBUG)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Kranendijk
Posts: 60
Joined: Wednesday 29 May 2019 14:39
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Zwolle
Contact:

Re: How to determine previous year?  [Solved]

Post by Kranendijk »

Thanks!!
Kranendijk
Posts: 60
Joined: Wednesday 29 May 2019 14:39
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Zwolle
Contact:

Re: How to determine previous year?

Post by Kranendijk »

Helps a lot, however I notice the counter for previousyear does not take into account all values for the previous year.

I created both managed counters (idx 562 and 563) to check usageDevice idx 1.
In the logfile I see the below lines when running the script, did I misconfigure? The script helps me for other scripts as well to make them more efficient, great!
Spoiler: show
021-01-29 12:57:29.090 (RFXcom) Temp + Humidity (Unknown)
2021-01-29 12:58:08.088 (RFXcom) Temp + Humidity (Unknown)
2021-01-29 12:59:00.657 Status: dzVents: Debug: yearTotal: 2020-11-23 ==>> 4.061
2021-01-29 12:59:00.657 Status: dzVents: Debug: yearTotal: 2020-11-24 ==>> 0.083
2021-01-29 12:59:00.657 Status: dzVents: Debug: yearTotal: 2020-11-25 ==>> 2.761
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-11-26 ==>> 0.0
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-11-27 ==>> 0.0
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-11-28 ==>> 0.0
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-11-29 ==>> 1.292
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-11-30 ==>> 0.0
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-12-01 ==>> 0.371
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-12-02 ==>> 0.0
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-12-03 ==>> 0.0
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-12-04 ==>> 0.151
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-12-05 ==>> 1.161
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-12-06 ==>> 0.067
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-12-07 ==>> 0.0
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-12-08 ==>> 0.0
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-12-09 ==>> 0.0
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-12-10 ==>> 0.0
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-12-11 ==>> 0.151
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-12-12 ==>> 0.0
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-12-13 ==>> 0.087
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-12-14 ==>> 0.104
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-12-15 ==>> 0.0
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-12-16 ==>> 0.673
2021-01-29 12:59:00.658 Status: dzVents: Debug: yearTotal: 2020-12-17 ==>> 0.282
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2020-12-18 ==>> 1.106
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2020-12-19 ==>> 0.64
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2020-12-20 ==>> 0.27
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2020-12-21 ==>> 0.0
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2020-12-22 ==>> 0.0
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2020-12-23 ==>> 0.0
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2020-12-24 ==>> 0.0
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2020-12-25 ==>> 2.517
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2020-12-26 ==>> 0.027
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2020-12-27 ==>> 0.0
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2020-12-28 ==>> 0.56
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2020-12-29 ==>> 0.0
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2020-12-30 ==>> 2.353
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2020-12-31 ==>> 0.0
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2021-01-01 ==>> 0.262
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2021-01-02 ==>> 0.0
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2021-01-03 ==>> 0.0
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2021-01-04 ==>> 0.0
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2021-01-05 ==>> 0.0
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2021-01-06 ==>> 0.0
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2021-01-07 ==>> 0.027
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2021-01-08 ==>> 0.638
2021-01-29 12:59:00.659 Status: dzVents: Debug: yearTotal: 2021-01-09 ==>> 1.436
2021-01-29 12:59:00.660 Status: dzVents: Debug: yearTotal: 2021-01-10 ==>> 0.565
2021-01-29 12:59:00.660 Status: dzVents: Debug: yearTotal: 2021-01-11 ==>> 0.0
2021-01-29 12:59:00.660 Status: dzVents: Debug: yearTotal: 2021-01-12 ==>> 3.604
2021-01-29 12:59:00.660 Status: dzVents: Debug: yearTotal: 2021-01-13 ==>> 0.137
2021-01-29 12:59:00.660 Status: dzVents: Debug: yearTotal: 2021-01-14 ==>> 2.048
2021-01-29 12:59:00.660 Status: dzVents: Debug: yearTotal: 2021-01-15 ==>> 0.0
2021-01-29 12:59:00.660 Status: dzVents: Debug: yearTotal: 2021-01-16 ==>> 0.0
2021-01-29 12:59:00.660 Status: dzVents: Debug: yearTotal: 2021-01-17 ==>> 0.0
2021-01-29 12:59:00.660 Status: dzVents: Debug: yearTotal: 2021-01-18 ==>> 0.0
2021-01-29 12:59:00.660 Status: dzVents: Debug: yearTotal: 2021-01-19 ==>> 0.0
2021-01-29 12:59:00.660 Status: dzVents: Debug: yearTotal: 2021-01-20 ==>> 0.022
2021-01-29 12:59:00.660 Status: dzVents: Debug: yearTotal: 2021-01-21 ==>> 1.697
2021-01-29 12:59:00.660 Status: dzVents: Debug: yearTotal: 2021-01-22 ==>> 0.395
2021-01-29 12:59:00.660 Status: dzVents: Debug: yearTotal: 2021-01-23 ==>> 1.22
2021-01-29 12:59:00.660 Status: dzVents: Debug: yearTotal: 2021-01-24 ==>> 1.942
2021-01-29 12:59:00.660 Status: dzVents: Debug: yearTotal: 2021-01-25 ==>> 5.143
2021-01-29 12:59:00.661 Status: dzVents: Debug: yearTotal: 2021-01-26 ==>> 3.266
2021-01-29 12:59:00.661 Status: dzVents: Debug: yearTotal: 2021-01-27 ==>> 0.0
2021-01-29 12:59:00.661 Status: dzVents: Debug: yearTotal: 2021-01-28 ==>> 0.0
2021-01-29 12:59:00.661 Status: dzVents: Debug: yearTotal: 2021-01-29 ==>> 0.0
2021-01-29 12:59:00.661 Status: dzVents: > 2021: 22.402
2021-01-29 12:59:00.661 Status: dzVents: > 2020: 1677.343
2021-01-29 12:59:00.661 Status: dzVents: Info: yearTotal: ------ Finished 000AapJaarenVorigJaar
2021-01-29 12:59:00.907 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2021-01-29 12:59:25.886 (RFXcom) Temp + Humidity (Unknown)
The script as I use it (untouched apart from idx-es and timer which I changed to 1 minute to have results on screen almost directly):
Spoiler: show
-- Hier de managed counter vullen, die komt niet op het dashboard
-- Per week/maand/jaar

local scriptVar = 'yearTotal'

return
{
on =
{
timer =
{
'every 1 minutes',
},

httpResponses =
{
scriptVar .. '*',
}
},

logging =
{
level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
marker = scriptVar,
},

execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
usageDevice = dz.devices(1) -- Replace with ID of P1 device you want to track
currentYear = dz.devices(562) -- Create as virtual managed counter (energy returned) and change to the ID of the new device
lastYear = dz.devices(563) -- Create as virtual managed (energy returned) and change to the ID of the new device
-- ****************************** No changes required below this line *********************************************

local function triggerJSON(id, period, delay)
local URLString = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=' .. period .. '&idx=' .. id
dz.openURL(
{
url = URLString,
callback = scriptVar .. '_' .. period
}).afterSec(delay or 0)
end

local function calculateYearsTotal(rt)
local totals = {}

for id, result in ipairs(rt) do
dz.log(rt[id].d .. ' ==>> ' .. ( rt[id].r1 + rt[id].r2 ), dz.LOG_DEBUG)
totals[rt[id].d:sub(1,4)] = ( totals[rt[id].d:sub(1,4)] or 0 ) + rt[id].r1 + rt[id].r2
end

return totals
end

if not item.isHTTPResponse then
triggerJSON(usageDevice.id, 'year')
elseif item.ok then -- statusCode == 2xx
local totals = calculateYearsTotal(item.json.result)
dz.utils.dumpTable(totals)
currentYear.updateCounter(totals[tostring(dz.time.year)] * 1000 )
lastYear.updateCounter(totals[tostring(dz.time.year - 1)] * 1000 )
else
dz.log('Could not get (good) data from domoticz. Error (' .. (item.statusCode or 999) .. ')' ,dz.LOG_ERROR)
dz.log(item,dz.LOG_DEBUG)
end
end
}
json gives me output for the entire year, starting 2020-01-24 when the solar panels were placed.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to determine previous year?

Post by waaren »

Kranendijk wrote: Friday 29 January 2021 13:09 Helps a lot, however I notice the counter for previousyear does not take into account all values for the previous year.
Looking at the log and the totals, I guess that the totals are about right but that the number of lines in a short time do flood the log function causing your display to skip most loglines.

I added some lines and a function to consolidate the loglines in months to limit the number of loglines (only when in debug mode)

Code: Select all

-- Hier de managed counter vullen, die komt niet op het dashboard
-- Per week/maand/jaar

local scriptVar = 'yearTotal'

return
{
    on =
    {
        timer =
        {
            'every minute',
        },

        httpResponses =
        {
            scriptVar .. '*',
        }
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
        marker = scriptVar,
    },

    execute = function(dz, item)
        -- ****************************** Your settings below this line ***************************************************
        usageDevice = dz.devices(1)     -- Replace with ID of P1 device you want to track
        currentYear = dz.devices(562)    -- Create as virtual managed counter (energy returned) and change to the ID of the new device
        lastYear = dz.devices(563)       -- Create as virtual managed (energy returned) and change to the ID of the new device
        -- ****************************** No changes required below this line *********************************************

        local debug =  _G.logLevel == dz.LOG_DEBUG

        local function dumpSorted(t)
            local st = {}
            for key, _ in pairs(t) do
                table.insert(st, key)
            end
            table.sort(st)
            for _, key in ipairs(st) do
                dz.log(key .. ': '  ..t[key],dz.LOG_DEBUG)
            end
        end

        local function triggerJSON(id, period, delay)
            local  URLString = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=' .. period .. '&idx=' .. id
            dz.openURL(
            {
                url = URLString,
                callback = scriptVar .. '_' .. period
            }).afterSec(delay or 0)
        end

        local function calculateYearsTotal(rt)
            local totals = {}

            for id, result in  ipairs(rt) do
                if debug then
                    totals[rt[id].d:sub(1,7)] = ( totals[rt[id].d:sub(1,7)] or 0 ) + rt[id].r1 + rt[id].r2
                end
                totals[rt[id].d:sub(1,4)] = ( totals[rt[id].d:sub(1,4)] or 0 ) + rt[id].r1 + rt[id].r2
            end

            return totals
        end

        if not item.isHTTPResponse then
            triggerJSON(usageDevice.id, 'year')
        elseif item.ok then                                      -- statusCode == 2xx
           local totals = calculateYearsTotal(item.json.result)
           if debug then dumpSorted(totals) end
           currentYear.updateCounter(totals[tostring(dz.time.year)] * 1000 )
           lastYear.updateCounter(totals[tostring(dz.time.year - 1)] * 1000 )
        else
            dz.log('Could not get (good) data from domoticz. Error (' .. (item.statusCode or 999) .. ')'  ,dz.LOG_ERROR)
            dz.log(item,dz.LOG_DEBUG)
        end
    end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Kranendijk
Posts: 60
Joined: Wednesday 29 May 2019 14:39
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Zwolle
Contact:

Re: How to determine previous year?

Post by Kranendijk »

Yesssss! Thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest