Page 1 of 1

looping through data AND doing a lookup

Posted: Sunday 30 June 2019 17:42
by Gravityz
i have been able to do a lookup based on a trigger.

now i want to loop through all these items everytime instead of doing a trigger lookup.

can anybody point me in the right direction because i can not comprehent the foreach stuff

Code: Select all

local lookup = {
    Bamboelampklok = "100" ,
    Dressoirlamp = "101" ,
    Dressoirlamptoog = "102" ,
    LampachterTV = "103",
    Bamboelamphoek = "104" ,
    Plafondspots = "105" ,
    Vitrinekast = "106" ,
    Tuinlampen = "107" ,
    Tuinsproeier = "108" ,
    Woonkamerlampen = "109" ,
    WoonkamerlampenSfeervol = "114" ,
    SceneNormaal = "110" ,
    SceneRyan = "111" ,
    SceneSfeervol = "113" ,
    SceneLezen = "112" ,

}

    execute = function(domoticz, trigger)
     local habridgeid = lookup[trigger.name]
so basically i want a variable to hold the name of the device
second variable to hold the id of that device (the number behind the name)
and that part looping until i have gotten through all the devices specified under lookup.

can this be done with one lookup table or do i need 2 seperate tables

Re: looping through data AND doing a lookup

Posted: Sunday 30 June 2019 18:23
by waaren
Gravityz wrote: Sunday 30 June 2019 17:42 now i want to loop through all these items everytime instead of doing a trigger lookup.
can this be done with one lookup table or do i need 2 seperate tables
Not sure if I understand exactly what you try to achieve but have a look at this to see if that helps.

Code: Select all

return 
{
    on = { devices = {'trigger'}},
    
    execute = function(dz, item)
        local lookup = 
        {
            Bamboelampklok = "100" ,
            Dressoirlamp = "101" ,
            Dressoirlamptoog = "102" ,
            LampachterTV = "103",
            Bamboelamphoek = "104" ,
            Plafondspots = "105" ,
            Vitrinekast = "106" ,
            Tuinlampen = "107" ,
            Tuinsproeier = "108" ,
            Woonkamerlampen = "109" ,
            WoonkamerlampenSfeervol = "114" ,
            SceneNormaal = "110" ,
            SceneRyan = "111" ,
            SceneSfeervol = "113" ,
            SceneLezen = "112" ,
        }
        
        local rpad = function(str, len, char)
            if char == nil then char = ' ' end
            return string.rep(char, len - #str) .. str
        end
        
        local lpad = function(str, len, char)
            if char == nil then char = ' ' end
            return str .. string.rep(char, len - #str)
        end

        for name, id in pairs(lookup) do
            dz.log('device name: ' .. lpad(name,27) .. '; ID ==>> ' .. id )
        end

    end
}

Re: looping through data AND doing a lookup

Posted: Sunday 30 June 2019 18:53
by Gravityz
wow, that looks very promising.

i can however not use the lpad(name,27) as a device name

i tried dz.devices((lpad(name,27)). state but that did not work

also
local device = (lpad(name,27) and then
local state = dz.devices(device) or
local state = device.state

does not work

Re: looping through data AND doing a lookup

Posted: Sunday 30 June 2019 19:05
by waaren
Gravityz wrote: Sunday 30 June 2019 18:53 i can however not use the lpad(name,27) as a device name
i tried dz.devices((lpad(name,27)). state but that did not work
Try

Code: Select all

dz.devices(name)
the lpad() and rpad() functions are only there to make the lay-out easier to read by padding the name with spaces so all names have the same width on the screen.

Re: looping through data AND doing a lookup  [Solved]

Posted: Sunday 30 June 2019 19:18
by Gravityz
damned, thought i tried that one but noticed i did it with the lpad, not the name only.

working perfectly now

the other pitfall i discovered i was mixing devices with scenes/groups and not al commands work correctly then

the previous script was working perfectly also but i noticed that when turning on a scene, multiple lights go on and the scripts gets flooded(you can not capture all the lights triggering the script)

when the new script gets triggered it just updates all the light/scenes, triggered or not.

a lot of work just to fix functionality which should be implemented in habridge in the first place


anyway, truly thanks, you probably think and dream dzvents