Page 1 of 1

Howto compare the current energy consumption with the consumption yesterday at the same time

Posted: Saturday 02 November 2019 12:18
by steef27
I am looking for a possibility to compare the current energy consumption with the consumption yesterday at the same time.
I want to be able to determine whether the cumulated consumption is higher or lower than yesterday at the same time.
For now I have found scripts that calculate averages over 24 hours, but that is not what I am looking for.
Does anyone have an idea? Thanks in advance!

Re: Howto compare the current energy consumption with the consumption yesterday at the same time

Posted: Sunday 03 November 2019 10:52
by waaren
steef27 wrote: Saturday 02 November 2019 12:18 I am looking for a possibility to compare the current energy consumption with the consumption yesterday at the same time.
You could try withthe script below. It use the persistent historical data features of dzVents.

When not yet familiar with dzVents please start with reading Get started Before implementing (~ 5 minutes). Special attention please for "In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents disabled' is not checked. Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state."

Code: Select all

--[[
    this dzVents script collects the current energy return / usage to enable compararisment with yestedays figures  
]]--

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

    logging = { level = domoticz.LOG_DEBUG },

    data = { power = { history = true, maxHours = 25, maxItems = 1000 }},

    execute = function(dz, item)
        _G.logMarker =  _G.moduleLabel

        local smartMeter = dz.devices('Power') -- change to name of your smartMeter 

        local counterDeliveredToday = smartMeter.counterDeliveredToday
        local counterToday = smartMeter.counterToday 

        dz.log('counterDeliveredToday: ' .. counterDeliveredToday .. ': kwh',dz.LOG_DEBUG)
        dz.log('counterToday: ' .. counterToday,dz.LOG_DEBUG)

        dz.data.power.add -- Store current values in persistent historical data
        (
            {
                ['delivered'] = counterDeliveredToday, 
                ['used'] = counterToday 
            }
        )
        local power, index = dz.data.power.getAtTime('24:00:00') 
        if not(power) then 
            dz.log('Not enough data collected yet. Taking oldest data to compare with',dz.LOG_FORCE)
            power, index = dz.data.power.getOldest() 
        end

        dz.log('delivery dailytotal at ' .. power.time.rawDateTime  .. ': ' .. 
                power.data.delivered ..': kwh . ==> Delta (' .. dz.utils.round(( power.data.delivered - counterDeliveredToday ),4) .. ' kwh)',dz.LOG_DEBUG)
        dz.log('usage dailytotal at ' .. power.time.rawDateTime  .. ': ' .. 
                power.data.used ..': kwh . ==> Delta (' .. dz.utils.round(( power.data.used - counterToday ),4) .. ' kwh)',dz.LOG_FORCE)
    end
}

Re: Howto compare the current energy consumption with the consumption yesterday at the same time

Posted: Monday 04 November 2019 9:51
by steef27
I get an error:

2019-11-04 09:40:02.179 Status: dzVents: !Info: Not enough data collected yet. Taking oldest data to compare with
2019-11-04 09:40:02.180 Status: dzVents: Error (2.4.19): An error occured when calling event handler Log
2019-11-04 09:40:02.180 Status: dzVents: Error (2.4.19): /home/pi/domoticz/scripts/dzVents/generated_scripts/Log.lua:38: attempt to concatenate field 'rawDateTime' (a nil value)

Re: Howto compare the current energy consumption with the consumption yesterday at the same time

Posted: Monday 04 November 2019 9:59
by waaren
steef27 wrote: Monday 04 November 2019 9:51 I get an error:
attempt to concatenate field 'rawDateTime' (a nil value)
That is because rawDateTime is not available yet in 2.4.19. So use below script in which I used a combination of rawDate and rawTime or update to a recent Beta.

Code: Select all

--[[
    this dzVents script collects the current energy return / usage to enable comparing with yesterdays figures  
]]--

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

    logging = { level = domoticz.LOG_DEBUG },

    data = { power = { history = true, maxHours = 25, maxItems = 1000 }},

    execute = function(dz, item)
        _G.logMarker =  _G.moduleLabel

        local smartMeter = dz.devices('Power') -- change to name of your smartMeter 

        local counterDeliveredToday = smartMeter.counterDeliveredToday
        local counterToday = smartMeter.counterToday 

        dz.log('counterDeliveredToday: ' .. counterDeliveredToday .. ': kwh',dz.LOG_DEBUG)
        dz.log('counterToday: ' .. counterToday,dz.LOG_DEBUG)

        dz.data.power.add -- Store current values in persistent historical data
        (
            {
                ['delivered'] = counterDeliveredToday, 
                ['used'] = counterToday 
            }
        )
        local power, index = dz.data.power.getAtTime('24:00:00') 
        if not(power) then 
            dz.log('Not enough data collected yet. Taking oldest data to compare with',dz.LOG_FORCE)
            power, index = dz.data.power.getOldest() 
        end

        dz.log('delivery dailytotal at ' .. power.time.rawDate .. ' ' power.time.rawTime  .. ': ' .. 
                power.data.delivered ..': kwh . ==> Delta (' .. dz.utils.round(( power.data.delivered - counterDeliveredToday ),4) .. ' kwh)',dz.LOG_FORCE)
        dz.log('usage dailytotal at ' .. power.time.rawDate .. ' ' power.time.rawTime  .. ': ' .. 
                power.data.used ..': kwh . ==> Delta (' .. dz.utils.round(( power.data.used - counterToday ),4) .. ' kwh)',dz.LOG_FORCE)
    end
}

Re: Howto compare the current energy consumption with the consumption yesterday at the same time

Posted: Monday 04 November 2019 17:04
by steef27
Thanks! Works great.
Can I also add my Gasmeter in this script? Or do I need to create another script?

Re: Howto compare the current energy consumption with the consumption yesterday at the same time  [Solved]

Posted: Monday 04 November 2019 18:04
by waaren
steef27 wrote: Monday 04 November 2019 17:04 Thanks! Works great.
Can I also add my Gasmeter in this script? Or do I need to create another script?
Yes, not a problem but best to save this script with another name or remove datafile of current script.
Datafile to be found at <domoticz dir>/scripts/dzVents/data/__data_<scriptName>.lua

Code: Select all

--[[
    this dzVents script collects the current energy return / usage and gas usage to enable comparing with yesterdays figures  
]]--

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

    logging = { level = domoticz.LOG_DEBUG },

    data = { energy = { history = true, maxHours = 25, maxItems = 1000 }},

    execute = function(dz, item)
        _G.logMarker =  _G.moduleLabel

        local smartMeter = dz.devices('Power') -- change to name of your smartMeter 
		local gas = dz.devices('Gas') -- change to name of your gasMeter
		
        local counterDeliveredToday = smartMeter.counterDeliveredToday
        local counterToday = smartMeter.counterToday 
		local gasToday = gas.counterToday
		
        dz.log('counterDeliveredToday: ' .. counterDeliveredToday .. ': kwh',dz.LOG_DEBUG)
        dz.log('counterToday: ' .. counterToday,dz.LOG_DEBUG)
        dz.log('Gas usage today: ' .. gasToday,dz.LOG_DEBUG)

        dz.data.energy.add -- Store current values in persistent historical data
        (
            {
                ['delivered'] = counterDeliveredToday, 
                ['used'] = counterToday, 
                ['gas'] = gasToday, 
            }
        )
        local energy, index = dz.data.energy.getAtTime('24:00:00') 
        if not(energy) then 
            dz.log('Not enough data collected yet. Taking oldest data to compare with',dz.LOG_FORCE)
            energy, index = dz.data.energy.getOldest() 
        end

        dz.log('delivery dailytotal at ' .. energy.time.rawDate .. ' ' .. energy.time.rawTime  .. ': ' .. 
                energy.data.delivered ..': kwh . ==> Delta (' .. dz.utils.round(( energy.data.delivered - counterDeliveredToday ),4) .. ' kwh)',dz.LOG_FORCE)
        dz.log('usage dailytotal at ' .. energy.time.rawDate .. ' ' .. energy.time.rawTime  .. ': ' .. 
                energy.data.used ..': kwh . ==> Delta (' .. dz.utils.round(( energy.data.used - counterToday ),4) .. ' kwh)',dz.LOG_FORCE)
        dz.log('daily gas-usage at ' .. energy.time.rawDate .. ' ' .. energy.time.rawTime  .. ': ' .. 
                energy.data.gas ..': m3 . ==> Delta (' .. dz.utils.round(( energy.data.gas - gasToday ),4) .. ' m3)',dz.LOG_FORCE)
    end
}