Page 1 of 1

controlling multiple devices at once

Posted: Sunday 26 July 2020 13:25
by markjgabb
hey all im interested in controlling multiple devices at once... i tried the below code, but only the first device is controlled
any tips on why one is working but the other is not?
i know its something wrong with the local HallLight.... but im not sure why it doesnt apply correctly

Code: Select all


return {
	on = {
		devices = {['Loungeroom motion sensor'] =  {'at nighttime'}}
		
	},
	execute = function(dz, item, device)
	    local moodLight = dz.devices('Mood Light')
	    local HallLight = dz.devices(10,11)
        local delay = 120
	--	dz.log('Device ' .. device.name .. ' was changed', dz.LOG_INFO)
		if item.state == 'On' then
		    if item.active then
            if dz.time.matchesRule('between 23:00 and sunrise') then
                moodLight.cancelQueuedCommands()
                moodLight.dimTo(15)
                moodLight.switchOff().afterSec(delay)
                HallLight.cancelQueuedCommands()
                HallLight.dimTo(15)
                HallLight.switchOff().afterSec(delay)
            else
                moodLight.dimTo(100)
                moodLight.switchOff().afterSec(delay)
                HallLight.dimTo(100)
                HallLight.switchOff().afterSec(delay)
            end
    end
end
end
    
}

Re: controlling multiple devices at once

Posted: Sunday 26 July 2020 14:25
by hestia
local HallLight = dz.devices(10,11)
10,11 ?!?
maybe 10 or 11, not both...

Re: controlling multiple devices at once

Posted: Sunday 26 July 2020 14:38
by waaren
markjgabb wrote: Sunday 26 July 2020 13:25 I know its something wrong with the local HallLight.... but im not sure why it doesnt apply correctly

Code: Select all

	    local HallLight = dz.devices(10,11)
You cannot assign two devices to one variable using above syntax.

Can you try this ?

Code: Select all

return
{
    on =
    {
        devices =
        {
            ['Loungeroom motion sensor'] =
            {
                'at nighttime',
            },
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when al OK
        marker = 'Loungeroom motion',
    },

    execute = function(dz, item)

        if item.active then
             local allLights =   {
                                dz.devices('Mood Light'),
                                dz.devices(10),
                                dz.devices(11),
                            }
            local delay = 120

            dimLevel = dz.time.matchesRule('between 23:00 and sunrise') and 15 or 100
            for _, dv in ipairs(allLights) do
                dv.cancelQueuedCommands()
                dv.dimTo(dimLevel)
                dv.dimTo(0).afterSec(delay)
            end
        end
    end
}

Re: controlling multiple devices at once

Posted: Tuesday 28 July 2020 12:37
by markjgabb
hey that works... makes script alot smaller too thank you