Page 1 of 1

DzEvent script for dimmer control

Posted: Saturday 17 October 2020 13:11
by madpatrick
Hi,

I'm trying to write (cope/paste) my first script, but unfortunately need some help.
My plan is to swith on the dimmer (lamp) with a wall switch.
Button pressed is on at level 20%
Button pressed (again) is on at level 70%

My difficulty is to read out the dimValue of the dimmer together with the switch press

Code: Select all

device.name == 'Hal-ES2' and device.dimValue >= 20

Code: Select all

device.name == 'Hal-ES2' and dz.devices('Hal - Lamp').dimValue >= 20

Code: Select all

return
{
    on =
    {
        devices = {
            'Hal-ES2',     -- Switch Enkel/Short
            'Hal-ES4',     -- Switch Enkel/Short onder
            'Hal-EL2-1',   -- Switch Enkel/Long press
            'Hal-DS1',     -- Switch Dubbel/Short press
            'Hal-DS3',     -- Switch Dubbel/Short press
            'Hal-DL1-1'    -- Switch Dubbel/Long press
    }
},
    execute = function(dz, device)
                --dz.log('Contact ' .. item.name .. ' state is ' .. item.state, dz.LOG_DEBUG)
        if ((device.name == 'Hal-ES2') or
            (device.name == 'Hal-DS1')) then
            dz.devices('Hal - Lamp').dimTo(20)
        elseif
            ((device.name == 'Hal-ES4') or
             (device.name == 'Hal-DS3')) then
            dz.devices('Hal - Lamp').switchOff()
        elseif 
            ((device.name == 'Hal-ES2' and device.dimValue >= 20) or
             (device.name == 'Hal-DL1-1' and device.dimValue >= 20)) then
            dz.devices('Hal - Lamp').dimTo(70)
        end
    end
}
Another question is how do is can add a log message per section.
the dz.log is nog working somehow for me.....

Re: DzEvent script for dimmer control

Posted: Saturday 17 October 2020 15:01
by waaren
madpatrick wrote: Saturday 17 October 2020 13:11 I'm trying to write (cope/paste) my first script, but unfortunately need some help.
My plan is to switch on the dimmer (lamp) with a wall switch.
Button pressed is on at level 20%
Button pressed (again) is on at level 70%

Another question is how do is can add a log message per section.
the dz.log is nog working somehow for me.....
You logging was referring to item but you pass device to the execute function.

Can you have a look at this ?

Code: Select all

return
{
    on =
    {
        devices =
        {
            'Hal-ES2',     -- Switch Enkel/Short
            'Hal-ES4',     -- Switch Enkel/Short onder
            'Hal-EL2-1',   -- Switch Enkel/Long press
            'Hal-DS1',     -- Switch Dubbel/Short press
            'Hal-DS3',     -- Switch Dubbel/Short press
            'Hal-DL1-1',   -- Switch Dubbel/Long press
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'Dim switcher',
    },

    execute = function(dz, device)
        local name = device.name
        local lamp = dz.devices('Hal - Lamp')
        
        dz.log('Contact ' .. name .. ' state is ' .. device.state, dz.LOG_DEBUG)

        if ( name == 'Hal-ES2' or name == 'Hal-DL1-1' ) and lamp.active and lamp.level ~= 70 then
            lamp.dimTo(70)
            dz.log('Hal lamp dimmed at 70%', dz.LOG_DEBUG)
        elseif name == 'Hal-ES2' or name == 'Hal-DS1' then
            lamp.dimTo(20)
            dz.log('Hal lamp switched on at 20%', dz.LOG_DEBUG)
        elseif name == 'Hal-ES4' or name == 'Hal-DS3' then
            lamp.switchOff().checkFirst()
            dz.log('Hal lamp switched Off (if it was on) ', dz.LOG_DEBUG)
        else
            dz.log('No action needed ', dz.LOG_DEBUG)
        end
    end
}

Re: DzEvent script for dimmer control

Posted: Saturday 17 October 2020 15:11
by madpatrick
Thank Waaren,

The 70% level is not working propery.
If i press switch 'Hal-ES2' when the lamp is of it goes directly to 70% and not 20%.
The lamp should go to 70% if the lamp is already 20%

Re: DzEvent script for dimmer control

Posted: Saturday 17 October 2020 15:22
by waaren
madpatrick wrote: Saturday 17 October 2020 15:11 Thank Waaren,

The 70% level is not working propery.
If i press switch 'Hal-ES2' when the lamp is of it goes directly to 70% and not 20%.
The lamp should go to 70% if the lamp is already 20%
I modified my posted script. If a script is not working as expected the please post the log. That will make it easier to support you.

Re: DzEvent script for dimmer control

Posted: Saturday 17 October 2020 15:27
by madpatrick
This is working !
Thanks for your support (again)

ps. i'll post next time the output of the log