Page 1 of 1

Removing a value from an array

Posted: Saturday 07 September 2019 10:41
by Gerwazy
Hi,
I going to create a script handling with lights downstairs. I created an array with devices and want to add or remove a device depending on its state.
As far I understood, to add or remove a value from an array I have to use LUA (?)
My script (especially 2nd line)

Code: Select all

local lights = domoticz.devices().filter({ 'LedRelaxSides', 'LedRelaxMiddle', 'LightRelaxLibrary', 'LightRelaxWall'})
            local lights2 = domoticz.utils._.pull(lights, 'LedRelaxSides')
            lights2.forEach(function(light)
                    domoticz.log(" TEST " .. light.name)
                
            end)
But it shows 4 devices (all) and I expected only 3 (without LedRelaxSides)

What do I wrong?
Thank you in advance.

Re: Removing a value from an array  [Solved]

Posted: Saturday 07 September 2019 11:20
by waaren
Gerwazy wrote: Saturday 07 September 2019 10:41 I going to create a script handling with lights downstairs. I created an array with devices and want to add or remove a device depending on its state.
What do I wrong?
The filter returns devices into the table lights and you try to remove a string (name).

If you change it to

Code: Select all

local lights = domoticz.devices().filter({ 'LedRelaxSides', 'LedRelaxMiddle', 'LightRelaxLibrary', 'LightRelaxWall'})
            local lights2 = domoticz.utils._.pull(lights, domoticz.devices('LedRelaxSides'))
            lights2.forEach(function(light)
                    domoticz.log(" TEST " .. light.name)                
            end)
You will see the expected result.
Please note that using this method you will remove the device LedRelaxSides from both lights and lights2.

Re: Removing a value from an array

Posted: Saturday 07 September 2019 11:37
by Gerwazy
Corrected according to your advice and works as expected.

Thank you waaren!

Code: Select all

local lights = domoticz.devices().filter({ 'LedRelaxSides', 'LedRelaxMiddle', 'LightRelaxLibrary', 'LightRelaxWall'})
domoticz.utils._.pull(lights, domoticz.devices('LedRelaxSides'))
lights.forEach(function(light)
   domoticz.log(" TEST " .. light.name)
end)