looping through data AND doing a lookup  [Solved]

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

Moderator: leecollings

Post Reply
Gravityz
Posts: 652
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2025.1
Location: Netherlands
Contact:

looping through data AND doing a lookup

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

Re: looping through data AND doing a lookup

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Gravityz
Posts: 652
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2025.1
Location: Netherlands
Contact:

Re: looping through data AND doing a lookup

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

Re: looping through data AND doing a lookup

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Gravityz
Posts: 652
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2025.1
Location: Netherlands
Contact:

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

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest