problem with forEach function

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

Moderator: leecollings

Post Reply
Kerel
Posts: 28
Joined: Friday 02 December 2016 8:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

problem with forEach function

Post 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?
Kerel
Posts: 28
Joined: Friday 02 December 2016 8:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: problem with forEach function

Post 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.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: problem with forEach function

Post 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)
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest