Petit script de présence en LUA

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 <\>.
Répondre
Seidel76
Messages : 10
Inscription : 18 mars 2020, 02:02

Petit script de présence en LUA

Message par Seidel76 »

Bonjour à tous, voilà j'y connais pas grand chose en LUA, en fait je souhaiterais changer mon device principal "Presence" lorsque "Presence Watch" et "Presence iPhone" est à Off ( Script utilisant la géolocalisation de l'iPhone via iCloud mais qui n'est pas ultra précis ) et avec un petit script qui tourne sur mon Pi 4, qui détecte le wifi et le bluetooth de ceux ci, et qui met une variable à 1 ou 0 suivant la détection...

Voilà ce que j'ai tenté de faire, mais rien ne marche forcément !

Code : Tout sélectionner

commandArray = {}
if (otherdevices['Presence'] == "On") then
        if (otherdevices['Presence Watch']=='Off') then
            if (otherdevices['Presence iPhone']=='Off') then
                if ( uservariables["Varriable iPhone"] == "0" ) then
                    if ( uservariables["Variable Watch"] == "0" ) then
                        print ("Presence Off")
                        commandArray['Presence'] = 'Off'
                    end
                end
            end
        end
end
return commandArray
Je l'avais fait en Blockly aussi, mais apparemment les variables ne sont pas prises en compte...

Image

Merci pour vos avis !
denis_brasseur
Messages : 898
Inscription : 24 déc. 2018, 17:05
Localisation : (26)

Re: Petit script de présence en LUA

Message par denis_brasseur »

Tu peux largement simplifier ton script, mais le probleme n'est pas là.
Que dise tes log dans l'état de ton script ?
Commence par afficher l'état de tes 3 devices et 2 variables afin de comprendre ce qui se passe.

Code : Tout sélectionner

print(otherdevices['Presence'])
par exemple.
En l'absence de devicechanged, sI ton script est en mode device, il va s’exécuter à chaque changement d'état d'un device, en mode time, il va s'éxécuter par défaut toute les minutes.
'Varriable iPhone' : variable avec 2 r, c'est bien le nom de ta variable ?
Pi3 + DD PiDrive - RFXtrx433 - AEON Labs ZW090
Seidel76
Messages : 10
Inscription : 18 mars 2020, 02:02

Re: Petit script de présence en LUA

Message par Seidel76 »

J'avoue que mon code est très très moche et qu'il y a moyen de l'améliorer... Mais déjà ça serait cool qui fonctionne !

Sinon voici l'état des devices:

Code : Tout sélectionner

2020-04-23 17:42:04.441 Status: LUA: Presence: On
2020-04-23 17:42:04.441 Status: LUA: Presence iPhone: Off
2020-04-23 17:42:04.441 Status: LUA: Presence Watch: Off
2020-04-23 17:42:04.441 Status: LUA: Variable iPhone: 0
2020-04-23 17:42:04.441 Status: LUA: Variable Watch: 0
Mais rien ne fonctionne

PS: Oui oui pour la variable c'est normal, me suis planté à la création et j'ai pas rectifié ^^
Seidel76
Messages : 10
Inscription : 18 mars 2020, 02:02

Re: Petit script de présence en LUA

Message par Seidel76 »

Je me répond à moi même, il ne fallait faire qu'un seul devicechanged.

Voici au final ce que ça donne:

Code : Tout sélectionner

commandArray = {}

if (otherdevices['Presence'] == "On") then
--    print("Presence On")
        if (devicechanged['Presence iPhone']=='Off') then
--            print("Presence iPhone Off")
            if (otherdevices['Presence Watch']=='Off') then
--                print("Presence Watch Off")
                if ( uservariables["Varriable iPhone"] == 0 ) then
--                    print("Variable iPhone à 0")
                    if (uservariables["Variable Watch"] == 0 ) then
--                        print("Variable Watch à 0")
                        print ("Presence Off")
                        commandArray['Presence']='Off'
                    end
                end
            else
                if (otherdevices['Presence Watch']=='On') then
--                print("Presence Watch Off")
                    if ( uservariables["Varriable iPhone"] == 0 ) then
--                    print("Variable iPhone à 0")
                        print ("Presence Off")
                        commandArray['Presence']='Off'
                    end
                end
            end
        end
end
motionDetected = (otherdevices['Presence iPhone'] == 'On' or uservariables["Varriable iPhone"] == 0) 
if ( motionDetected and otherdevices['Presence'] == "Off") then
    print ("Presence On")
    commandArray['Presence']='On'
end
return commandArray
Par contre si quelqu'un veut bien me l'améliorer, je suis preneur !
denis_brasseur
Messages : 898
Inscription : 24 déc. 2018, 17:05
Localisation : (26)

Re: Petit script de présence en LUA

Message par denis_brasseur »

Ca peux donner quelque chose comme ceci, l'amélioration n'étant que visuel :

Code : Tout sélectionner

commandArray = {}

if otherdevices['Presence'] == "On" and devicechanged['Presence iPhone']=='Off' then
	if uservariables["Varriable iPhone"] == 0 and ((otherdevices['Presence Watch']=='Off' and uservariables["Variable Watch"] == 0) or otherdevices['Presence Watch']=='On') then
		print ("Presence Off")
		commandArray['Presence']='Off'
	end
end
if (otherdevices['Presence iPhone'] == 'On' or uservariables["Varriable iPhone"] == 0) and otherdevices['Presence'] == "Off" then
    print ("Presence On")
    commandArray['Presence']='On'
end
return commandArray
Pi3 + DD PiDrive - RFXtrx433 - AEON Labs ZW090
boum
Messages : 233
Inscription : 18 janv. 2019, 11:34
Localisation : France

Re: Petit script de présence en LUA

Message par boum »

Allez, juste pour le fun, la curiosité et pour comparer, une version dzVents (non testée et avec les mêmes tests logiques) :

Code : Tout sélectionner

return {
  on = {
	devices = { 'Presence iPhone', 'Presence Watch', }
	variables = { 'Varriable iPhone', 'Variable Watch', },
  },

  execute = function(domoticz, item)
    local presenceIphone = domoticz.devices('Presence iPhone').state
    local variableIphone = domoticz.variables('Varriable iPhone').value

    local presence = domoticz.devices('Presence')
    if presence.state == 'Off' then
      if presenceIphone == 'On' or variableIphone == 0 then
        print('Presence On')
        presence.switchOn()
      end
    else -- presence.state == 'On'
      if presenceIphone == 'Off' and variableIphone == 0 then
        local presenceWatch = domoticz.devices('Presence Watch').state
        local variableWatch = domoticz.variables('Variable Watch').value
        if not (presenceWatch == 'Off' and variableWatch ~= 0) then
          print('Presence Off')
          presence.switchOff()
        end
      end
    end
  end
}
Domoticz v2022.2 sur linux-mint / Z-Wave-JS / RFXCom / Zigbee2MQTT
Seidel76
Messages : 10
Inscription : 18 mars 2020, 02:02

Re: Petit script de présence en LUA

Message par Seidel76 »

Ah bah parfait ! Merci les gars je vais voir ça e me lancer dans LUA, car j'ai que des evenements en Blockly et des fois c'est pas terrible...
Seidel76
Messages : 10
Inscription : 18 mars 2020, 02:02

Re: Petit script de présence en LUA

Message par Seidel76 »

Du coup je suis en train de refaire tous mes blockly en LUA, mais je galère avec une notification de batterie, je vois pas où ça bloque.

Voici mon code:

Code : Tout sélectionner

commandArray = {}
function round(num, numDecimalPlaces)
  local mult = 10^(numDecimalPlaces or 0)
  return math.floor(num * mult + 0.5) / mult
end
batterie_watch = tonumber(otherdevices_svalues['Batterie Watch']:match("([^;]+)"))
batterie_watch = round(batterie_watch,0)
print (batterie_watch)

if (devicechanged['Batterie Watch']) then
    print ("Verification Watch")
	if (batterie_watch == "100.0" and otherdevices_idx['Etat batterie Watch'] == "Charged" and uservariables["Notification Watch"] ~= 1) then
     commandArray['SendNotification']='iCloud#Votre Apple Watch est entièrement rechargée !#0###pushover'
     commandArray['Variable:Notification Watch'] = '1'
     print("Variable Watch à 1")
    elseif batterie_watch == "7.0" and uservariables["Notification Watch"] ~= 2 then
     commandArray['SendNotification']='iCloud#Votre Apple Watch est à 10% de batterie !#0###pushover'
     commandArray['Variable:Notification Watch'] = '2'
     print("Variable Watch à 2")
     elseif batterie_watch == "5.0" and uservariables["Notification Watch"] ~= 3 then
     commandArray['SendNotification']='iCloud#Votre Apple Watch est à 5% de batterie !#0###pushover'
     commandArray['Variable:Notification Watch'] = '3'
     print("Variable Watch à 3")
     elseif batterie_watch == "1.0" and uservariables["Notification Watch"] ~= 4 then
     commandArray['SendNotification']='iCloud#Votre Apple Watch est à 1% de batterie !#0###pushover'
     commandArray['Variable:Notification Watch'] = '4'
     print("Variable Watch à 4")
     end
end
return commandArray
Ça affiche bien ma batterie: 7.0 par exemple
Mais jamais "Verification Watch", à croire que ca merde au niveau de if (devicechanged['Batterie Watch']), sachant que la valeur est en pourcentage et mise à jour toutes les 2 minutes...
denis_brasseur
Messages : 898
Inscription : 24 déc. 2018, 17:05
Localisation : (26)

Re: Petit script de présence en LUA

Message par denis_brasseur »

Seidel76 a écrit : 28 avr. 2020, 16:41
Ça affiche bien ma batterie: 7.0 par exemple
Mais jamais "Verification Watch", à croire que ca merde au niveau de if (devicechanged['Batterie Watch']), sachant que la valeur est en pourcentage et mise à jour toutes les 2 minutes...
Je crois comprendre que l'affichage de 7.0 correspond à

Code : Tout sélectionner

print (batterie_watch)
Dans ton code, if (devicechanged['Batterie Watch']) correspond à un changement du niveau de batterie.
Tant que la valeur de batterie_watch n'est pas est égale à 7.0, pas de changement, donc pas dans la boucle.
De plus, avec un condition comme if batterie_watch == "5.0", si au changement d'état de la batterie, la valeur est de 4.9, ta condition ne sera pas respectée.

Pas sur que la condition

Code : Tout sélectionner

if (devicechanged['Batterie Watch']) then
soit adapter à ton besoin, et mieux vaut comparer à une valeur strictement numerique

Code : Tout sélectionner

 if batterie_watch >= 5 and batterie_watch <= 7 
Beaucoup (trop) de strictes conditions à respecter dans ton code

Code : Tout sélectionner

elseif batterie_watch == "7.0" and uservariables["Notification Watch"] ~= 2 then
Pi3 + DD PiDrive - RFXtrx433 - AEON Labs ZW090
Seidel76
Messages : 10
Inscription : 18 mars 2020, 02:02

Re: Petit script de présence en LUA

Message par Seidel76 »

En effet je me suis largement compliqué la tâche...

Voici mon script final:

Code : Tout sélectionner

commandArray = {}
function round(num, numDecimalPlaces)
  local mult = 10^(numDecimalPlaces or 0)
  return math.floor(num * mult + 0.5) / mult
end
batterie_watch = tonumber(otherdevices_svalues['Batterie Watch']:match("([^;]+)"))
batterie_watch = round(batterie_watch,0)
--print (batterie_watch)

	if batterie_watch == 100 and uservariables["Notification Watch"] ~= 1 then
     commandArray['SendNotification']='iCloud#Votre Apple Watch est entièrement rechargée !#0###pushover'
     commandArray['Variable:Notification Watch'] = '1'
     print("Variable Watch à 1")
     end
     if batterie_watch == 10 and uservariables["Notification Watch"] ~= 2 then
     commandArray['SendNotification']='iCloud#Votre Apple Watch est à 10% de batterie !#0###pushover'
     commandArray['Variable:Notification Watch'] = '2'
     print("Variable Watch à 2")
     end
     if batterie_watch == 5 and uservariables["Notification Watch"] ~= 3 then
     commandArray['SendNotification']='iCloud#Votre Apple Watch est à 5% de batterie !#0###pushover'
     commandArray['Variable:Notification Watch'] = '3'
     print("Variable Watch à 3")
     end
     if batterie_watch == 1 and uservariables["Notification Watch"] ~= 4 then
     commandArray['SendNotification']='iCloud#Votre Apple Watch va bientôt s\'éteindre !#0###pushover'
     commandArray['Variable:Notification Watch'] = '4'
     print("Variable Watch à 4")
     end

if (otherdevices['Etat batterie Watch'] == "Charging" and uservariables["Notification Watch"] ~= 0) then
     commandArray['Variable:Notification Watch'] = '0'
     print("Variable Watch à 0")
end
return commandArray
Merci denis_brasseur ! :D
Répondre