Page 1 of 1

Timer questions

Posted: Sunday 01 November 2020 4:51
by Bospieper
Hello,
I am starting to begin programming in dzvents. As a newbie i am stumbling into beginners problems such as timing.
I have a sensor wich detect movement and a dimmable lamp. I want to use four different time frames each with a different dim level and time to switch off. I have it working in Blockly but I want all my Blockly's transferred to dzvents.
devices: sensor and dimmer
timeframes: sunrise-13:00, 13:00-sunset, sunset-23:59 and 23:59-sunrise
switchoff: 4 minutes off 4 minutes 2 minutes
dimlevel 50% 0% 50% 10%
I have a starting script but that is only for one time frame. I am having problems with programming more than one time frame.
I hope someone can give me a start in how to use time frames.
thanx
Piet

Code: Select all


return {
	active = true,
	
	on = {
		devices = {
            			['Sensor'] = {'at 23:59-04:00'}

        }
},
	execute = function(domoticz, Sensor, timer)
		
			if domoticz.devices('Sensor').state == 'On' then
			   domoticz.devices('Dimmer').dimTo(50).forMin(2)
            
        end
        end
}

Re: Timer questions  [SOLVED]

Posted: Sunday 01 November 2020 7:57
by waaren
Bospieper wrote: Sunday 01 November 2020 4:51 I want to use four different time frames each with a different dim level and time to switch off.
Can you try below script

Code: Select all

--[[
         dzvents timerule based settings
]]--


return {
    active = true,

    on =
    {
        devices =
        {
            'Sensor',
        }
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- set to domoticz.LOG_ERROR when all ok
        marker = 'time frames',
    },

    execute = function(dz, item)

        local light = dz.devices('Dimmer')

        local timeTable =
        {  --  periodStart, periodEnd, dimLevel, duration,
               {'sunrise','12:59',50,4},
               {'13:00','sunset',0,0},
               {'sunset','23:59',50,4},
               {'23:59','sunrise',10,2},
        }

        local function setParms()
            for _, settings in ipairs(timeTable) do
                local constructedTimeRule = 'between ' ..  settings[1] .. ' and ' .. settings[2]
                if dz.time.matchesRule(constructedTimeRule) then
                    dz.log('It is ' .. constructedTimeRule .. ' ==>> ' .. light.name .. ' will be set to dimlevel ' .. settings[3] .. ', for ' .. settings[4] .. ' minutes.', dz.LOG_DEBUG)
                    return settings[3], settings[4]
                end
            end
            return 0, 0
        end

        if item.state == 'On' then
           local level, duration = setParms()
           light.cancelQueuedCommands()
           light.dimTo(level)
           light.dimTo(0).afterMin(duration)
        end
    end
}


Re: Timer questions

Posted: Sunday 01 November 2020 12:52
by Bospieper
Hi Waaren,
Thanx for the quick reponse. This works ! :)
I see I have yet a lot to learn about programming in Lua en Dzvents.
Piet.

Re: Timer questions

Posted: Sunday 01 November 2020 17:42
by waaren
Bospieper wrote: Sunday 01 November 2020 12:52 I see I have yet a lot to learn about programming in Lua en Dzvents.
Feel free to ask if anything is unclear in the example. I am happy to explain where needed.

Re: Timer questions

Posted: Sunday 01 November 2020 21:09
by Bospieper
Hi Waaren,
I will certainly do that but first I have to study on how to program in Lua. I've ordered a book from Amazon, it will be soon in my possession.
As I see how you did the programming is this something you do not get from the document Next generation LUA scripting. I see all kinds of Lua commands in the script. I was struggling for weeks with this simple script to get it working but with no results and I see now why :)
So again thanks and i hope that soon I am able to do some programming myself.
Piet

Re: Timer questions

Posted: Wednesday 09 June 2021 14:27
by egkman
Great script!!

I added the feature to have the dimming device returning to a certain (default) level, after the timer expires.
This is helpful in certain dark area's (halls, etc.) where you may want this default level during periods as defined in the timetable.

Code: Select all

--[[
         dzvents timerule based settings - extended with 'dim level after' feature
]]--


return {
    active = true,

    on =
    {
        devices =
        {
            'your_sensor',
        }
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- set to domoticz.LOG_ERROR when all ok
        marker = 'time frames',
    },

    execute = function(dz, item)

        local light = dz.devices('your_dimming_light')

        local timeTable =
        {  --  periodStart, periodEnd, dimLevel, duration, afterLevel
               {'sunrise','12:59',75,2,25},
               {'13:00','sunset',95,2,30},
               {'sunset','23:59',75,2,25},
               {'23:59','sunrise',25,2,0},
        }

        local function setParms()
            for _, settings in ipairs(timeTable) do
                local constructedTimeRule = 'between ' ..  settings[1] .. ' and ' .. settings[2]
                if dz.time.matchesRule(constructedTimeRule) then
                    dz.log('It is ' .. constructedTimeRule .. ' ==>> ' .. light.name .. ' will be set to dimlevel ' .. settings[3] .. ', for ' .. settings[4] .. ' minutes.' .. ' returning to level ' .. settings[5] .. ' after', dz.LOG_DEBUG)
                    return settings[3], settings[4], settings[5]
                end
            end
            return 0, 0, 0
        end

        if item.state == 'On' then
           local level, duration, afterlevel = setParms()
           light.cancelQueuedCommands()
           light.dimTo(level)
           light.dimTo(afterlevel).afterMin(duration)
        end
    end
}