Page 1 of 1

Different kWh values

Posted: Saturday 20 March 2021 22:22
by CronoS
Hi,

I have got a question. I am running a DZvents script for quite some time now to see how much kwh I used in my house, this on a P1 meter and my Solaredge solarpanels. Bascally the script is working OK for quite some time now.

In this script I look at the kwh's on 2 different ways. The first device is using a General custom device for kWh. In the script I look how much kwh is being generated from the solarpanels then I take this value minus the kWh that is delivered back into the P1 sensor and I add the value what I have bought/used from the P1 sensor. This is all in kWh, I place the value in a General custom device which gives me the kwh's used in house
Image

Now, the second method is that I do the same but now in Watts. So I look how much Watt is being generated minus the watts that are delivered back in the P1 sensor and then I add the usage value from the P1. The result I put in an electric kWh device where the I let the device calculate the kWh on the device. The advantage of using an electric kwh device in Domoticz is that I have better logging and I can look at the costs, which I why I prefer this device. on a later stage I will delete the first option in the script..
Image

Now, I would suspect that using both methods would give me the same kWh value.. but they are not, they are off 0,2 kwh. Could there be that when a Electric / kwh device calulates is always a little bit off then to read out the devices, because of timing?

Re: Different kWh values

Posted: Saturday 20 March 2021 23:18
by waaren
CronoS wrote: Saturday 20 March 2021 22:22 Now, I would suspect that using both methods would give me the same kWh value.. but they are not, they are off 0,2 kwh. Could there be that when a Electric / kwh device calulates is always a little bit off then to read out the devices, because of timing?
The first method is based on absolute meter values. That should be correct.

The "Watt method" is an approximation. Even within one second the amount of Watts can vary and domoticz only gets the value of a moment. It use that value and the time past since the last sample and do the math.

Assume you use 100 Watt at second 1-9 and 200 Watt at second 10 and domoticz gets the sample of second 10. It can only calculate with that value of 200 because it does not know about the 9 previous seconds but the average Watts over the last 10 seconds is 110 Watt.

Re: Different kWh values

Posted: Sunday 21 March 2021 21:58
by CronoS
Thanks. That is indeed what is happening I think. Is there any way to work around that in the Electric device?

Re: Different kWh values

Posted: Sunday 21 March 2021 23:15
by waaren
CronoS wrote: Sunday 21 March 2021 21:58 Thanks. That is indeed what is happening I think. Is there any way to work around that in the Electric device?
Not sure but if you share the current script I could try to modify it in such a way that it will update (second method) the device based on the device set to 'from device' and no longer based on the device set to 'Computed'

Re: Different kWh values

Posted: Monday 22 March 2021 12:05
by CronoS
Thanks waaren. It is not the nicest script, it does more then only that. So hopefully you can read it OK, if not I don't blame you :)
But here it is:

Code: Select all

--[[  dzVents script to Parse P1 Smart Meter Electricity value into separate Meter Readings.

local fetchIntervalMins = 1    -- (Integer) Minutes frequence of this script execution 1 = every minute, 10 = every 10 minutes, etc ) must be one of (1,2,3,4,5,6,10,12,15,20,30)

]]--

local ScriptVersion = '0.1.9'
local fetchIntervalMins = 1

return
{
    on =
    {
        devices = {'SolarEdge Act. Vermogen (AC)','Stroom P1'},
		timer = { 'every minute between 16 minutes after sunset and 16 minutes before sunrise' } 
    },

    logging =
    {
        level = domoticz.LOG_INFO,    -- Uncomment this line to override the dzVents global logging setting LOG_DEBUG for debug loggin
        marker = 'SME '.. ScriptVersion,
    },

    data =
    {
        --debugData =           -- You will find this file with debugdata (build in the minutes around 00:00) in <domoticz dir>/scripts/dzVents/data directory
        --{
        --    history = true,
        --    maxItems = 20,
        --},
        dayDeliveredsolar =
        {
            initial = 0,
        },
    },

    execute = function(dz, item)

    --  The following need updated for your environment get the Idx or 'Name' of the Device tab.
        local P1data = 1008   -- Stroom P1 sensor
        local solardata = 1055 -- Solaredge sensor
        local idx_usage = 1030  -- Electra used today
        local idx_delivered = 1031   -- Electra delivered today
        local idx_home_watt = 1104 -- Used at home watt sensor
        local idx_home_kWh = 1035 -- Used at home seperate kWh sensor
        local idx_solar = 1058 -- used solar in house 
        local idx_solar_watt = 1155 -- used solar in house  
        
        local function logWrite(str,level)
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end

        -- Read sensors
        local p1Table = dz.devices(P1data)
        local solarTable = dz.devices(solardata)

        -- Place statistics in variables
        
        -- Read Totals kWh 
        local dayDeliveredsolar = solarTable.counterToday
        local dayDeliveredelectra = p1Table.counterDeliveredToday
        local dayUsedinhouse = p1Table.counterToday

        logWrite("Geleverd zonnepanelen: " .. dayDeliveredsolar)
        -- Read Total Watts 
        local WattDeliveredsolarActual = solarTable.actualWatt
        local WattUsageActual = p1Table.usage
        local WattDeliveredActual = p1Table.usageDelivered
        
        logWrite("Watt solar Actual (WattDeliveredsolarActual): " .. WattDeliveredsolarActual)
        logWrite("Watt p1 Actual usage (WattUsageActual): " .. WattUsageActual)
        logWrite("Watt p1 Actual delivered (WattDeliveredActual): " .. WattDeliveredActual)

        -- store current dayDeliveredsolar
        if dz.time.matchesRule('at 23:30-23:59') then
            dz.data.dayDeliveredsolar = dayDeliveredsolar
        end

        -- check dayDeliveredsolar and set to 0 when required
        if dz.time.matchesRule('at 00:00-09:00') then
            if dz.data.dayDeliveredsolar <= dayDeliveredsolar then
                dayDeliveredsolar = 0
                WattDeliveredsolarActualReset = 0
            end
        end

        --logWrite("Totaal gebruikt: " .. dayUsedinhouse)
        --logWrite("Totaal geleverd aan electra: " .. dayDeliveredelectra)
        --logWrite("Totaal geleverd solar: " .. dayDeliveredsolar)

        -- Update de "vandaag" devices..
        local function updateCountergeneral(idx,value)
            dz.devices(idx).updateCustomSensor(value)
            dz.log("Set " .. dz.devices(idx).name .. " to: ",dz.LOG_DEBUG)
        end
        local function updateCounterkWh(idx,power_value)
            dz.devices(idx).updateElectricity(power_value,0)
            dz.log("Set " .. dz.devices(idx).name .. " to: ",dz.LOG_DEBUG)
        end

        updateCountergeneral(idx_usage,dayUsedinhouse)
        updateCountergeneral(idx_delivered,dayDeliveredelectra)

        -- berekenen wat vandaag in huis is gebruikt
        if (dayDeliveredsolar == 0.0 ) then
            dayTotal = dayUsedinhouse
        else
            -- Calculation of the total value 
            dayTotalsolarusedinhouse = dayDeliveredsolar - dayDeliveredelectra
            dayTotal = dayUsedinhouse + dayTotalsolarusedinhouse
        end
        
        -- berekenen wat vandaag in huis is gebruikt in Watt 
        if (WattDeliveredsolarActualReset == 0.0 ) then
            TotalWatt = WattUsageActual
        else
            -- Calculation of Watt is being used now
            -- Compare if WattDeliveredsolarActual is not lower then WattDeliveredActual, wrong value; timing problem
            if WattDeliveredsolarActual < WattDeliveredActual then
                TotalWatt = WattUsageActual
                logWrite("TotalWatt fout: " .. TotalWatt)
            end
            
            --if compare if WattDeliveredsolarActual is larger thenWattDeliveredActual, OK value
            if WattDeliveredsolarActual >= WattDeliveredActual then
                TotalWattInhouse = WattDeliveredsolarActual - WattDeliveredActual
                logWrite("TotalWattInhouse Solar goed: " .. TotalWattInhouse)
                TotalWatt = TotalWattInhouse + WattUsageActual 
                logWrite("TotalWatt goed: " .. TotalWatt)
            end
        end
        
        -- Place in Sensor
        updateCounterkWh(idx_home_watt,TotalWatt)
        updateCounterkWh(idx_solar_watt,TotalWattInhouse)
        updateCountergeneral(idx_home_kWh,dayTotal)
        updateCountergeneral(idx_solar,dayTotalsolarusedinhouse)

        --if dz.time.matchesRule('at 23:55-00:05') then -- Building history file with debug data
        --    debugData = {}
        --    table.insert(debugData, 'dayTotal = ' .. tostring(dayTotal) )
        --    table.insert(debugData, 'dayDeliveredsolar = '.. tostring(dayDeliveredsolar) )
        --    table.insert(debugData, 'dayDeliveredelectra = '.. tostring(dayDeliveredelectra) )
        --    table.insert(debugData, 'dayTotalsolarusedinhouse = '.. tostring(dayTotalsolarusedinhouse ) )
        --    table.insert(debugData, 'dayUsedinhouse = ' .. tostring(dayUsedinhouse ) )

        --    dz.data.debugData.add(debugData)
        --end
    end
}
Device 1104: is an Electric device which shows the total Watt production, the device itself compute it to kWh
Device 1035: This device takes the absolute powerconsumption and place it as a general custom device

Device 1155 is also an Electric device which shows the totall Watt I used in house, just coming from Solar. Also the device itself compute it to kWh
Device 1058: This device takes the absolute solar usage in house, so looking at the total solar consumption in kWh and put it in a general custom device

Re: Different kWh values

Posted: Tuesday 23 March 2021 23:00
by waaren
CronoS wrote: Monday 22 March 2021 12:05 So hopefully you can read it OK
No problem reading it but in recent builds there is an issue with this type of device. The issue must be solved before something usefull can be done with this script.
It might take some time.

Re: Different kWh values

Posted: Wednesday 24 March 2021 11:57
by CronoS
Awesome thanks, just let me know if there is something I need to test.. ; for now I update the electric device like this: dz.devices(idx).updateElectricity(power_value,0) .. I give it a value of 0 and the device itself compute the KWH..