Page 1 of 1

.compare and dz.data issue [solved]

Posted: Wednesday 27 January 2021 8:58
by hestia
Hello,
I use .compare to compare 2 times T1 and T2
In one direction it's ok: T1.compare(T2)
In the other it's KO: T2.compare(T1)
when I save T2 in dz.data

If T2 = dz.time directly (or first run of the script), it's ok in both directions :?

Code: Select all

local DEVICES =  {203}

return {
    logging =   {
        level   =   
                domoticz.LOG_DEBUG,
	            },
    on =        {
                devices = DEVICES,
  	            },
    data =      {
        		DEOLtime = {},
        	    },
        	
        execute = function(dz, the_device, triggerInfo)

        local LOG_LEVEL = dz.LOG_DEBUG

    	local l_DEOLtime
    	l_DEOLtime = dz.data.DEOLtime

    	if l_DEOLtime == nil then
		    l_DEOLtime = dz.time
		    print ('l_DEOLtime == nil')
	    else
		    print ('l_DEOLtime not nil')        
	    end
	    print ('l_DEOLtime= ' .. l_DEOLtime.raw)	

        l_DEOLtimeTmp = dz.time.addSeconds(120)
        print ('l_DEOLtimeTmp ' .. l_DEOLtimeTmp.raw)

        local l_DEOLComp
        l_DEOLComp = l_DEOLtimeTmp.compare(l_DEOLtime).compare
        print ('l_DEOLComp ' .. l_DEOLComp)
        
        l_DEOLComp = l_DEOLtime.compare(l_DEOLtimeTmp).compare
        print ('l_DEOLComp ' .. l_DEOLComp)

        dz.data.DEOLtime = l_DEOLtime

end}

Code: Select all

021-01-27 08:47:54.916 Status: dzVents: Info: ------ Start internal script: zTimeCompareData2: Device: "test inter 203 (Dummy)", Index: 203
2021-01-27 08:47:54.917 Status: dzVents: l_DEOLtime == nil
2021-01-27 08:47:54.917 Status: dzVents: l_DEOLtime= 2021-1-27 8:47:54.743
2021-01-27 08:47:54.918 Status: dzVents: l_DEOLtimeTmp 2021-01-27 08:49:54
2021-01-27 08:47:54.918 Status: dzVents: l_DEOLComp -1
2021-01-27 08:47:54.918 Status: dzVents: l_DEOLComp 1
2021-01-27 08:47:54.921 Status: dzVents: Info: ------ Finished zTimeCompareData2
2021-01-27 08:48:00.851 Status: EventSystem: Script event triggered: /home/pi/PROD_domoticz/dzVents/runtime/dzVents.lua

2021-01-27 08:48:07.891 Status: dzVents: Info: ------ Start internal script: zTimeCompareData2: Device: "test inter 203 (Dummy)", Index: 203
2021-01-27 08:48:07.892 Status: dzVents: l_DEOLtime not nil
2021-01-27 08:48:07.892 Status: dzVents: l_DEOLtime= 2021-1-27 8:47:54.743
2021-01-27 08:48:07.893 Status: dzVents: l_DEOLtimeTmp 2021-01-27 08:50:07
2021-01-27 08:48:07.893 Status: dzVents: l_DEOLComp -1
2021-01-27 08:48:07.893 Status: dzVents: Info: ------ Finished zTimeCompareData2
2021-01-27 08:48:07.893 Error: dzVents: Error: (3.1.0) An error occurred when calling event handler zTimeCompareData2
2021-01-27 08:48:07.893 Error: dzVents: Error: (3.1.0) .../scripts/dzVents/generated_scripts/zTimeCompareData2.lua:37: attempt to call a nil value (field 'compare')
Version: 2020.2 (build 12819)
Compile Date: 2021-01-04 08:00:57
dzVents Version: 3.1.0

I should miss something, but what?

Re: .compare and dz.data issue

Posted: Wednesday 27 January 2021 9:38
by waaren
hestia wrote: Wednesday 27 January 2021 8:58 I should miss something, but what?
The first execution of the script you use a complete dzVents time object. This object consists of all time attributes and the functions to access and modify them. When you store an object in persistent data only the attributes are stored; functions are not stored.

So when you try to use the remainder of the time object from persistent data in the second and subsequent executions, the function you try to use (compare) is not in the object.

Luckily the solution is simple. You don't need to store the complete time object but storing the raw attribute is enough to be able to recreate the same complete time object in a next run by using the dz.time.makeTime() function.

see below

Code: Select all

local DEVICES =  {203}

return
{
    on =
    {
        devices = DEVICES,
      },

    data =
    {
        DEOLtime =
        {
            initial = '',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'timeCompare',
    },

    execute = function(dz)

        if dz.data.DEOLtime ~= '' then
            dz.log('l_DEOLtime not nil',dz.LOG_DEBUG)
        else
            dz.log('l_DEOLtime == nil',dz.LOG_DEBUG)
        end

        local l_DEOLtime = ( dz.data.DEOLtime and dz.time.makeTime(dz.data.DEOLtime) ) or dz.time -- if nil then use dz.time else recreate the object
        dz.log('l_DEOLtime= ' .. l_DEOLtime.raw , dz.LOG_DEBUG)

        l_DEOLtimeTmp = dz.time.addSeconds(120)
        dz.log ('l_DEOLtimeTmp ' .. l_DEOLtimeTmp.raw, dz.LOG_DEBUG)

        local l_DEOLComp
        l_DEOLComp = l_DEOLtimeTmp.compare(l_DEOLtime).compare
        dz.log ('l_DEOLComp ' .. l_DEOLComp, dz.LOG_DEBUG)

        l_DEOLComp = l_DEOLtime.compare(l_DEOLtimeTmp).compare
        dz.log ('l_DEOLComp ' .. l_DEOLComp, dz.LOG_DEBUG)

        dz.data.DEOLtime = l_DEOLtime.raw
    end
}

Re: .compare and dz.data issue

Posted: Wednesday 27 January 2021 10:17
by hestia
Thanks waaren for you quick reply
solved for the script :-)

But what I don't understand is why it works in one direction and not in the other direction?
The time inside the parenthesis is not necessary a time object?
T1.compare(T2)
T1 should be a time object
T2 could be an object or a what ? (attributes of an object only?)

Object oriented programming is hard for me as I learnt programming with FORTRAN 4 77 ;-)

Re: .compare and dz.data issue

Posted: Wednesday 27 January 2021 10:28
by waaren
hestia wrote: Wednesday 27 January 2021 10:17 But what I don't understand is why it works in one direction and not in the other direction?
l_DEOLtimeTmp = dz.time.addSeconds(120)

Here you created a complete time object l_DEOLtimeTmp with all functions including the compare function. That is why

l_DEOLComp = l_DEOLtimeTmp.compare(l_DEOLtime).compare

does work

The other way around
l_DEOLComp = l_DEOLtime.compare(l_DEOLtimeTmp).compare
cannot work because l_DEOLtime does not have the compare function (nor any other function) if it is restored from persistent data. It has only values (attributes)

Re: .compare and dz.data issue [solved]

Posted: Wednesday 27 January 2021 12:20
by hestia
Thanks waaren for the explanation :-)