Page 1 of 1

DZventz access to (global) persistant data

Posted: Thursday 10 October 2019 21:43
by ceesgeert
I have tried to use (global) persistant data

But I am not able to access the data in a Dzvents script function. What I want to achieve is to set the ambiance for a set of hue spots based on a choice from 1-5 triggered by an event (scene selection). In order to do that I need an array of presets for red, green,blue and the corresponding dimlevel.

From the array I select the appropriate set (say index 4) and push that to a command to set the hue light. That works with hard code.

Now I have tried almost everything I know. A predefined array as in the example, but also with a single number, a history of 5 values, in the global data, also in local data. But noting I try seems to work. I need some guidance.
code example hue.txt
(1.13 KiB) Downloaded 84 times

Re: DZventz access to (global) persistant data

Posted: Thursday 10 October 2019 21:53
by waaren
ceesgeert wrote: Thursday 10 October 2019 21:43 Now I have tried almost everything I know. A predefined array as in the example, but also with a single number, a history of 5 values, in the global data, also in local data. But noting I try seems to work. I need some guidance.
If you try to acces data from the globaldata you should use domoticz.globalData.name
even when you access the data from within global_data.lua

Re: DZventz access to (global) persistant data

Posted: Thursday 10 October 2019 21:58
by ceesgeert
I know. I have read all that multiple times. But it seems I am blind to the solution. And I do have programming skills.

Re: DZventz access to (global) persistant data

Posted: Thursday 10 October 2019 22:40
by ceesgeert
I had tried and have tried again the use of domoticz.globalData.name. I have also set a fixed index to be sure. Same result.
r = domoticz.globalData.ambiance_R[4]
g = domoticz.globalData.ambiance_G[4]
b = domoticz.globalData.ambiance_B[4]
d = domoticz.globalData.ambiance_dimlevel[4]

Re: DZventz access to (global) persistant data

Posted: Thursday 10 October 2019 23:23
by waaren
ceesgeert wrote: Thursday 10 October 2019 22:40 I had tried and have tried again the use of domoticz.globalData.name. I have also set a fixed index to be sure. Same result.
Try this set of global_data.lua and test script.

Code: Select all

-- global_data.lua

return
{
    data = -- Because these tables are nowhere else set, they need to be initialized
    {
        ambiance_R = { initial = { 255, 255, 224, 255, 255 }},  -- RED
        ambiance_G = { initial = { 189, 255, 224, 182,  84 }},  -- GREEN
        ambiance_B = { initial = { 127,  74, 255, 114,  49 }},  -- BLUE

        ambiance_dimlevel = { initial = { 100,  57, 100,  31,  31}},         -- with respective dimlevel
    },
          
    helpers =
    {
        test_global_hue = function(dz, selected_ambiance)
            local r = dz.globalData.ambiance_R[selected_ambiance]
            local g = dz.globalData.ambiance_G[selected_ambiance]
            local b = dz.globalData.ambiance_G[selected_ambiance]
            local d = dz.globalData.ambiance_dimlevel[selected_ambiance]
           
            dz.log('\nred:     ' .. r ..
                   '\ngreen:    ' .. g ..
                   '\nblue:     ' .. b ..
                   '\ndimlevel: ' .. d ,dz.LOG_FORCE)
       
        --    domoticz.devices("L_H1_Hue_color").setHex(r,g,b)
        end,
    }
}
test script

Code: Select all

return
{
    on = { timer = { 'every minute' }},
   
    logging = { level = domoticz.LOG_DEBUG, marker = 'hue Test' },

    execute = function(dz)
        dz.helpers.test_global_hue(dz,1)
        dz.helpers.test_global_hue(dz,2)
        dz.helpers.test_global_hue(dz,3)
        dz.helpers.test_global_hue(dz,4)
        dz.helpers.test_global_hue(dz,5)
    end
}

Re: DZventz access to (global) persistant data

Posted: Friday 11 October 2019 0:25
by ceesgeert
Thanks Waaren. That was very helpful. This works! I can now start building the rest of my system in an efficient way.

Re: DZventz access to (global) persistant data

Posted: Friday 11 October 2019 10:08
by ceesgeert
Apparently I think it means that the domoticz object referring to global_data can only be accessed via "dz" and not via "domoticz"...

dz.globalData.ambiance_R[selected_ambiance] works
domoticz.globalData.ambiance_R[selected_ambiance] fails

Re: DZventz access to (global) persistant data

Posted: Friday 11 October 2019 10:17
by waaren
ceesgeert wrote:Apparently I think it means that the domoticz object referring to global_data can only be accessed via "dz" and not via "domoticz"...

dz.globalData.ambiance_R[selected_ambiance] works
domoticz.globalData.ambiance_R[selected_ambiance] fails
No it' s just a name. As long as it refers to the domoticz object it's ok. As example you could change dz to Domo in the script and dz to domoticz in global_data if you do this throughout the files.

Re: DZventz access to (global) persistant data

Posted: Friday 11 October 2019 11:42
by boum
Hello Ceesgeert, I won't get into the why it was not working. But I had just a remark about the style. So that personal and you can totally dismiss it, it's your call.

Why are you spliting the components in different tables? Was it because it didn't work when you did it differently or no reason?
I'd do something like that, easier to change later:

Code: Select all

-- global_data.lua

return
{
    data = -- Because these tables are nowhere else set, they need to be initialized
    {
        ambiance = { initial = { 
            { r=255, g=189,  b=127, dim=100, },
            { r=255, g=255,  b=74,  dim=57, },
            { r=224, g=224,  b=255, dim=100, },
            { r=255, g=182,  b=114, dim=31, },
            { r=255, g=84,   b=49,  dim=31, },
        } },
    },
          
    helpers =
    {
        test_global_hue = function(dz, selected_ambiance)
            local ambiance = dz.globalData.ambiance[selected_ambiance]
            local r = ambiance.r
            local g = ambiance.g
            local b = ambiance.b
            local d = ambiance.dim
           
            dz.log('\nred:     ' .. r ..
                   '\ngreen:    ' .. g ..
                   '\nblue:     ' .. b ..
                   '\ndimlevel: ' .. d ,dz.LOG_FORCE)
       
        --    domoticz.devices("L_H1_Hue_color").setHex(r,g,b)
        end,
    }
}

Re: DZventz access to (global) persistant data  [Solved]

Posted: Friday 11 October 2019 13:56
by ceesgeert
Thank you for your tip Boum. Indeed I started with one 2-dimensional table but changed it many times even up to single values. However the problems remained.

Fortunately my problem not getting access to the global data was resolved by Waaren. (Great this forum and thank you for the fast reponses!)

As far as I am concerned this item can be marked as " solved"