Removing a value from an array  [Solved]

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

Moderator: leecollings

Post Reply
Gerwazy
Posts: 3
Joined: Monday 22 October 2018 22:56
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Poland
Contact:

Removing a value from an array

Post 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.
Last edited by Gerwazy on Saturday 07 September 2019 12:26, edited 3 times in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Removing a value from an array  [Solved]

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Gerwazy
Posts: 3
Joined: Monday 22 October 2018 22:56
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Poland
Contact:

Re: Removing a value from an array

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

Who is online

Users browsing this forum: No registered users and 1 guest