Page 1 of 1

script qubino dimmer ad motion

Posted: Wednesday 22 July 2020 12:53
by AllesVanZelf
I have a script that does not work as I would have expected.

Code: Select all

return
{
     on =
     { devices =
        {
            'Daglicht',
            'MS Zolder Overloop Beweging',
            'Lamp Zolder Overloop Dimmer',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
    },
    
    execute = function(dz)
        local dimmer = dz.devices('Lamp Zolder Overloop Dimmer')
        local daglicht = dz.devices('Daglicht').state
        local motion = dz.devices('MS Zolder Overloop Beweging').state
 
 
        dz.log('Lamp Zolder Overloop Dimmer,    state: ' .. dimmer.state, dz.LOG)
        dz.log('Daglicht,   state: ' .. daglicht, dz.LOG_DEBUG)
        dz.log('MS Zolder Overloop Beweging,      state: ' .. motion, dz.LOG)

        if dimmer.state == 'On' and motion == 'On' then
            dz.log('Switching dimmer zolder to 35%', dz.LOG_DEBUG)
            dimmer.dimTo(35)
            
        elseif dimmer.state == 'On' and daglicht == 'On' and motion == 'Off' then
            dz.log('Switching dimmer zolder to 20% and then Off overdag', dz.LOG_DEBUG)
            dimmer.setLevel(20).afterSec(45)
            dimmer.switchOff().afterSec(60)
            
        elseif dimmer.state == 'On' and daglicht == 'Off' and dz.time.matchesRule('at 16:00-23:00') and motion == 'Off' then
            dz.log('Switching dimmer zolder to 20% avond', dz.LOG_DEBUG)
            dimmer.dimTo(20).afterSec(150)
            
        elseif dimmer.state == 'On' and daglicht == 'Off' and dz.time.matchesRule('at 23:00-06:00') and motion == 'Off' then
            dz.log('Switching dimmer zolder to 20 and then Off nacht', dz.LOG_DEBUG)
            dimmer.dimTo(20).afterSec(150)
            dimmer.switchOff().afterSec(200)
        end
    end
}
The first command is excecuted. So 'Overdag' this is dimmer.setLevel(20). If I comment out -- dimmer.setLevel then The switch is switched Off.
Why is this? Why do they not both excecute?
Somethng wrong in my code, but what? Please advise

Re: script qubino dimmer ad motion

Posted: Wednesday 22 July 2020 14:33
by AllesVanZelf
I think I solved it myself 8-)
I rebuild the script and changed some:

Code: Select all

return
{
     on =
     {
        devices =
        {
            'MS Zolder Overloop Beweging',
            'Lamp Zolder Overloop Dimmer',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
    },
    
    execute = function(dz)
        local dimmer = dz.devices('Lamp Zolder Overloop Dimmer')
        local motion = dz.devices('MS Zolder Overloop Beweging').state
 
 
        dz.log('Lamp Zolder Overloop Dimmer,    state: ' .. dimmer.state, dz.LOG)
        dz.log('MS Zolder Overloop Beweging,      state: ' .. motion, dz.LOG)


        if dimmer.level > 0 and motion == 'On' then
            dz.log('Switching dimmer zolder to 35%', dz.LOG_DEBUG)
            dimmer.dimTo(35).silent()
         
         elseif dimmer.level > 0 and motion == 'Off' and dz.time.matchesRule('between 23:00 and sunset') then
            dz.log('Switching dimmer zolder to 20% and then Off', dz.LOG_DEBUG)
            dimmer.dimTo(20).afterSec(45).silent()
            dimmer.switchOff().afterSec(60).silent()
        
        elseif dimmer.level > 0 and motion == 'Off' and dz.time.matchesRule('between sunset and 23:00') then
            dz.log('Switching dimmer zolder to 20% avond', dz.LOG_DEBUG)
            dimmer.dimTo(20).afterSec(45).silent()
        
        elseif dimmer.state == 'Off' or dimmer.level == 0 then
            dimmer.cancelQueuedCommands()
            dz.log('Switching dimmer zolder cancelQueueCommands', dz.LOG_DEBUG)
        end
    end

} 
At first, after the light went Off, it went On again after some seconds. Therefore I added the last part:

Code: Select all

        elseif dimmer.state == 'Off' or dimmer.level == 0 then
            dimmer.cancelQueuedCommands()
            dz.log('Switching dimmer zolder cancelQueueCommands', dz.LOG_DEBUG)
It looks like this is working.
If you see a problem in this code, or if this could be more easily done, please advise me!

Re: script qubino dimmer ad motion

Posted: Wednesday 22 July 2020 14:56
by waaren
AllesVanZelf wrote: Wednesday 22 July 2020 12:53 I have a script that does not work as I would have expected.

The first command is excecuted. So 'Overdag' this is dimmer.setLevel(20). If I comment out -- dimmer.setLevel then The switch is switched Off.
Why is this? Why do they not both excecute?
Somethng wrong in my code, but what? Please advise
the commands you give to dimmer (Lamp Zolder Overloop Dimmer) do trigger the script again leading to unexpected results because this device can have more states then only 'On' and 'Off' .

Can you try this?

Code: Select all

return
{
     on =
     { devices =
        {
            'Daglicht',
            'MS Zolder Overloop Beweging',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
    },
    
    execute = function(dz)
        local dimmer = dz.devices('Lamp Zolder Overloop Dimmer')
        local daglicht = dz.devices('Daglicht').state
        local motion = dz.devices('MS Zolder Overloop Beweging').state
 
 
        dz.log('Lamp Zolder Overloop Dimmer,    state: ' .. dimmer.state, dz.LOG)
        dz.log('Daglicht,   state: ' .. daglicht, dz.LOG_DEBUG)
        dz.log('MS Zolder Overloop Beweging,      state: ' .. motion, dz.LOG)

        if dimmer.state == 'On' and motion == 'On' then
            dz.log('Switching dimmer zolder to 35%', dz.LOG_DEBUG)
            dimmer.dimTo(35).silent()
            
        elseif dimmer.state == 'On' and daglicht == 'On' and motion == 'Off' then
            dz.log('Switching dimmer zolder to 20% and then Off overdag', dz.LOG_DEBUG)
            dimmer.dimTo(20).afterSec(45).silent()
            dimmer.dimTo(0).afterSec(60).silent()
            
        elseif dimmer.state == 'On' and daglicht == 'Off' and dz.time.matchesRule('at 16:00-23:00') and motion == 'Off' then
            dz.log('Switching dimmer zolder to 20% avond', dz.LOG_DEBUG)
            dimmer.dimTo(20).afterSec(150).silent()
            
        elseif dimmer.state == 'On' and daglicht == 'Off' and dz.time.matchesRule('at 23:00-06:00') and motion == 'Off' then
            dz.log('Switching dimmer zolder to 20 and then Off nacht', dz.LOG_DEBUG)
            dimmer.dimTo(20).afterSec(150).silent()
            dimmer.dimTo(0).afterSec(200).silent()
        end
    end
}

Re: script qubino dimmer ad motion  [Solved]

Posted: Wednesday 22 July 2020 15:02
by AllesVanZelf
So the silent() part would have done the trick here. I did include that in my second script and this seems to work. So maybe the dimmer.cancelQueuedCommands() would have not been necessary.