DzEvent script for dimmer control

Moderator: leecollings

Post Reply
User avatar
madpatrick
Posts: 759
Joined: Monday 26 December 2016 12:17
Target OS: Linux
Domoticz version: 2025.2
Location: Netherlands
Contact:

DzEvent script for dimmer control

Post 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.....
-= HP server GEN11 =- ZwaveJS-=- Domoticz v2025.2 -=- Dashticz =-
-= Checkout https://github.com/MadPatrick for the plugins =-
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DzEvent script for dimmer control

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
madpatrick
Posts: 759
Joined: Monday 26 December 2016 12:17
Target OS: Linux
Domoticz version: 2025.2
Location: Netherlands
Contact:

Re: DzEvent script for dimmer control

Post 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%
-= HP server GEN11 =- ZwaveJS-=- Domoticz v2025.2 -=- Dashticz =-
-= Checkout https://github.com/MadPatrick for the plugins =-
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DzEvent script for dimmer control

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
madpatrick
Posts: 759
Joined: Monday 26 December 2016 12:17
Target OS: Linux
Domoticz version: 2025.2
Location: Netherlands
Contact:

Re: DzEvent script for dimmer control

Post by madpatrick »

This is working !
Thanks for your support (again)

ps. i'll post next time the output of the log
-= HP server GEN11 =- ZwaveJS-=- Domoticz v2025.2 -=- Dashticz =-
-= Checkout https://github.com/MadPatrick for the plugins =-
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest