Selecting Persistent data variables based on a number (SOLVED)

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

Moderator: leecollings

Post Reply
wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Selecting Persistent data variables based on a number (SOLVED)

Post 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.
Last edited by wizzard72 on Wednesday 26 August 2020 19:48, edited 1 time in total.
wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Selecting Persistent data variables based on a number

Post 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)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Selecting Persistent data variables based on a number

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Selecting Persistent data variables based on a number

Post by wizzard72 »

Hi @waaren, thank you so much!!! I was struggeling with this for some time.

Learned something today :-)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest