Page 1 of 1

problem with forEach function

Posted: Sunday 19 November 2017 10:38
by Kerel
This code is not working properly for me:

Code: Select all

return {
	on = {
		devices = {
            'All lights'
		},

		timer = {
		}
	},
	execute = function(domoticz, device, triggerInfo)
		local lightsDownstairs = domoticz.devices().filter(function(device)
                return  (device.name == 
                                (
                                        'Sofa light' or
                                        'Dining room light' or
                                        'TV light' or
                                        'Hallway light' or
                                        'Kitchen light'
                                )
                        )
                end
        )

		if triggerInfo.type == 'device'
		then
		        print('trigger = device')
		        print('devicename = ' .. device.name)
		        if device.name == 'All lights'
		        then
		                print('Device state: ' .. device.state)		                
		                if device.state == 'On'
		                then
                                lightsDownstairs.forEach(function(light)
                                        print('Do something with ' .. light.name)
                                        light.switchOn()
                                end
                                )
                            
                        else
                                lightsDownstairs.forEach(function(light)
                                        print('Do something with ' .. light.name)
                                        light.switchOff()
                                end
                                )
	                    end
	                    
	                    
	            end
	            
	    end
	    
	end
}
It seems it runs the lightsDownstairs.forEach(function(light) loop only for the first device in the array (which is 'Sofa light'). But I want it to run through all the devices in the array..

My log says:

Code: Select all

2017-11-19 10:38:04.813 User: Admin initiated a switch command (99/All lights/On)
2017-11-19 10:38:04.881 dzVents: Info: Handling events for: "All lights", value: "On"
2017-11-19 10:38:04.881 dzVents: Info: ------ Start internal script: Light switch events: Device: "All lights (Virtual switches controller)", Index: 99
2017-11-19 10:38:04.909 dzVents: trigger = device
2017-11-19 10:38:04.909 dzVents: devicename = All lights
2017-11-19 10:38:04.909 dzVents: Device state: On
2017-11-19 10:38:04.909 dzVents: Do something with Sofa light
2017-11-19 10:38:04.909 dzVents: Info: ------ Finished Light switch events
Any thoughts?

Re: problem with forEach function

Posted: Sunday 19 November 2017 11:12
by Kerel
It turns out, this does seem to work:

Code: Select all

		local lightsDownstairs = domoticz.devices().filter(function(device)
                -- return  (device.name == 
                --                 (
                --                         'Sofa light' and
                --                         'Dining room light' and
                --                         'TV light' and
                --                         'Hallway light' and
                --                         'Kitchen light'
                --                 )
                --         )
                return (device.name == 'Sofa light' or 
                        device.name == 'Dining room light' or
                        device.name == 'TV light' or
                        device.name == 'DHallway light' or
                        device.name == 'Kitchen light')
                end
        )
But I'm not really fond of this way of coding.

Re: problem with forEach function

Posted: Monday 20 November 2017 8:38
by dannybloe
You can make a table and use that in your filter.

Code: Select all

	local lights = { 'Sofa light', 'Dining room light' }
	local _ = require('lodash')
	local lightsDownstairs = domoticz.devices().filter(function(device)
		return _.includes(lights, device.name)
	end)