Simplify conditions

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

Moderator: leecollings

Post Reply
Hydci
Posts: 35
Joined: Tuesday 04 June 2019 21:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Simplify conditions

Post by Hydci »

Hello,
I would like to know if it was possible to simplify the script for example the two if dz.time.matchesRule('at 7:30 on mon,tue,thu,fri') and dz.time.matchesRule('at 8:30 on wed,sat,sun') and dz.time.matchesRule('25 minutes after sunset') for not having too much if.
The two blinds must be separated

Code: Select all

return {
        on = 
        {
            timer     = { '25 minutes after sunset','at 7:30 on mon,tue,thu,fri','at 8:30 on wed,sat,sun'},
            devices = {'Jardin','Bouton volet'},
        },

        logging = 
        {
            level = domoticz.LOG_DEBUG, 
            marker = 'Volets' 
        },

    execute = function( dz, item)

        Cuisine = dz.devices('Volet cuisine')  -- racourci des devices
        Salon = dz.devices('Volet salon')
    
        if item.state == "1 Click" and dz.devices('Volet cuisine').state == 'Closed' and dz.devices('Volet salon').state == 'Closed' then
            Cuisine.switchOn().silent()
            Salon.switchOn().silent()
	    elseif item.state == "1 Click" and dz.devices('Volet cuisine').state == 'Open' and dz.devices('Volet salon').state == 'Open' then
	        Cuisine.switchOff().silent()
            Salon.switchOff().silent()
        end
	    if item.state == "2 Click" and dz.devices('Volet cuisine').state == 'Closed' then
            Cuisine.switchOn().silent()
	    elseif item.state == "2 Click" and dz.devices('Volet cuisine').state == 'Open' then
	        Cuisine.switchOff().silent()
        end
	    if item.state == "3 Click" and dz.devices('Volet salon').state == 'Closed' then
            Salon.switchOn().silent()
	    elseif item.state == "3 Click" and dz.devices('Volet salon').state == 'Open' then
            Salon.switchOff().silent()
        end
    
        if item.isDevice and item.state == 'Off' and dz.time.matchesRule('at nighttime') and dz.devices('Volet salon').state == 'Open' then
            Salon.switchOff().silent()
		end
        if dz.time.matchesRule('25 minutes after sunset') and dz.devices('Volet cuisine').state == 'Open' then 
            Cuisine.switchOff().silent()
		end
		if dz.time.matchesRule('25 minutes after sunset') and dz.devices('Volet salon').state == 'Open' and dz.devices('Jardin').state == 'Off' then 
            Salon.switchOff().silent()
		end
        if dz.time.matchesRule('at 7:30 on mon,tue,thu,fri') and dz.devices('Volet cuisine').state == 'Closed' then -- lever du soleil
            Cuisine.switchOn().silent()
		end
		if dz.time.matchesRule('at 7:30 on mon,tue,thu,fri') and dz.devices('Volet salon').state == 'Closed' then -- lever du soleil
            Salon.switchOn().silent()
		end
		if dz.time.matchesRule('at 8:30 on wed,sat,sun') and dz.devices('Volet cuisine').state == 'Closed' then -- lever du soleil
            Cuisine.switchOn().silent()
		end
		if dz.time.matchesRule('at 8:30 on wed,sat,sun') and dz.devices('Volet salon').state == 'Closed' then -- lever du soleil
            Salon.switchOn().silent()
			end
        end
    end
}
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest