Modify P1 meter script solved  [Solved]

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

Moderator: leecollings

Post Reply
erijk999
Posts: 8
Joined: Thursday 21 May 2020 15:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Modify P1 meter script solved

Post by erijk999 »

How can I merge high and low consumption in this script?

Code: Select all


--[[ this dzVents script collects the Used Energy in kWh for this year, this month, this week and today

           History:
            20200114: first release on forum (thx to OedzesG for the idea )
            20200120: Some small output lay-out changes
            20200205: Fixed bugs in comparing JSON date to currentdate in month and day (thx to @Kranendijk for reporting and testing )
            20200205: Use native WP.counterToday
            20200414: Adapted for WP energy production
            20200416: Made scriptVar Unique, added logline and reverted wrong calculation of seconds per day
]]--

local scriptVar = 'Used Energy'

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

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

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    data =
    {
        Delivered =
        {
            initial = {},
        },
    },

    execute = function(dz, item)
    
        -- ***** Your settings below this line
        local Delivered = dz.devices(1) -- change to name of your WPMeter between quotes or id without quotes
        local DeliveredText = dz.devices(25) -- text device showing WP Consumption
        -- ***** No changes required below this line

         local function getGraphData(period, id)
            if period == nil then period = 'year' end
            if id == nil then id = Delivered.id end
            url = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=' .. period .. '&idx=' .. id
            dz.openURL({ url = url, callback = scriptVar })
        end
        
        local function updateTextSensor()
            local lastWeek = dz.time.week - 1
            if lastWeek == 0 then lastWeek = 52 end
            
            DeliveredText.updateText
            (
            '' .. string.format("%6.1f", dz.data.Delivered.year ) .. ' kWh ín ' .. os.date("%Y ",os.time()) .. '\n' ..
            '' .. string.format("%6.1f", dz.data.Delivered.month ) .. ' kWh ín ' .. os.date("%B %Y ",os.time()) .. '\n' ..
            '' .. string.format("%6.1f", dz.data.Delivered.week ) .. ' kWh ín ' .. 'Week ' .. dz.time.week .. ' of ' .. dz.time.year .. '\n' ..
            '' .. string.format("%6.1f", dz.data.Delivered.week2 ) .. ' kWh ín ' .. 'Week ' .. lastWeek .. ' of ' .. dz.time.year .. '\n' ..
            '' .. string.format("%6.1f", dz.data.Delivered.day ) .. ' kWh on ' .. os.date("%A %d %B %Y",os.time())
            )
        end
        
        local function processJSON(t)
            local yearVolume, monthVolume, weekVolume, week2Volume = 0, 0, 0, 0
            local currentMonthIdentifier = dz.time.rawDate:sub(1,4) .. '%-' .. dz.time.rawDate:sub(6,7)
            local day = 86400 -- (24 * 60 * 60)
            local startofWeek = os.date("%Y-%m-%d",os.time()- ( dz.time.wday - 2 ) * day)
            local startofLastWeek = os.date("%Y-%m-%d",os.time()- ( dz.time.wday + 5 ) * day)
            
            dz.log('wday: '  .. dz.time.wday,dz.LOG_FORCE)
	    dz.log('startofLastWeek: '  .. startofLastWeek,dz.LOG_FORCE)
	    dz.log('startofWeek: '  .. startofWeek,dz.LOG_FORCE)
            
            for index, Delivered in ipairs(t) do
                if Delivered.d:match(dz.time.year) then
                    yearVolume = yearVolume + Delivered.v
                end
                if Delivered.d:match(currentMonthIdentifier) then
                    monthVolume = monthVolume + Delivered.v
                end
                if Delivered.d >= startofWeek then
                    weekVolume = weekVolume + Delivered.v
                end 
                if Delivered.d >= startofLastWeek and Delivered.d < startofWeek then
			dz.log(Delivered,dz.LOG_FORCE)         
			week2Volume = week2Volume + Delivered.v
                end
            end
            
            -- getGraphData('day' )
            dz.data.Delivered.day = Delivered.counterToday
            dz.data.Delivered.month = monthVolume 
            dz.data.Delivered.year = yearVolume + 20
            dz.data.Delivered.week = weekVolume
            dz.data.Delivered.week2 = week2Volume
            updateTextSensor()
        end

        -- main
        if item.isHTTPResponse and item.isJSON then
            processJSON(item.json.result)            
        elseif item.isTimer or item.isDevice then
            getGraphData()
        else
            dz.log('Error while retrieving Delivered data. Result is ' .. item.statusText ..' ; Response is: ' .. item.data,LOG_ERROR)
        end
    end
}
Last edited by erijk999 on Monday 04 January 2021 13:35, edited 1 time in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Modify P1 meter script

Post by waaren »

erijk999 wrote: Monday 04 January 2021 11:18 How can I merge high and low consumption in this script?
Can you please elaborate a bit on what you want.

What device (idx, name, type, subtype) shows the high and low consumption?
How should the result be presented?

Did you search the forum for scripts already doing this? Did you look at the report option in the log of your P1 device?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
erijk999
Posts: 8
Joined: Thursday 21 May 2020 15:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Modify P1 meter script

Post by erijk999 »

what I only want is that high and low consumption are added together from the p1 meter (idx1)
erijk999
Posts: 8
Joined: Thursday 21 May 2020 15:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Modify P1 meter script  [Solved]

Post by erijk999 »

Solved
ikkuh
Posts: 2
Joined: Sunday 10 January 2021 16:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Modify P1 meter script solved

Post by ikkuh »

nou, de oplossing zou wel handig zijn voor anderen!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest