Page 1 of 1

Selecting Persistent data variables based on a number (SOLVED)

Posted: Wednesday 26 August 2020 17:03
by wizzard72
For example I have 4 persistent data variables:

Code: Select all

data = {
		myVar1 = { initial = 0,history = true, maxItems = 20 },
		myVar2 = { initial = 0,history = true, maxItems = 20 },
		myVar3 = { initial = 0,history = true, maxItems = 20 },
		myVar4 = { initial = 0,history = true, maxItems = 20 },
	},
I want to select one of the persistent data variables based of the last digit.
With domoticz.devices() this is possible, for example:

Code: Select all

deviceStartName = "Light"
for i = 1, 4, 1 do
	domoticz.devices(deviceStartName .. i).dimTo(25)
end
How to do something similair with persistent data variables? If loopting through the persistent data variables names is possible, this is also a solution for me.

Re: Selecting Persistent data variables based on a number

Posted: Wednesday 26 August 2020 17:10
by wizzard72
Normal code to add data to the persistent data variable:

Code: Select all

domoticz.data.myVar1.add(sensor.data)

If the persistent data variable is used like this it's solved my problem:

Code: Select all

domoticz.data('myVar1').add(sensor.data)

Re: Selecting Persistent data variables based on a number

Posted: Wednesday 26 August 2020 17:54
by waaren
wizzard72 wrote: Wednesday 26 August 2020 17:03 How to do something similar with persistent data variables? If looping through the persistent data variables names is possible, this is also a solution for me.
Please note that initial = has no meaning for historic persistent data and is therefore ignored.

As most other objects in dzVents, persistent historic data is a Lua table. Lua table with a string type key can be accessed via two different styles (the one without brackets is so called " Syntax sugar")

dz.data.myVar1.data = dz.data.myVar1['data'] = dz.data['myVar1'].data = dz.data{'myvar' .. 1][data] -- etc..

Hope below example helps understanding

Code: Select all

return
{
    on =
    {
        timer =
        {
            'every minute',
        },
    },

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

    data =
    {
        myVar1 = { history = true, maxItems = 4 },
        myVar2 = { history = true, maxItems = 3 },
        myVar3 = { history = true, maxItems = 2 },
        myVar4 = { history = true, maxItems = 5 },
    },

    execute = function(dz, item)

        for i = 1, 4 do
            dz.data[ 'myVar'  .. i ].add( i * dz.time.minutes )
        end

        dz.log('Raw Date Time of first  record in myVar4: ' .. dz.data.myVar3.getOldest().time.raw,dz.LOG_DEBUG) -- direct access via

        for i = 4, 1, -1 do
            dz.log(dz.data[ 'myVar' .. i ]['getOldest']().data,dz.LOG_DEBUG)
        end

    end
}

Re: Selecting Persistent data variables based on a number

Posted: Wednesday 26 August 2020 19:47
by wizzard72
Hi @waaren, thank you so much!!! I was struggeling with this for some time.

Learned something today :-)