domoticz.time.matchesRule - can i write it in a more elagant way?

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

Moderator: leecollings

Post Reply
rkarolek
Posts: 33
Joined: Friday 31 August 2018 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

domoticz.time.matchesRule - can i write it in a more elagant way?

Post by rkarolek »

Hi
I wrote script which use domoticz.time.matchesRule.
I try set LED lvl during all day depends on actual time.
script works well, but i wonder if its possible to write it in more elegant way?

Code: Select all

return {
   on = {
  	timer = {'between 10:00 and 23:40'}    --uruchomienie skryptu co minute
   },
   
   logging = {
       level = domoticz.LOG_INFO,
       marker = "A240_LED_BLUE"
   },
   
   execute = function(domoticz)
    
    if (domoticz.time.matchesRule('at 10:00-20:10')) then

      	BLUE_LVL = 50
      	BLUE   = domoticz.devices('B1').level
	    
	    if (BLUE ~= BLUE_LVL) then
	        domoticz.devices('B1').dimTo(BLUE_LVL)
	        domoticz.log('nowy poziom BLUE   ' .. BLUE_LVL)
	    end
    end
    
    if (domoticz.time.matchesRule('at 20:11-21:00')) then

      	BLUE_LVL = 25
      	BLUE   = domoticz.devices('B1').level
	    
	    if (BLUE ~= BLUE_LVL) then
	        domoticz.devices('B1').dimTo(BLUE_LVL)
	        domoticz.log('nowy poziom BLUE   ' .. BLUE_LVL)
	    end
    end

    if (domoticz.time.matchesRule('at 21:01-21:10')) then
 
      	BLUE_LVL = 12
      	BLUE   = domoticz.devices('B1').level
	    
	    if (BLUE ~= BLUE_LVL) then
	        domoticz.devices('B1').dimTo(BLUE_LVL)
	        domoticz.log('nowy poziom BLUE   ' .. BLUE_LVL)
	    end
    end

    if (domoticz.time.matchesRule('at 21:11-22:00')) then
 
      	BLUE_LVL = 50
      	BLUE   = domoticz.devices('B1').level
	    
	    if (BLUE ~= BLUE_LVL) then
	        domoticz.devices('B1').dimTo(BLUE_LVL)
	        domoticz.log('nowy poziom BLUE   ' .. BLUE_LVL)
	    end
    end

    if (domoticz.time.matchesRule('at 22:01-22:30')) then

      	BLUE_LVL = 25
      	BLUE   = domoticz.devices('B1').level
	    
	    if (BLUE ~= BLUE_LVL) then
	        domoticz.devices('B1').dimTo(BLUE_LVL)
	        domoticz.log('nowy poziom BLUE   ' .. BLUE_LVL)
	    end
    end

    if (domoticz.time.matchesRule('at 22:31-23:00')) then

      	BLUE_LVL = 1
      	BLUE   = domoticz.devices('B1').level
	    
	    if (BLUE ~= BLUE_LVL) then
	        domoticz.devices('B1').dimTo(BLUE_LVL)
	        domoticz.log('nowy poziom BLUE   ' .. BLUE_LVL)
	    end
    end

    if (domoticz.time.matchesRule('at 23:01-23:30')) then
 
      	BLUE_LVL = 0
      	BLUE   = domoticz.devices('B1').level
	    
	    if (BLUE ~= BLUE_LVL) then
	        domoticz.devices('B1').dimTo(BLUE_LVL)
	        domoticz.log('nowy poziom BLUE   ' .. BLUE_LVL)
	    end
    end

end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: domoticz.time.matchesRule - can i write it in a more elagant way?

Post by waaren »

rkarolek wrote: Tuesday 16 June 2020 12:47 Hi
I wrote script which use domoticz.time.matchesRule.
script works well, but i wonder if its possible to write it in more elegant way?
Something like below ?

Code: Select all

return {
   on = {
      timer = {'between 10:00 and 23:40'}    --uruchomienie skryptu co minute
   },

   logging = {
       level = domoticz.LOG_INFO,
       marker = "A240_LED_BLUE"
   },

   execute = function(domoticz)

        local dimmer = domoticz.devices('B1')
        local timeBasedLevels =
        {
            ['at 10:00-20:10'] = 50,
            ['at 20:11-21:00'] = 25,
            ['at 21:01-21:10'] = 12,
            ['at 21:11-22:00'] = 50,
            ['at 22:01-23:00'] = 50,
            ['at 22:31-23:00'] = 1,
            ['at 23:01-23:30'] = 0,
        }

        for timeRule, level in pairs(timeBasedLevels) do
            if domoticz.time.matchesRule(timeRule) and dimmer.level ~= level then
                dimmer.dimTo(level)
                domoticz.log('nowy poziom BLUE   ' .. level)
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
rkarolek
Posts: 33
Joined: Friday 31 August 2018 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: domoticz.time.matchesRule - can i write it in a more elagant way?

Post by rkarolek »

Wow!!!
looks more elegant, tyvm for your help
rkarolek
Posts: 33
Joined: Friday 31 August 2018 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: domoticz.time.matchesRule - can i write it in a more elagant way?

Post by rkarolek »

one more question....
is t possible to change in that way two or more device? each one with diferent value?
its mean for example
10-15 LED1 40% LED2 30% LED3 0%
15-17 LED1 10% LED2 30% LED3 50%
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: domoticz.time.matchesRule - can i write it in a more elagant way?

Post by waaren »

rkarolek wrote: Tuesday 16 June 2020 14:36 is t possible to change in that way two or more device? each one with diferent value?
Yes. something like below..

Code: Select all

return {
   on = {
      timer = {'between 10:00 and 23:40'}    
   },

   logging = {
       level = domoticz.LOG_INFO,
       marker = "A240_LED_BLUE"
   },

   execute = function(dz)

        local dimmers =
        {
            dz.devices('B1'),
            dz.devices('B2'),
            dz.devices('B3'),
        }

        local timeBasedLevels =
        {
            ['at 10:00-20:10'] = { 50, 13, 20 },
            ['at 20:11-21:00'] = { 25, 14, 21 },
            ['at 21:01-21:10'] = { 12, 15, 22 },
            ['at 21:11-22:00'] = { 50, 16, 23 },
            ['at 22:01-22:30'] = { 50, 17, 24 },
            ['at 22:31-23:00'] = { 1, 18, 25 },
            ['at 23:01-23:30'] = { 0, 0, 0 },
        }

        for timeRule, levels in pairs(timeBasedLevels) do
            if dz.time.matchesRule(timeRule) then
                for index, dimmer in ipairs(dimmers) do
                    if dimmer.level ~= levels[index] then
                        dimmer.dimTo(levels[index])
                        dz.log('Now dimming ' .. dimmer.name .. ' from ' ..  dimmer.level .. '%  to ' .. levels[index] .. '%')
                    end
                end
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
rkarolek
Posts: 33
Joined: Friday 31 August 2018 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: domoticz.time.matchesRule - can i write it in a more elagant way?

Post by rkarolek »

tyvm once more
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest