Khw Last Two months  [Solved]

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

Moderator: leecollings

Post Reply
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Khw Last Two months

Post by djdevil »

Hi I'm using this script to extrapolate the Khw of the past two months.
But my invoice always starts from the 3rd of every month, so it would be necessary for the script to calculate like this from 03.05.2020 to 03.07.2020. How can I do thanks


Code: Select all

local httpResponses = 'twoMonthTotal'

return {
    on      =   {   
                    timer           =   { 'on 1/* at 04:17' }, -- if you get data from previous months you only have to do this once a month at a quiet time
                    httpResponses   =   { httpResponses .. '*' } 
                },

    logging =   {   
                    level           =   domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
                    marker          =   httpResponses
                },
                
    execute = function(dz, item)
        -- ****************************** Your settings below this line ***************************************************
        usageDevice = dz.devices(580)          -- Replace xxxx with ID of energyDevice you want to track
        twoMonthTotals = dz.devices(725)        -- 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 calculateTwoMonthTotal(rt)
            local twoMonthTotal = 0
			local dateFmt = '%Y-%m'
            monthMinus1 = os.date(dateFmt,os.time{day=3, year=dz.time.year, month=dz.time.month - 1 })
            monthMinus2 = os.date(dateFmt,os.time{day=3, year=dz.time.year, month=dz.time.month - 2 })
            for id, result in  ipairs(rt) do 
                if result.d:sub(1,7) == monthMinus1 or result.d:sub(1,7) == monthMinus2 then
                    logWrite(result.d .. ' ==>> ' .. result.v)
                    twoMonthTotal = twoMonthTotal + result.v
                end
            end
            return twoMonthTotal * 1000
        end    
        
        if not item.isHTTPResponse then
            triggerJSON(usageDevice.id, 'year')
        elseif item.ok then                                      -- statusCode == 2xx
            twoMonthTotals.update(0,calculateTwoMonthTotal(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: Khw Last Two months

Post by waaren »

djdevil wrote: Thursday 16 July 2020 10:50 Hi I'm using this script to extrapolate the Khw of the past two months.
But my invoice always starts from the 3rd of every month, so it would be necessary for the script to calculate like this from 03.05.2020 to 03.07.2020. How can I do thanks
At what day of the month are you going to schedule the script? Somewhere in July after the 3rd ? or something different?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Khw Last Two months

Post by djdevil »

hello waaren thanks for the help, from the 3rd of every month the script should start! and the calculation of the last two months always from day 3
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Khw Last Two months

Post by waaren »

djdevil wrote: Thursday 16 July 2020 19:36 hello waaren thanks for the help, from the 3rd of every month the script should start! and the calculation of the last two months always from day 3
Can you try this one.

Code: Select all

local scriptVar = 'twoMonthTotal'

return
{
    on =
    {
        timer =
        {
            'on 4/* at 04:17'   -- if you get data from previous months you only have to do this once a month at a quiet time
        },
        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(580)          -- Replace xxxx with ID of energyDevice you want to track
        twoMonthTotals = dz.devices(725)        -- Create as virtual managed counter (energy) and change yyyy to the ID of the new device
        -- ****************************** No changes required below this line *********************************************

        local function getDates(month, day )
            local day = day or 3
            local endMonth = month or dz.time.month
            local beginMonth = (endMonth + 10) % 12
            local beginYear = dz.time.year

            if beginMonth > endMonth then beginYear = beginYear - 1 end

            local endDate = dz.time.year .. '-' .. dz.utils.leadingZeros(endMonth,2) .. '-' .. dz.utils.leadingZeros(day,2)
            local beginDate = beginYear .. '-' .. dz.utils.leadingZeros(beginMonth,2) .. '-' .. dz.utils.leadingZeros(day,2)
            return beginDate, endDate
        end

        local function triggerJSON(id, period)
            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 = scriptVar .. '_' .. period})
        end

        local function calculateTwoMonthTotal(rt, firstDate, lastDate)
            local twoMonthTotal = 0
            local beginDate, endDate = getDates()
            for id, result in  ipairs(rt) do
                if ( result.d >= beginDate) and ( result.d < endDate ) then
                    dz.log (result.d .. ' ==>> ' .. result.v,dz.LOG_DEBUG)
                    twoMonthTotal = twoMonthTotal + result.v
                end
            end
            return twoMonthTotal * 1000
        end

        if not item.isHTTPResponse then
            triggerJSON(usageDevice.id, 'year')
        elseif item.ok then                                      -- statusCode == 2xx
            local twoMonthTotal = calculateTwoMonthTotal(item.json.result)
            dz.log('twoMonthTotal = ' .. twoMonthTotal,dz.LOG_DEBUG)
            twoMonthTotals.update(0,twoMonthTotal)
        else
            dz.log('Could not get (good) data from domoticz. Error (' .. (item.statusCode or 999) .. ')' ,dz.LOG_ERROR)
            dz.log(item.data,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
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Khw Last Two months

Post by djdevil »

waaren wrote: Thursday 16 July 2020 20:26
djdevil wrote: Thursday 16 July 2020 19:36 hello waaren thanks for the help, from the 3rd of every month the script should start! and the calculation of the last two months always from day 3
Can you try this one.

Code: Select all

local scriptVar = 'twoMonthTotal'

return
{
    on =
    {
        timer =
        {
            'on 4/* at 04:17'   -- if you get data from previous months you only have to do this once a month at a quiet time
        },
        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(580)          -- Replace xxxx with ID of energyDevice you want to track
        twoMonthTotals = dz.devices(725)        -- Create as virtual managed counter (energy) and change yyyy to the ID of the new device
        -- ****************************** No changes required below this line *********************************************

        local function getDates(month, day )
            local day = day or 3
            local endMonth = month or dz.time.month
            local beginMonth = (endMonth + 10) % 12
            local beginYear = dz.time.year

            if beginMonth > endMonth then beginYear = beginYear - 1 end

            local endDate = dz.time.year .. '-' .. dz.utils.leadingZeros(endMonth,2) .. '-' .. dz.utils.leadingZeros(day,2)
            local beginDate = beginYear .. '-' .. dz.utils.leadingZeros(beginMonth,2) .. '-' .. dz.utils.leadingZeros(day,2)
            return beginDate, endDate
        end

        local function triggerJSON(id, period)
            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 = scriptVar .. '_' .. period})
        end

        local function calculateTwoMonthTotal(rt, firstDate, lastDate)
            local twoMonthTotal = 0
            local beginDate, endDate = getDates()
            for id, result in  ipairs(rt) do
                if ( result.d >= beginDate) and ( result.d < endDate ) then
                    dz.log (result.d .. ' ==>> ' .. result.v,dz.LOG_DEBUG)
                    twoMonthTotal = twoMonthTotal + result.v
                end
            end
            return twoMonthTotal * 1000
        end

        if not item.isHTTPResponse then
            triggerJSON(usageDevice.id, 'year')
        elseif item.ok then                                      -- statusCode == 2xx
            local twoMonthTotal = calculateTwoMonthTotal(item.json.result)
            dz.log('twoMonthTotal = ' .. twoMonthTotal,dz.LOG_DEBUG)
            twoMonthTotals.update(0,twoMonthTotal)
        else
            dz.log('Could not get (good) data from domoticz. Error (' .. (item.statusCode or 999) .. ')' ,dz.LOG_ERROR)
            dz.log(item.data,dz.LOG_DEBUG)
        end
    end
}
Hello i try the code this the log debug it seems that we miss the day 3-07 inside, in fact it stops at 02-07 how come?

Code: Select all

 2020-07-17 10:25:01.452 Status: dzVents: Debug: twoMonthTotal: Processing device-adapter for kWh Bimestre: Counter device adapter
2020-07-17 10:25:01.452 Status: dzVents: Debug: twoMonthTotal: 2020-05-03 ==>> 10.019
2020-07-17 10:25:01.452 Status: dzVents: Debug: twoMonthTotal: 2020-05-04 ==>> 9.560
2020-07-17 10:25:01.452 Status: dzVents: Debug: twoMonthTotal: 2020-05-05 ==>> 9.084
2020-07-17 10:25:01.452 Status: dzVents: Debug: twoMonthTotal: 2020-05-06 ==>> 8.057
2020-07-17 10:25:01.452 Status: dzVents: Debug: twoMonthTotal: 2020-05-07 ==>> 12.043
2020-07-17 10:25:01.452 Status: dzVents: Debug: twoMonthTotal: 2020-05-08 ==>> 13.821
2020-07-17 10:25:01.452 Status: dzVents: Debug: twoMonthTotal: 2020-05-09 ==>> 7.029
2020-07-17 10:25:01.452 Status: dzVents: Debug: twoMonthTotal: 2020-05-10 ==>> 8.548
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-11 ==>> 13.938
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-12 ==>> 8.691
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-13 ==>> 6.887
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-14 ==>> 9.930
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-15 ==>> 9.159
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-16 ==>> 7.321
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-17 ==>> 11.118
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-18 ==>> 8.680
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-19 ==>> 9.610
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-20 ==>> 8.466
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-21 ==>> 7.943
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-22 ==>> 6.489
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-23 ==>> 7.454
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-24 ==>> 11.015
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-25 ==>> 7.439
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-26 ==>> 6.944
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-27 ==>> 8.195
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-28 ==>> 7.366
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-29 ==>> 7.602
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-30 ==>> 8.298
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-05-31 ==>> 8.477
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-06-01 ==>> 9.775
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-06-02 ==>> 10.540
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-06-03 ==>> 9.594
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-06-04 ==>> 8.503
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-06-05 ==>> 8.521
2020-07-17 10:25:01.453 Status: dzVents: Debug: twoMonthTotal: 2020-06-06 ==>> 6.990
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-07 ==>> 8.701
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-08 ==>> 10.842
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-09 ==>> 9.295
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-10 ==>> 6.255
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-11 ==>> 10.423
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-12 ==>> 8.109
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-13 ==>> 9.982
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-14 ==>> 7.804
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-15 ==>> 6.962
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-16 ==>> 7.296
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-17 ==>> 6.789
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-18 ==>> 8.137
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-19 ==>> 6.128
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-20 ==>> 7.774
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-21 ==>> 5.838
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-22 ==>> 9.419
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-23 ==>> 9.607
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-24 ==>> 5.562
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-25 ==>> 7.420
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-26 ==>> 8.156
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-27 ==>> 7.867
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-28 ==>> 7.092
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-29 ==>> 9.795
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-06-30 ==>> 7.071
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-07-01 ==>> 7.422
2020-07-17 10:25:01.454 Status: dzVents: Debug: twoMonthTotal: 2020-07-02 ==>> 9.252
2020-07-17 10:25:01.455 Status: dzVents: Debug: twoMonthTotal: twoMonthTotal = 522104.0 
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Khw Last Two months

Post by waaren »

djdevil wrote: Friday 17 July 2020 10:27 Hello i try the code this the log debug it seems that we miss the day 3-07 inside, in fact it stops at 02-07 how come?

Code: Select all

   if ( result.d >= beginDate) and ( result.d < endDate ) then
                    dz.log (result.d .. ' ==>> ' .. result.v,dz.LOG_DEBUG)
                    twoMonthTotal = twoMonthTotal + result.v
                end
I assume you need to either exclude the last or the first date (otherwise you will pay this day in two periods.

You control this by using
> or >= ; greater then or greater or equal
< or <= ; smaller then or smaller or equal
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Khw Last Two months

Post by djdevil »

waaren wrote: Friday 17 July 2020 11:45
djdevil wrote: Friday 17 July 2020 10:27 Hello i try the code this the log debug it seems that we miss the day 3-07 inside, in fact it stops at 02-07 how come?

Code: Select all

   if ( result.d >= beginDate) and ( result.d < endDate ) then
                    dz.log (result.d .. ' ==>> ' .. result.v,dz.LOG_DEBUG)
                    twoMonthTotal = twoMonthTotal + result.v
                end
I assume you need to either exclude the last or the first date (otherwise you will pay this day in two periods.

You control this by using
> or >= ; greater then or greater or equal
< or <= ; smaller then or smaller or equal
hello thanks for replying, so to include also the 3 of the last month, how should I do? or is your code already included? because my bills always start from the 3rd day of the last two months

the total carried out by hand from 529.164 khw of the previous two months from 3-05 to 3-07 inclusive
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Khw Last Two months  [Solved]

Post by waaren »

djdevil wrote: Friday 17 July 2020 12:06 hello thanks for replying, so to include also the 3 of the last month, how should I do? or is your code already included? because my bills always start from the 3rd day of the last two months
Change this part

Code: Select all

   if ( result.d >= beginDate) and ( result.d < endDate ) then
                    dz.log (result.d .. ' ==>> ' .. result.v,dz.LOG_DEBUG)
                    twoMonthTotal = twoMonthTotal + result.v
                end
to

Code: Select all

   if ( result.d >= beginDate) and ( result.d <= endDate ) then
                    dz.log (result.d .. ' ==>> ' .. result.v,dz.LOG_DEBUG)
                    twoMonthTotal = twoMonthTotal + result.v
                end
But this not make sense. One day is now part of the total in two periods

period 1: 3/1 -3/3
period 2: 3/3 - 3/5 ==>> 3/3 is part of the total in period 1 and in period 2
period 3: 3/5 - 3/7 ==>> 3/5 is part of the total in period 2 and in period 3
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Khw Last Two months

Post by djdevil »

Thanks was what I needed! Dear as always to help! Thanks Waaren
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest