Page 1 of 1

Strange behaviour persistent data in dzVents in domoticz 3.8153

Posted: Sunday 15 October 2017 20:03
by Bertvdw
When converting my old lua scripts with dzVentz 1.1 i discoverd some strange/incorrect behaviour with the persistent data.
I occurs when i try to save the previous state of a domotics device with an initial value of nil or {}.

Every time the script runs the persistent data is reset to nil instead of only the first time.

I created two testscript:

script 1:

This script run as espected, the value test increases with one every run

-- Start script

Code: Select all

return {
  on = { timer = {'every minute'} 
       },
  data = { test = {initial = 1 }
         },
  execute = function(domoticz, d)
      domoticz.data.test = domoticz.data.test + 1
      domoticz.log(  'TestData1 -- aantal = ' .. domoticz.data.test , LOG_INFO )
   end -- function
}
script 2:

Every run i get the following result.
As you can see, the script starts with the initial data every run for all variables the data segment

2017-10-15 19:41:00.363 dzVents: Info: device nil
2017-10-15 19:41:00.380 dzVents: Info: device RaamSlaapkamer
2017-10-15 19:41:00.380 dzVents: Info: test: 123
2017-10-15 19:41:00.380 dzVents: Info: test: 1

Code: Select all

return {
  on = { timer = {'every minute'} 
       },
  data = { test = {initial = 123 }
         , dev = {initial = nil}
         },
  execute = function(domoticz, d)
    
    if ( not domoticz.data.dev ) then
      domoticz.log(  'device nil', LOG_INFO )
      domoticz.data.dev = domoticz.devices("RaamSlaapkamer")
      domoticz.log(  'device ' .. domoticz.data.dev.name , LOG_INFO )
    else 
      domoticz.log(  'device set', LOG_INFO )
    end
    domoticz.log(  'test: ' .. domoticz.data.test, LOG_INFO ) 
    domoticz.data.test = 1        
    domoticz.log(  'test: ' .. domoticz.data.test, LOG_INFO )
   end -- function
}

What am i doing wrong?

Bert

Re: Strange behaviour persistent data in dzVents in domoticz 3.8153

Posted: Monday 16 October 2017 11:31
by dannybloe
Well, the problem is that you are assigning a device object to your variable and that's something you'd better not do as the object has functions and they cannot be serialized to the persistent store. So better you just put in the variable what you actually need instead of the entire device object.

Re: Strange behaviour persistent data in dzVents in domoticz 3.8153

Posted: Monday 16 October 2017 12:29
by Bertvdw
Thanks for your reaction Danny.
I will try your suggestion.

The reason why i stored the complete device object in the the data variable is because of the example in the dzVents documentation ( in the persistent data chapter).

You can define as many variables as you like and put whatever value in there that you like. It doesn't have to be just a number, you can even put the entire device state in it:
return {
active = true,
on = {
devices = { 'MySwitch' }
},
data = {
previousState = {initial=nil}
},
execute = function(domoticz, switchDevice)
-- set the previousState:
domoticz.data.previousState = switchDevice

-- read something from the previousState:
if (domoticz.data.previousState.temperature > .... ) then
end
end
}


Bert

Re: Strange behaviour persistent data in dzVents in domoticz 3.8153

Posted: Monday 16 October 2017 12:42
by dannybloe
Yep.. indeed.. that wasn't good advice, now was it? ;-)

Re: Strange behaviour persistent data in dzVents in domoticz 3.8153

Posted: Monday 16 October 2017 13:23
by Bertvdw
I'll try tonight.
I'll let you know if it's working.