Occasional huge outliers in virtual energy meter's cumulative counter value

Moderator: leecollings

Post Reply
JanCeuleers
Posts: 23
Joined: Saturday 16 January 2021 16:07
Target OS: Linux
Domoticz version:
Contact:

Occasional huge outliers in virtual energy meter's cumulative counter value

Post by JanCeuleers »

I have created a few virtual meters, one of which is used to track how much of my PV production is being immediately consumed rather than being returned onto the grid. The virtual meters are being fed from a LUA script (below). My issue is that occasionally the virtual meter's log shows huge values being logged for the energy component of the virtual meter, which are almost immediately compensated for a large negative log entry.

What might the cause be?
Own Consumption graph
Own Consumption graph
OwnConsumptionGraph.png (61.47 KiB) Viewed 308 times
Here is the script. The graph corresponds with ownconsmeter (id 123).

Code: Select all

function timeOffset( sDate_ )
	local t1 = os.time()
	local t2 = parseDate( sDate_ )
	return os.difftime( t1, t2 )
end

function parseDate( sDate_ )
	local sYear = string.sub( sDate_, 1, 4 )
	local sMonth = string.sub( sDate_, 6, 7 )
	local sDay = string.sub( sDate_, 9, 10 )
	local sHour = string.sub( sDate_, 12, 13 )
	local sMinutes = string.sub( sDate_, 15, 16 )
	local sSeconds = string.sub( sDate_, 18, 19 )
	return os.time{ year=sYear, month=sMonth, day=sDay, hour=sHour, min=sMinutes, sec=sSeconds }
end

commandArray = {}
debug = false

smartmeter = "Grid 3P energy"
solarmeter = "Solar energy"
dummymeter = "Consumption"
dummymeter_id = 20
ownconsmeter = "Own consumption"
ownconsmeter_id = 123
dummymeter_trigger = smartmeter
ownconspercent_id = 124

for deviceName,deviceValue in pairs(devicechanged) do
    if (deviceName == solarmeter or deviceName == smartmeter) then
        smart_p, smart_e = otherdevices_svalues[smartmeter]:match("([^;]+);([^;]+)")
        solar_p, solar_e = otherdevices_svalues[solarmeter]:match("([^;]+);([^;]+)")

        consumption_p = tonumber(solar_p) - tonumber(smart_p)
        consumption_e = solar_e - smart_e

        if (tonumber(smart_p) >= 0) then  -- if we're currently supplying the grid
            own_p = tonumber(solar_p) - tonumber(smart_p)
            own_e = tonumber(solar_e) - tonumber(smart_e)
        else
            own_p = tonumber(solar_p)
            own_e = tonumber(solar_e)
        end

        if (tonumber(solar_p) == 0) then
            ownpercent=100
        else
            ownpercent=100 * tonumber(own_p) / tonumber(solar_p)
        end

        commandArray[1] = {['UpdateDevice'] = dummymeter_id .. "|0|" .. consumption_p} -- .. ";" .. consumption_e}

        if (timeOffset(otherdevices_lastupdate[solarmeter]) < 120) then    -- only trust the solar meter if recently updated
            commandArray[2] = {['UpdateDevice'] = ownconsmeter_id .. "|0|".. own_p .. ";" .. own_e}
            commandArray[3] = {['UpdateDevice'] = ownconspercent_id .. "|0|" .. ownpercent}
        end
    end
end

return commandArray
JanCeuleers
Posts: 23
Joined: Saturday 16 January 2021 16:07
Target OS: Linux
Domoticz version:
Contact:

Re: Occasional huge outliers in virtual energy meter's cumulative counter value

Post by JanCeuleers »

Although I'd still be interested in the answer to the question, I now realise that my approach was incorrect.

My error (although it can't have caused the symptoms reported) is that an energy meter needs to be fed an ever-increasing counter value. That is: whereas the power component of an energy meter (the first value in the returned commandArray) is of course an instantaneous value expressed in Watt, the energy component (the 2nd commandArray value) needs to be the total amount of energy consumed/generated since the beginning of time, expressed in Wh.

So an "own consumption" virtual meter, like the one I was asking the question about, needs to do its own integration: each time the script is run it needs to check what energy value was returned last time, and return as the new energy value the old-one plus the current power (wattage) times the amount of time elapsed since the last update, in hours.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests