Optimisation de script

Forum dédié aux problématiques concernant les scripts pour DomoticZ.
Entourez votre code et les logs avec les balises nommées code grâce au bouton <\>.
Wolfen38
Messages : 1983
Inscription : 19 oct. 2017, 14:49

Optimisation de script

Message par Wolfen38 »

Salut tout le monde.

J'au un script par interrupteur qui se présente comme suit (ce sont des inter xiaaomi à 4 touches possibles) :

Code : Tout sélectionner

commandArray = {}

if (devicechanged['Interrupteur Anna'] == '1 Click' and otherdevices['Virtuel Anna'] == 'Off') then
    commandArray['Virtuel Anna']='On'
	commandArray['Group:Lumiere blanche Anna1']='On'
	commandArray['Interrupteur Anna']='Set Level: 40'
			
	else
	if (devicechanged['Interrupteur Anna'] == '1 Click' and otherdevices['Virtuel Anna'] == 'On') then
		commandArray['Group:Lumiere blanche Anna1']='Off'
		commandArray['Group:Lumiere blanche Anna2']='Off'
		commandArray['Group:Lumiere blanche Anna rgb']='Off'
		commandArray['Group:Lumiere violette Anna']='Off'
		commandArray['Virtuel Anna']='Off'
		commandArray['Interrupteur Anna']='Set Level: 40'
		end
	
end

if (devicechanged['Interrupteur Anna'] == '2 Click' and otherdevices['Virtuel Anna'] == 'Off') then
	commandArray['Virtuel Anna']='On'
	commandArray['Group:Lumiere blanche Anna2']='On'
	commandArray['Interrupteur Anna']='Set Level: 40'
	
	else
	if (devicechanged['Interrupteur Anna'] == '2 Click' and otherdevices['Virtuel Anna'] == 'On') then
		commandArray['Group:Lumiere blanche Anna1']='On'
		commandArray['Interrupteur Anna']='Set Level: 40'
		end
	
end

if ((devicechanged['Interrupteur Anna'] == '3 Click' and otherdevices['Virtuel Anna'] == 'Off')) then 
	commandArray['Virtuel Anna']='On'
	commandArray['Group:Lumiere violette Anna']='On'
	commandArray['Interrupteur Anna']='Set Level: 40'
	
		
	else
	if ((devicechanged['Interrupteur Anna'] == '3 Click' and otherdevices['Virtuel Anna'] == 'On')) then
		commandArray['Group:Lumiere blanche Anna rgb']='On'
		commandArray['Interrupteur Anna']='Set Level: 40'
		end
	
end

if ((devicechanged['Interrupteur Anna'] == '4 Click' and otherdevices['Virtuel prise Anna'] == 'Off')) then 
	commandArray['Virtuel prise Anna']='On'
	commandArray['Prise lampe Anna']='On'
	commandArray['Interrupteur Anna']='Set Level: 40'
	
		
	else
	if ((devicechanged['Interrupteur Anna'] == '4 Click' and otherdevices['Virtuel prise Anna'] == 'On')) then
	commandArray['Virtuel prise Anna']='Off'
	commandArray['Prise lampe Anna']='Off'
	commandArray['Interrupteur Anna']='Set Level: 40'
		end
	
end

return commandArray
Je pense que les latences que j'ai peuvent provenir de scripts assez mal fait comme celui là, et peut être qu'une autre façon de les écrire réduirait les latences non ?

Merci.
Keros
Messages : 6638
Inscription : 23 juil. 2019, 20:57

Re: Optimisation de script

Message par Keros »

Ton code vérifie toutes les conditions de la façon dont tu l'as écrit. Surtout avec les else et les if à la ligne suivante.

En optimisant, tu pourrais l'écrire comme ceci :

Code : Tout sélectionner

if devicechanged['Interrupteur Anna'] == '1 Click' then 
	if otherdevices['Virtuel Anna'] == 'Off' then
		commandArray['Virtuel Anna']='On'
		commandArray['Group:Lumiere blanche Anna1']='On'
		commandArray['Interrupteur Anna']='Set Level: 40'
	else
		commandArray['Group:Lumiere blanche Anna1']='Off'
		commandArray['Group:Lumiere blanche Anna2']='Off'
		commandArray['Group:Lumiere blanche Anna rgb']='Off'
		commandArray['Group:Lumiere violette Anna']='Off'
		commandArray['Virtuel Anna']='Off'
		commandArray['Interrupteur Anna']='Set Level: 40'
elseif devicechanged['Interrupteur Anna'] == '2 Click' then
	if otherdevices['Virtuel Anna'] == 'Off' then
		commandArray['Virtuel Anna']='On'
		commandArray['Group:Lumiere blanche Anna2']='On'
		commandArray['Interrupteur Anna']='Set Level: 40' 40'
	else
		commandArray['Group:Lumiere blanche Anna1']='On'
		commandArray['Interrupteur Anna']='Set Level: 40'
elseif devicechanged['Interrupteur Anna'] == '3 Click'	 then

	ETC ....
end
Comment bien utiliser le forum : Poser une question, Mettre un script, un fichier, une image ou des logs
Mes petits guides : Débuter en programmation, Le débogage, Le choix de matériel, Les sauvegardes
Ma présentation - Mes Tutos
Wolfen38
Messages : 1983
Inscription : 19 oct. 2017, 14:49

Re: Optimisation de script

Message par Wolfen38 »

Tu penses que je peux gagner en latence ?

Je vais essayer.
Keros
Messages : 6638
Inscription : 23 juil. 2019, 20:57

Re: Optimisation de script

Message par Keros »

Tu as d'autres scripts d'interrupteur comme celui ci ?
Comment bien utiliser le forum : Poser une question, Mettre un script, un fichier, une image ou des logs
Mes petits guides : Débuter en programmation, Le débogage, Le choix de matériel, Les sauvegardes
Ma présentation - Mes Tutos
Wolfen38
Messages : 1983
Inscription : 19 oct. 2017, 14:49

Re: Optimisation de script

Message par Wolfen38 »

Oui, tous. Un script par Inter. Une vingtaine d'inter.

Envoyé de mon Mi 9T Pro en utilisant Tapatalk

Keros
Messages : 6638
Inscription : 23 juil. 2019, 20:57

Re: Optimisation de script

Message par Keros »

Oui donc tu vérifies 20 x 8 conditions à chaque fois tu tu as un devicechange. Cela peut entrainer une latence.

Je pense qu'il faudrait regrouper tous tes scripts d'inter dans un seul et utiliser des else if :

Code : Tout sélectionner

if devicechanged["Interrupteur Anna"] then
	
	if devicechanged['Interrupteur Anna'] == '1 Click' then 
		if otherdevices['Virtuel Anna'] == 'Off' then
			commandArray['Virtuel Anna']='On'
			commandArray['Group:Lumiere blanche Anna1']='On'
			commandArray['Interrupteur Anna']='Set Level: 40'
		else
			commandArray['Group:Lumiere blanche Anna1']='Off'
			commandArray['Group:Lumiere blanche Anna2']='Off'
			commandArray['Group:Lumiere blanche Anna rgb']='Off'
			commandArray['Group:Lumiere violette Anna']='Off'
			commandArray['Virtuel Anna']='Off'
			commandArray['Interrupteur Anna']='Set Level: 40'
	elseif devicechanged['Interrupteur Anna'] == '2 Click' then
		if otherdevices['Virtuel Anna'] == 'Off' then
			commandArray['Virtuel Anna']='On'
			commandArray['Group:Lumiere blanche Anna2']='On'
			commandArray['Interrupteur Anna']='Set Level: 40' 40'
		else
			commandArray['Group:Lumiere blanche Anna1']='On'
			commandArray['Interrupteur Anna']='Set Level: 40'
	elseif devicechanged['Interrupteur Anna'] == '3 Click'	 then

		ETC ....
	end

elseif devicechanged["Interrupteur Papa"] then
	
	Le script pour Interrupteur Papa
	
elseif devicechanged["Interrupteur Maman"] then

	Le script pour Interrupteur Maman
	
end
En vérifiant le nom du capteur, tu réduis à 20 le nombre de vérifications dés que tu as un devicechange.
Comment bien utiliser le forum : Poser une question, Mettre un script, un fichier, une image ou des logs
Mes petits guides : Débuter en programmation, Le débogage, Le choix de matériel, Les sauvegardes
Ma présentation - Mes Tutos
denis_brasseur
Messages : 898
Inscription : 24 déc. 2018, 17:05
Localisation : (26)

Re: Optimisation de script

Message par denis_brasseur »

Quand tu dit latence, de quel ordre ?

J'ai une vingtaine de script basé sur un changement d'état, dont certain portant sur plusieurs device, peu ou pas de latence (maxi 1 seconde), je ne pense pas que les regrouper dans un même script change grand chose, mais tu peux toujours essayer.
Pi3 + DD PiDrive - RFXtrx433 - AEON Labs ZW090
Wolfen38
Messages : 1983
Inscription : 19 oct. 2017, 14:49

Re: Optimisation de script

Message par Wolfen38 »

Ok, bah j'ai du boulot.

Je pensais que pour le systeme c’était moins lourd de lire 20 petits scripts qu'un énorme.
Wolfen38
Messages : 1983
Inscription : 19 oct. 2017, 14:49

Re: Optimisation de script

Message par Wolfen38 »

Par contre, j'avais des erreurs, il me demandait des end comme celà, ca te surprend (un end avant chaque elseif) ?

Code : Tout sélectionner

commandArray = {}
 
if devicechanged['Interrupteur salon'] == '1 Click' then
if  otherdevices['Virtuel salle manger'] == 'Off' then
	commandArray['Sonoff-salon']='On'
	commandArray['Virtuel salle manger']='On'
	commandArray['Interrupteur salon']='Set Level: 40'
	else
	commandArray['Sonoff-salon']='Off'
	commandArray['Virtuel salle manger']='Off'
	commandArray['Interrupteur salon']='Set Level: 40'
	end
elseif devicechanged['Interrupteur salon'] == '2 Click' then
if otherdevices['Virtuel salon'] == 'Off' then
    commandArray['Group:Lumiere violet salon']='On'
    commandArray['Virtuel salon']='On'
    commandArray['Interrupteur salon']='Set Level: 40'
	else
	commandArray['Group:Lumiere violet salon']='Off'
	commandArray['Virtuel salon']='Off'
	commandArray['Interrupteur salon']='Set Level: 40'
	end
elseif devicechanged['Interrupteur salon'] == '3 Click' then
if otherdevices['Virtuel salon'] == 'Off' then
    commandArray['Group:Lumiere blanche salon']='On'
	commandArray['Virtuel salon']='On'
	commandArray['Interrupteur salon']='Set Level: 40'
	else
	commandArray['Group:Lumiere blanche salon']='Off'
	commandArray['Virtuel salon']='Off'
	commandArray['Interrupteur salon']='Set Level: 40'
	end
	end	
	 
return commandArray
denis_brasseur
Messages : 898
Inscription : 24 déc. 2018, 17:05
Localisation : (26)

Re: Optimisation de script

Message par denis_brasseur »

Avec un code bien indenté, tu vois que le end ferme bien le if précédent, ta syntaxe est correcte

Code : Tout sélectionner

commandArray = {}
 
if devicechanged['Interrupteur salon'] == '1 Click' then
    if  otherdevices['Virtuel salle manger'] == 'Off' then
        commandArray['Sonoff-salon']='On'
        commandArray['Virtuel salle manger']='On'
        commandArray['Interrupteur salon']='Set Level: 40'
    else
        commandArray['Sonoff-salon']='Off'
        commandArray['Virtuel salle manger']='Off'
        commandArray['Interrupteur salon']='Set Level: 40'
    end
elseif devicechanged['Interrupteur salon'] == '2 Click' then
    if otherdevices['Virtuel salon'] == 'Off' then
        commandArray['Group:Lumiere violet salon']='On'
        commandArray['Virtuel salon']='On'
        commandArray['Interrupteur salon']='Set Level: 40'
    else
        commandArray['Group:Lumiere violet salon']='Off'
        commandArray['Virtuel salon']='Off'
        commandArray['Interrupteur salon']='Set Level: 40'
    end
elseif devicechanged['Interrupteur salon'] == '3 Click' then
    if otherdevices['Virtuel salon'] == 'Off' then
        commandArray['Group:Lumiere blanche salon']='On'
        commandArray['Virtuel salon']='On'
        commandArray['Interrupteur salon']='Set Level: 40'
    else
        commandArray['Group:Lumiere blanche salon']='Off'
        commandArray['Virtuel salon']='Off'
        commandArray['Interrupteur salon']='Set Level: 40'
    end
end 
     
return commandArray
Pi3 + DD PiDrive - RFXtrx433 - AEON Labs ZW090
Répondre