avg() calculation of historical variable wrong  [Solved]

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

Moderator: leecollings

Post Reply
zavjah
Posts: 36
Joined: Tuesday 13 August 2019 10:20
Target OS: Raspberry Pi / ODroid
Domoticz version:

avg() calculation of historical variable wrong

Post by zavjah »

Hi all,

I use the following adapted script to record min and max values of temperature and calculate the avg() of both for the last 72h:

Code: Select all

-- Source: https://www.domoticz.com/forum/viewtopic.php?p=204082#p204082
local httpResponses = 'getMaxTemp'

return {on = {timer           =   {'at 00:00'},                       -- Um Mitternacht die Max Temp der letzten 24h ermitteln
              httpResponses   =   { httpResponses }
              },
    data = {
            tempMaxHist = { history = true, maxItems = 3},
            tempMinHist = { history = true, maxItems = 3},
            },
	logging = {level = domoticz.LOG_INFO,
	           marker = '_var_MaxAvgTemp',
	          },
    execute = function(dz, item)
        local tempDevice = dz.devices('Außen')
        local extremes = {}
        local tempMaxHist = dz.data.tempMaxHist
        local tempMaxAvg
        local tempMinHist = dz.data.tempMaxHist
        local tempMinAvg


        -- Temeraturwerte von device holen
        if item.isDevice or item.isTimer then
            dz.openURL({    url = 'http://192.168.178.23:8080/json.htm?type=graph&sensor=temp&range=day&idx=' .. tempDevice.idx  .. '&range=day',
                            method = "GET",
                            callback = httpResponses })
            return
        end

        -- Max Temperatur ermitteln
        local function getExtremes(rt)
            extremes.high               = {}
            extremes.high.temperature   = -256
            extremes.low                = {}
            extremes.low.temperature    = 10000

            for key in ipairs(rt) do
                if tonumber(rt[key].te) > extremes.high.temperature then
                    extremes.high.temperature = tonumber(rt[key].te)
                end
                if tonumber(rt[key].te) < extremes.low.temperature then
                    --logWrite("New min temperature found on " .. convertTime(rt[key].d) .. " ==>> " .. rt[key].te )
                    extremes.low.temperature = tonumber(rt[key].te)
                    --extremes.low.tTime = rt[key].d
                end
            end
            return extremes
        end

        -- Min/Max Temeratur ausgeben
        if item.ok and item.isJSON then
            extremes = getExtremes(item.json.result)
            dz.devices('MaxTempGestern').updateTemperature(extremes.high.temperature)
            dz.devices('MinTempGestern').updateTemperature(extremes.low.temperature)

        -- Max Temperatur historisieren, durchschnitt der letzten 72h berechnen, ausgeben
            tempMaxHist.add(extremes.high.temperature)
            tempMaxAvg = tempMaxHist.avg()
            dz.devices('TempDurchschnitt72h').updateTemperature(tempMaxAvg)

        -- Min. Temperatur historisieren, durchschnitt der letzten 72h berechnen, ausgeben
            tempMinHist.add(extremes.low.temperature)
            tempMinAvg = tempMinHist.avg()
            dz.devices('TempMinDurchschnitt72h').updateTemperature(tempMinAvg)


        -- Bei Fehler loggen
        else
            --dz.log('Keine valide Antwort von domoticz ',dz.LOG_ERROR)
            --dz.log(item,dz.LOG_ERROR)
        end
    end
}
I've added min temperature recently and realized that the avg() is not calculated properly. I've checked the recorded single values in the virtual devices and they simply do not match.

I've tried to read out what is saved in both historical variables but whatever I do I get the error that I am attempting to call a nil value. I assume i am doing something wrong rather than that there are no values inside the variables because the virtual devices are being fed each day with calculated values, but they are wrong.

I've added the logged values of two virtual devices for max temperature and the avg() max temperature for comparison. Can anyone get around it?

Thank for any help,
zavjah
Attachments
temperature-last-month_TempDurchschnitt72h.csv
(1.13 KiB) Downloaded 41 times
temperature-last-month_MaxTempGestern.csv
(1.08 KiB) Downloaded 31 times
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: avg() calculation of historical variable wrong

Post by waltervl »

No clue what is going wrong, but it is always wise to add some logging to debug your issue.
especcially around the tempMinHist.add(extremes.low.temperature) and tempMaxHist.add(extremes.high.temperature) to see what you get and what you would have expected.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
zavjah
Posts: 36
Joined: Tuesday 13 August 2019 10:20
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: avg() calculation of historical variable wrong

Post by zavjah »

waltervl wrote: Monday 04 April 2022 16:51 No clue what is going wrong, but it is always wise to add some logging to debug your issue.
especcially around the tempMinHist.add(extremes.low.temperature) and tempMaxHist.add(extremes.high.temperature) to see what you get and what you would have expected.
Hello waltervl,

thx for the hint. But what exactly do you mean with "especially arround the tempMinHist.add..."? Is it possible to include only those lines of code into debug logging? I thought it all script (in the logging section) or nothing. How am I suppose to do that?

thx for the help,
Zavjah
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: avg() calculation of historical variable wrong

Post by waltervl »

Add some lines like to show the values in the log:

dz.log('extremes.low.temperature: ' .. extremes.low.temperature ,dz.LOG_INFO)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
zavjah
Posts: 36
Joined: Tuesday 13 August 2019 10:20
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: avg() calculation of historical variable wrong

Post by zavjah »

waltervl wrote: Tuesday 05 April 2022 0:09 Add some lines like to show the values in the log:

dz.log('extremes.low.temperature: ' .. extremes.low.temperature ,dz.LOG_INFO)
Ahh, that's what you meant, got it.

Well, I do that - not via the log - but via a virtual devices that hold the daily temperatures and thus also have a history:

Code: Select all

dz.devices('MaxTempGestern').updateTemperature(extremes.high.temperature)
dz.devices('MinTempGestern').updateTemperature(extremes.low.temperature)
That's how i realized, that something is wrong :-/.

Is there a way to dump the values of my persistent variables from the data section to check, what is stored there:

Code: Select all

data = {
tempMaxHist = { history = true, maxItems = 3},
tempMinHist = { history = true, maxItems = 3},
},
I've tried that but with no success. If i could retrieve this data i could compare them to the data stored in the virtual devices for daily temperatures.

Best regards,
zavjah
zavjah
Posts: 36
Joined: Tuesday 13 August 2019 10:20
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: avg() calculation of historical variable wrong

Post by zavjah »

Hi all,

can anyone tell me how can I dump the values from the persistent variable I've created?

thx
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: avg() calculation of historical variable wrong

Post by waltervl »

zavjah wrote: Sunday 24 April 2022 13:47 Hi all,

can anyone tell me how can I dump the values from the persistent variable I've created?

thx
I suppose you tried all the documented dzvents dump and debug options?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
zavjah
Posts: 36
Joined: Tuesday 13 August 2019 10:20
Target OS: Raspberry Pi / ODroid
Domoticz version:

[SOLVED] Re: avg() calculation of historical variable wrong  [Solved]

Post by zavjah »

waltervl wrote: Monday 25 April 2022 17:43
I suppose you tried all the documented dzvents dump and debug options?
Hello waltervl,

sure, I did. At the end I found out that although I was saving the identical Information into the variables (see here: viewtopic.php?p=288351#p288351) like in the virtual devices, values in the variable were different ones. Why - I do not know :shock:

I solved my need now differently. I am reading the devices with the range=month and calculate the values my self:

Code: Select all

local getTemperatures = 'getTemperatures'

return {on = {timer           =   {'at 00:05'},
              httpResponses   =   { getTemperatures }
              },
	logging = {level = domoticz.LOG_INFO,
	           marker = '_var_TempMinMaxAvg',
	          },
    execute = function(dz, item)
        local tempDevice = dz.devices('Außen')
        local zeitraum = 4 -- tage zurück inkl. heute, deshalb 4, wenn man 3 ermitteln will

    -- Temeraturwerte von device holen
        if item.isDevice or item.isTimer then
            dz.openURL({url = 'http://192.168.178.23:8080/json.htm?type=graph&sensor=temp&idx=' .. tempDevice.idx  .. '&range=month',
                        callback = getTemperatures })
            return
        end

    -- Funktion, um die Werte zu ermitteln/berechnen
        local function getTempValues(rt)
            local startDate = (dz.time.addMinutes(-1 * zeitraum * 24 * 60)).rawDate
            local endDate = dz.time.rawDate
            local yesterday = (dz.time.addMinutes(-24 * 60)).rawDate
            local SumMaxTemp72h = 0
            local SumMinTemp72h = 0
            
            for index, record in ipairs(rt) do
            -- MinAvg und MaxAvg der letzten 72h ermitteln
                if record.d > startDate and record.d < endDate then
                    --dz.log(record)
                    SumMinTemp72h = SumMinTemp72h + record.tm
                    SumMaxTemp72h = SumMaxTemp72h + record.te
                end
                MinTempAvg72h = dz.utils.round(SumMinTemp72h/3,1)
                MaxTempAvg72h = dz.utils.round(SumMaxTemp72h/3,1)
            
            -- Min und Max von gestern ermitteln
                if record.d == yesterday then
                    MaxTemp = record.te
                    MinTemp = record.tm
                end
            end
            
        -- Werte in die Devices schreiben
            dz.devices('MinTempGestern').updateTemperature(MinTemp)
            dz.devices('TempMinDurchschnitt72h').updateTemperature(MinTempAvg72h)
            dz.devices('MaxTempGestern').updateTemperature(MaxTemp)
            dz.devices('TempDurchschnitt72h').updateTemperature(MaxTempAvg72h)
        end
    
    -- Funktion aufrufen und das Array ausm Device übergeben
        temperatures = getTempValues(item.json.result)
    end
}
Maybe this helps someone else, too :-D.

Cheers,
Zavjah
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest