How to set dimmer based on time between to states

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
LPCevaal
Posts: 1
Joined: Saturday 02 January 2021 9:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

How to set dimmer based on time between to states

Post by LPCevaal »

Hello, I am new to dzVents and am trying to adapt the following script based on a previous dimmer topic.

Code: Select all

return
{
    on =
    {
        devices = { 'Wall Switch Kitchen' }, -- Switch Controlling two lights
    },

    logging =
    {
        level = domoticz.LOG_INFO,
        marker = "Wall Switch Kitchen"
    },

    execute = function(domoticz, device)

        local dimDevice = domoticz.devices(677,679) -- Two lights to be controlled at the a same time
        local dimLevel = 100 -- Would it be possible to determine the actual dim level as a starting point?
                
        if device.state == "Brightness_Move_Down" then
            <set a timer to determine duration of this device state in milliseconds>
             
        elseif device.state == "Brightness_Stop" then
            <a calculation of the amount of milliseconds between Brightness_Move_Down and Brightness_Stop>
            <every 200 milliseconds is 5% of dim, so dimLevel = dimLevel - Calculation/200 * 5>

            domoticz.log('Set ' .. dimDevice.name .. ' to dimLevel '.. dimLevel .. '%', domoticz.LOG_INFO)
            dimDevice.dimTo(dimLevel)
            dimDevice.cancelQueuedCommands()
            if dimDevice.level == 0 then
                dimDevice.switchOff()
           
        if device.state == "Brightness_Move_Up" then  -- Repeat code, but now to increase dimmer
        local dimLevel = 0 -- Reset Dim Level to allow it to go up

        <set a timer to determine duration of this device state in milliseconds>
             
        elseif device.state == "Brightness_Stop" then
            <a calculation of the amount of milliseconds between Brightness_Move_Down and Brightness_Stop>
            <every 200 milliseconds is 5% of dim, so dimLevel = dimLevel + Calculation/200 * 5>

            domoticz.log('Set ' .. dimDevice.name .. ' to dimLevel '.. dimLevel .. '%', domoticz.LOG_INFO)
            dimDevice.dimTo(dimLevel)
            dimDevice.cancelQueuedCommands()
            if dimDevice.level == 100 then
                dimDevice.switchOn()
 
           end
        end
    end
}
To summarize the above: a long press on the buton results in state "Brightness_Move_Up" and releasing the button results in state "Brightness_Stop". I would like to use the time between the two events to determine the amount of dimming required. Lights are controlled by two buttons; one for On and one for Off. The on button results in the "Brightness_Move_Up" sequence and the Off button results in the "Brightness_Move_Down" sequence.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to set dimmer based on time between to states

Post by waaren »

LPCevaal wrote: Saturday 02 January 2021 10:17
To summarize the above: a long press on the buton results in state "Brightness_Move_Up" and releasing the button results in state "Brightness_Stop". I would like to use the time between the two events to determine the amount of dimming required. Lights are controlled by two buttons; one for On and one for Off. The on button results in the "Brightness_Move_Up" sequence and the Off button results in the "Brightness_Move_Down" sequence.
I doubt if the timing will be very accurate but I tested below code and it will do what you describe.

Code: Select all

local scriptVar = 'dimTimer'

return 
{
    on = 
    {
        devices = 
        {
            'Wall Switch Kitchen', 
        },
    },

    logging = 
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all ok
        marker = scriptVar,
    },
    
    data =
    {
        action = 
        {
            initial = '',
        },
    },
    
    execute = function(dz, item)
        local dimmer1 = dz.devices(677)
        local dimmer2 = dz.devices(679)

        local dimDevices = { dimmer1, dimmer2 }

        function mDimTo(level, devices)
            for _, device in ipairs(devices) do
                device.dimTo(level)
           end
        end

        if item.state ~= 'Brightness_Stop' then
            dz.data.action = item.state
        else
            direction = ( dz.data.action == 'Brightness_Move_Up' and 1 ) or -1
            newLevel = math.min( 100, math.max( 0, dimmer1.level + ( item.lastUpdate.millisecondsAgo / 200 * 5 * direction ) ) )
            
            dz.log('dim dimDevices from level ' .. dimmer1.level .. '% to level '.. newLevel .. '%', dz.LOG_INFO)
            mDimTo(newLevel, dimDevices)
        end
    
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest