Page 1 of 1

Problem with groups and dzVents  [Solved]

Posted: Saturday 21 December 2019 13:20
by havnegata
First of all I want to thank all the great contributors on this forum! I have gotten a lot help just by looking at the examples, but now I'm a bit stuck. I have TWO Ikea bulbs which I'm trying to turn on simultaneously by using two motion sensors and found that I can turn on ONE of them (710) by using this code (some of you will obviously recognize the code, thanks to you!):

Code: Select all

 return {
        on = {
            devices = {
                'Bevegelse_trapp2etasje', 'Bevegelse_Fibaro'}
                
        },
         
    execute = function(dz, item)
            local LuxDevice = dz.devices(695)                 
            local Lux = LuxDevice.lux
            local Lights = dz.devices(710)
            
            if Lux < 12 and item.state == "On" then
            Lights.cancelQueuedCommands()
            
		if dz.time.matchesRule("at 23:01-06:00") then
			Lights.dimTo(10)
		
		else
			Lights.dimTo(50)
			end
        elseif item.state == "Off" and Lights.state == "On" then 
            Lights.switchOff().afterSec(90)
        
        end
    end
}
But as I stated in the beginning I want to turn BOTH bulbs on simultaneously, so then I put them in a group and called it "Lys2etasje".

Code: Select all

 return {
        on = {
            devices = {
                'Bevegelse_trapp2etasje', 'Bevegelse_Fibaro'}
                
        },
         
    execute = function(dz, item)
            local LuxDevice = dz.devices(695)                 
            local Lux = LuxDevice.lux
            local Lights = dz.groups("Lys2etasje")
            
        if Lux < 12 and item.state == "On" then
            Lights.cancelQueuedCommands()
            
		if dz.time.matchesRule("at 23:01-06:00") then
			Lights.dimTo(10)
		
		else
			Lights.dimTo(50)
			end
        elseif item.state == "Off" and Lights.state == "On" then 
            Lights.switchOff().afterSec(90)
        
        end
    end
}
But the last script doesn't turn any bulbs on. Any thoughts?

Edit: no errors in logs (Have enabled Debug (Everything))
I'm using Conbee II and two Ikea Tradfri bulbs, a motion sensor from Fibaro and one from Xiaomi and deCONZ plugin

Re: Problem with groups and dzVents

Posted: Saturday 21 December 2019 17:51
by waaren
havnegata wrote: Saturday 21 December 2019 13:20 I'm a bit stuck. I have TWO Ikea bulbs which I'm trying to turn on simultaneously by using two motion sensors
But the last script doesn't turn any bulbs on. Any thoughts?
Edit: no errors in logs (Have enabled Debug (Everything))
Kind of surprised you don't see any errors in the log. dimTo is not one of the available group methods and if I try your script at my test system it will come with the message:
Error: dzVents: Error: (2.5.3) ... 18: attempt to call a nil value (field 'dimTo')

Please see below for a script example where you can overcome this by using a function that will loop through all devices in a group.

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'Bevegelse_trapp2etasje', 
            'Bevegelse_Fibaro',
        },
    },

       logging = 
    { 
        level = domoticz.LOG_DEBUG
    },

    execute = function(dz, item)
        _G.logMarker =  _G.moduleLabel

        local LuxDevice = dz.devices(695)
        local Lux = LuxDevice.lux
        local Lights = dz.groups("Lys2etasje")
        
        local function groupDimTo(group, level)
            group.devices().forEach(function(dv)
                dv.dimTo(level)
            end)
        end
 
        if Lux < 12 and item.state == "On" then
            Lights.cancelQueuedCommands()
            if dz.time.matchesRule("at 23:01-06:00") then
                groupDimTo(Lights, 10)
            else
                groupDimTo(Lights, 50)
            end
        elseif item.state == "Off" and Lights.state == "On" then 
            Lights.switchOff().afterSec(90)
        end
    end
}

Re: Problem with groups and dzVents

Posted: Saturday 21 December 2019 18:25
by havnegata
Wow! That's awesome! Thanks a lot!

Re: Problem with groups and dzVents

Posted: Monday 23 December 2019 21:33
by havnegata
Now there is only a small problem. The lights turn on fine, but they won't turn off. There are still no errors in the log

Re: Problem with groups and dzVents

Posted: Monday 23 December 2019 22:05
by waaren
havnegata wrote: Monday 23 December 2019 21:33 Now there is only a small problem. The lights turn on fine, but they won't turn off. There are still no errors in the log
My guess is that the state of Lights is not "On" when the script reaches line 37. Can you try to change that line from

Code: Select all

        elseif item.state == "Off" and Lights.state == "On" then 
to

Code: Select all

        elseif item.state == "Off" and Lights.state ~= "Off" then 

Re: Problem with groups and dzVents

Posted: Wednesday 25 December 2019 17:51
by havnegata
Thanks for your help :-) I tried your solution, but it didn't give me my wanted result. So I minimized the script a little and this works:

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'Bevegelse_trapp2etasje', 
            'Bevegelse_Fibaro',
        },
    },

       logging = 
    { 
        level = domoticz.LOG_DEBUG,
        marker = "Yngve"
    },

    execute = function(dz, item)

        local Lights = dz.groups("Lys2etasje")
        

         if item.state == "On" then
            Lights.cancelQueuedCommands()
            Lights.switchOn().checkFirst()
        elseif item.state == "Off" and Lights.state ~= "Off" then 
            Lights.switchOff().afterSec(60)
        end
    end
}
But it would be nice to be able to expand it a bit, but then it won't turn off:

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'Bevegelse_trapp2etasje', 
            'Bevegelse_Fibaro',
        },
    },

       logging = 
    { 
        level = domoticz.LOG_DEBUG,
        marker = "Yngve"
    },

    execute = function(dz, item)
      

        
        
        local Lights = dz.groups("Lys2etasje")
   local function groupDimTo(group, level)
            group.devices().forEach(function(dv)
                dv.dimTo(level)
 end)
 end
        if item.state == "On" then
            Lights.cancelQueuedCommands()
   if dz.time.matchesRule("at 23:01-06:00") then
                groupDimTo(Lights, 10)
            else
                groupDimTo(Lights, 50)
            end
        
        elseif item.state == "Off" and Lights.state ~= "Off" then 
            Lights.switchOff().afterSec(90)					-- also, without success, tried with: groupDimTo(Lights, 0)
        end
end

}
This part works:

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'Bevegelse_trapp2etasje', 
            'Bevegelse_Fibaro',
        },
    },

       logging = 
    { 
        level = domoticz.LOG_DEBUG,
        marker = "Yngve"
    },

    execute = function(dz, item)
      

        
        
        local Lights = dz.groups("Lys2etasje")
   local function groupDimTo(group, level)
            group.devices().forEach(function(dv)
                dv.dimTo(level)
 end)
 end
        if item.state == "On" then
            Lights.cancelQueuedCommands()
   if dz.time.matchesRule("at 23:01-06:00") then
                groupDimTo(Lights, 10)
            else
                groupDimTo(Lights, 50)
            end
But the Off-part doesn't:

Code: Select all

elseif item.state == "Off" and Lights.state ~= "Off" then 
            Lights.switchOff().afterSec(90)
        end
end

}
I also tried to exchange this part:

Code: Select all

Lights.switchOff().afterSec(90)
with this part:

Code: Select all

groupDimTo(Lights, 0).afterSec(90)
without success

Re: Problem with groups and dzVents

Posted: Saturday 28 December 2019 0:36
by waaren
havnegata wrote: Wednesday 25 December 2019 17:51 Thanks for your help :-) I tried your solution, but it didn't give me my wanted result. So I minimized the script a little and this works:
Hi can you try this ?

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'Bevegelse_trapp2etasje', 
            'Bevegelse_Fibaro',
        },
    },

       logging = 
    { 
        level = domoticz.LOG_DEBUG
    },

    execute = function(dz, item)
        _G.logMarker =  _G.moduleLabel

        local LuxDevice = dz.devices(695)
        local Lux = LuxDevice.lux
        local Lights = dz.groups("Lys2etasje")
        
        local function groupDimTo(group, level)
            group.devices().forEach(function(dv)
                dv.dimTo(level)
            end)
        end
        
        local function groupOff(group, delay)
            delay = delay or 0
            group.devices().forEach(function(dv)
                dv.switchOff().afterSec(delay)
            end)
        end
        
        if Lux < 12 and item.state == "On" then
            Lights.cancelQueuedCommands()
            if dz.time.matchesRule("at 23:01-06:00") then
                groupDimTo(Lights, 10)
            else
                groupDimTo(Lights, 50)
            end
        elseif item.state == "Off" then 
            groupOff(Lights, 90)
        end
    end
}

Re: Problem with groups and dzVents

Posted: Sunday 29 December 2019 10:31
by havnegata
Sorry for the late reply, but because of Christmas I haven't been at home. Tested now and it still doesn't turn off. Here is something from the log (I find it difficult to extract the relevant loglines, tips is appreciated :-) ).
Spoiler: show

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
           -- 'Bevegelse_trapp2etasje', 
            'Bevegelse_Fibaro',
            'Bevegelse_StueSoer',               -- added another motion sensor for simplifying testing
        },
    },

       logging = 
    { 
        level = domoticz.LOG_DEBUG,
        marker = "Yngve"                        -- this helped me a bit for filtering
    },

    execute = function(dz, item)
       -- _G.logMarker =  _G.moduleLabel        -- what does this do?
        
        local LuxDevice = dz.devices(695)
        local Lux = LuxDevice.lux
        local Lights = dz.groups("Lys2etasje")
        
        local function groupDimTo(group, level)
            group.devices().forEach(function(dv)
                dv.dimTo(level)
            end)
        end
        
        local function groupOff(group, delay)
            delay = delay or 0
            group.devices().forEach(function(dv)
                dv.switchOff().afterSec(delay)
            end)
        end
        
        if Lux < 12 and item.state == "On" then
            Lights.cancelQueuedCommands()
            if dz.time.matchesRule("at 23:01-06:00") then
                groupDimTo(Lights, 10)
            else
                groupDimTo(Lights, 50)
            end
        elseif item.state == "" then 
            groupOff(Lights, 90)
        end
    end
}

Code: Select all

2019-12-29 10:27:22.753 Status: dzVents: Info: Yngve: ------ Start internal script: Test lys 2 etasje: Device: "Bevegelse_StueSoer (Aeotec_gen5)", Index: 139
2019-12-29 10:27:22.754 Status: dzVents: Debug: Yngve: Processing device-adapter for Lux_Fibaro: Lux device adapter
2019-12-29 10:27:22.754 Status: dzVents: Debug: Yngve: Processing device-adapter for Lys2etasje: Group device adapter
2019-12-29 10:27:22.755 Status: dzVents: Debug: Yngve: Processing device-adapter for 2etasje_gangnord: Switch device adapter
2019-12-29 10:27:22.755 Status: dzVents: Debug: Yngve: Constructed timed-command: Set Level 50
2019-12-29 10:27:22.756 Status: dzVents: Debug: Yngve: Processing device-adapter for 2etasje_gangsør: Switch device adapter
2019-12-29 10:27:22.756 Status: dzVents: Debug: Yngve: Constructed timed-command: Set Level 50
2019-12-29 10:27:22.756 Status: dzVents: Info: Yngve: ------ Finished Test lys 2 etasje

Re: Problem with groups and dzVents

Posted: Sunday 29 December 2019 15:15
by waaren
havnegata wrote: Sunday 29 December 2019 10:31 Sorry for the late reply, but because of Christmas I haven't been at home. Tested now and it still doesn't turn off. Here is something from the log (I find it difficult to extract the relevant loglines, tips is appreciated :-) ).

Code: Select all

elseif item.state == "" then
Why did you remove the Off in this line ?

Code: Select all

elseif item.state == "" then
It should either be

Code: Select all

elseif item.state == "Off" then
or

Code: Select all

elseif item.state ~= "On" then

Re: Problem with groups and dzVents

Posted: Sunday 29 December 2019 16:07
by havnegata
:oops: Don't know how that happened, but anyways, great success! Thanks again, waaren, great support! :D