Cannot activate persistent data

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

Moderator: leecollings

Post Reply
gurzixo
Posts: 5
Joined: Wednesday 10 August 2016 1:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Cannot activate persistent data

Post by gurzixo »

Hello,

I am struggling for 2 days to activate persistent data.

I run Domoticz on a Raspberry pi, and have 2 zsticks reading house and solar panel consumption. I want to create a composite meter showing how much electricity I use from the utility; the problem is that sometime I create more electricity that I use, but unfortunately my meter is not electromechanical, and so cannot turn backwards anymore ;-) so I need to store the amount of electricity I send back to the grid.
I have - more or less - understood lua and dzVents, and everything seems to work except for storing this amount between invocations.

In order to pinpoint the problem, I wrote the following minimal dzVent script:

Code: Select all

return {
    active = true,
    on = {
        ['timer'] = {
            'every minute'
        }
    },
    data = {
                mydata = { initial = 10 },
    },
    execute = function(domoticz)
        print("Old mydata: ")
        print(mydata)
        mydata = 20
        print("New mydata: "..mydata)
    end
}
Which should print 10 at the first run, then 20.
But the variable mydata is always nil, as shown in this logfile: https://pastebin.com/MrTwvvhG

I cleared everything before starting the log.
The first script invocation occurs at line 82
The second invocation occurs at line 159
In both cases, Old mydata is nil

Just in case, the content of domoticzData.lua is available here: https://pastebin.com/iB6qa6vh

What did I do wrong?

Thanks!
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Cannot activate persistent data

Post by dannybloe »

You should do: print(domoticz.data.mydata). Look carefully at the examples. Good luck.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
gurzixo
Posts: 5
Joined: Wednesday 10 August 2016 1:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Cannot activate persistent data

Post by gurzixo »

Thanks dannybloe for your quick answer and your kind words.

I modified my script as follows:

Code: Select all

return {
    active = true,
    on = {
        ['timer'] = {
            'every minute'
        }
    },
    data = {
                mydata = { initial = 10 },
    },
    execute = function(domoticz)
        print("Old mydata: ")
        print(mydata)
        print("domoticz.data.mydata:")
        print(domoticz.data.mydata)
        mydata = 20
        print("New mydata: "..mydata)
    end
}
and got the following:
2018-09-17 19:17:00.099 Status: EventSystem: reset all events...
2018-09-17 19:17:00.104 Status: dzVents: Write file: /home/pi/domoticz/scripts/dzVents/generated_scripts/mydata.lua
2018-09-17 19:17:00.144 Status: Incoming connection from: 192.168.32.231
2018-09-17 19:17:00.660 Status: dzVents: Old mydata:
2018-09-17 19:17:00.662 Status: dzVents: domoticz.data.mydata:
2018-09-17 19:17:00.664 Status: dzVents: 10
2018-09-17 19:17:00.665 Status: dzVents: New mydata: 20
2018-09-17 19:18:00.688 Status: dzVents: Old mydata:
2018-09-17 19:18:00.690 Status: dzVents: domoticz.data.mydata:
2018-09-17 19:18:00.692 Status: dzVents: 10
2018-09-17 19:18:00.694 Status: dzVents: New mydata: 20
So it looks like the persistent data is created when I save the script, but not loaded before script invocation nor saved after...

FYI, I have a file "domoticz/scripts/dzVents/data/__data_mydata.lua" which contains:
-- Persistent Data
local multiRefObjects = {

} -- multiRefObjects
local obj1 = {
["mydata"] = 10;
}
return obj1
Any ideas?
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Cannot activate persistent data

Post by dannybloe »

Take a close look at the examples please. Follow the right syntax: domoticz.data.mydata = 20
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
gurzixo
Posts: 5
Joined: Wednesday 10 August 2016 1:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Cannot activate persistent data

Post by gurzixo »

Thanks dannybloe!

Forty years of development, half a million LOC (C, C++ but novice in lua) and still BS in the eyes! you made my day.
You could have added a smiley in your first response as a hint :-)
I was starting to analyse and try to understand self.callEventHandler() ... Nice piece of code!

In the meantime I found here https://stackoverflow.com/questions/916 ... to-console a little gem, very useful in debug:

Code: Select all

function dump(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. dump(v) .. ','
      end
      return s .. '} '
   else
      return tostring(o)
   end
end
Maybe you could make it available for debugging scripts?

Thanx again!
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Cannot activate persistent data

Post by dannybloe »

Well, glad it works and thanks for the compliment :-)
There is a dumpTable function in the code but it is not put on the domoticz object for no obvious reason. I could add it.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests