====== Liste des scripts LUA ====== \\ === 1- Modifier la couleur d’une lumière Led en fonction de la température de la pièce === //By captainigloo// **But :** Script permettant de modifier la couleur d’une led RGB en fonction de la température de la pièce -- Title: Set RGB light output depending on Temperature -- -- Description : -- Set white color if temp is < 10°C -- Set light blue color if temps is between 10 and 15 -- Set dark blue color if temp is between 15 and 19 -- Set green color if temp is between 19 and 22 -- Set Yellow color ir temp is between 22 and 25 -- Set Orange color if temp is between 25 and 28 -- Set Red color if temp is greater than 28 -- -- Inputs -- * input_4 : N° entrée Temperature -- Output -- * output_6 : N° Sortie RGB light -- Events -- This script may be launched on input_4 changes local temp = calaos:getInputValue("input_4") if temp < 10.0 then --blanc str = "set 0x60FFFF" elseif temp >= 10.0 and temp < 15.0 then --bleu clair str = "set 0x28FFFF" elseif temp >= 15.0 and temp < 19.0 then --bleu foncé str = "set 0x0060FF" elseif temp >= 19.0 and temp < 22.0 then -- vert str = "set 0x28FF28" elseif temp >= 22.0 and temp < 25.0 then -- Jaune str = "set 0x7AFF00" elseif temp >= 25.0 and temp < 28.0 then -- Orange str = "set 0xFFFF00" elseif temp > 28.0 then -- Rouge str = "set 0xFF3D00" end calaos:setOutputValue("output_6", str) return true \\ === 2- Envoyer un SMS via Free suivant un évènement donné === //By captainigloo// **But :** Script permettant d’envoyer un message par SMS en fonction d’une condition. Attention ne fonctionne qu’avec free. Voir le tuto complet ici : [[fr:calaos_tuto_free_sms|Utilisation du systeme de notifications par sms de free]] function urlencode(str) if (str) then str = string.gsub (str, "\n", "\r\n") str = string.gsub (str, "([^%w ])", function (c) return string.format ("%%%02X", string.byte(c)) end) str = string.gsub (str, " ", "%%20") end return str end local user = "username" local pass = "password" local msg = "le message à envoyer en fonction de la règle" calaos:requestUrl("https://smsapi.free-mobile.fr/sendmsg?user="..user.."&pass="..pass.."&msg="..urlencode(msg)) return true \\ === 3- Gestion d'un chauffage=== //By Raoulh// **But :** Script permettant de gérer le chauffage en fonction de la température des panneaux solaires du ballon EC et du plancher. print("script chauffage start") local temp_panneaux = calaos:getInputValue("input_temp_3") local temp_ballonh = calaos:getInputValue("input_temp_0") local temp_plancher = calaos:getInputValue("input_temp_2") local consigne = calaos:getInputValue("intern_10") local consigne_ballon = calaos:getInputValue("intern_1") local consigne_plancher = calaos:getInputValue("intern_0") if temp_panneaux >= (temp_ballonh + consigne) then -- active pompe solaire si < max if temp_ballonh < 70 then calaos:setOutputValue("output_chauff_2", true) else calaos:setOutputValue("output_chauff_2", false) end -- force stop elect resistance calaos:setOutputValue("output_chauff_1", false) active_elec = false end -- Arret de la pompe solaire si temperature panneau insuffisante if temp_panneaux <= temp_ballonh then -- stop pompe solaire calaos:setOutputValue("output_chauff_2", false) active_elec = true end -- start/stop elec si consigne et active elec if active_elec then if temp_ballonh <= consigne_ballon then calaos:setOutputValue("output_chauff_1", true) end if temp_ballonh > (consigne_ballon + 1) then calaos:setOutputValue("output_chauff_1", false) end end -- start/stop plancher pompe si consigne if temp_plancher <= consigne_plancher then calaos:setOutputValue("output_chauff_0", true) end if temp_plancher > (consigne_plancher + 1) then calaos:setOutputValue("output_chauff_0", false) end -- pas besoin d'action, toujours retourner false return false \\ === 4- Gestion de l'alimentation d'un transfo de rubans leds === //By Raoulh & Eric64// **But :** Script permettant de gérer la mise en route d'un transformateur 220v/24v pour l'allumage de led. **Précisions :** plusieurs cordons leds sont allimentés par le même transfo. Nous souhaitons que le transfo soit éteint lorsqu'aucune led n'est allumée et pouvoir allumer ou éteindre le transfo lorsque le système détecte un changement d'état d'une led. **A noter :** Calaos V2 ne gère pas les rubans led RGBW. Il faut donc créer 2 lumières DMX, une pour le RGB l'autre pour le W. __A créer dans CALAOS INSTALLER :__ - IO inter_booleen. "active_bloc" : Elle va nous permettre de signaler un changement d'état des leds - lumière : cette lumière est "fictive". Elle sert __Règles à créer pour chaque lumière :__ SI RGBplafond == changed ALORS active_bloc = toggle __Règle pour gérer le transfo :__ SI active_bloc = changed ALORS exécution du script __Script :__ local RGBplafond = calaos:getOutputValue("output_55") -- lumière RGB du plafond local Wplafond = calaos:getOutputValue("output_72") -- lumière W du plafond local RGBmeuble = calaos:getOutputValue("output_73") -- lumière RGB des meubles local Wmeuble = calaos:getOutputValue("output_74") -- lumière W des meubles local RGBplinthes = calaos:getOutputValue("output_75") -- lumière RGB des plinthes local Wplinthes = calaos:getOutputValue("output_76") -- lumière W des plinthes -- SI une lumière est différente de 0 (donc n'est pas éteinte) if RGBplafond ~= "0" or Wplafond ~= "0" or RGBmeuble ~= "0" or Wmeuble ~= "0" or RGBplinthes ~= "0" or Wplinthes ~= "0" then -- ALORS allume le bloc calaos:setOutputValue("output_81", true) -- output_81 correspond à la lumière "Fictive" et donc au relais du transfo else -- SINON coupe le bloc calaos:setOutputValue("output_81", false) end return true \\ === 5- Conserver la valeur d'un appui sur un BP dans une variable === //By eric64// **But :** Script permettant de donner une valeur True ou False à une variable lors de l'appui sur un BP pour allumer/éteindre des lumières. Certaines lumières de mon entrée sont pilotées par un BP (pour éclairer le temps voulu) et un détecteur de mouvement (pour éclairer juste le temps du passage). Je veux que les actions du détecteur de mouvement soient bloquées lorsque j'allume les lumières depuis le BP. Lorsque j'allume par le BP la variable passe à True. J'ai donc ajouté la condition variable == false sur les actions du détecteur de mouvement. local Variable_Activation_Inter = calaos:getInputValue("intern_7") -- je déclare ma variable if Variable_Activation_Inter == false then -- si ma variable est fausse (je n'ai donc pas allumé les lumières par BP) calaos:setOutputValue("output_78", set 0xFFFF00) -- alors j'allume ma lumière RGB en orange calaos:setOutputValue("output_54", true) -- + 2 spots au sol calaos:setOutputValue("output_14", true) -- + 1 spot plafond calaos:setOutputValue("intern_7", true) -- et je modifie l'état de ma variable pour la mettre à vrai elseif Variable_Activation_Inter == true then -- sinon si ma variable est vrai (j'avais déjà donc allumé mes lumières par BP) calaos:setOutputValue("output_78", false) -- alors j'éteins toutes mes lumières calaos:setOutputValue("output_54", false) calaos:setOutputValue("output_14", false) calaos:setOutputValue("intern_7", false) -- et je remets ma variable à faux end return true \\ === 6- Détection d'une situation "panique" === //By mifrey// **But :** Script permettant de détecter une situation "panique" dans laquelle un poussoir est actionné plusieurs fois pendant un certain temps. **Précisions :** Le script doit être mis dans les conditions d'une règle et être déclenché par tous les poussoirs voulus (je les ai mis tous chez moi). Il retourne true en cas de situation panique. Dans l'exemple, il retourne true si les poussoirs sont pressés 6 fois (12 changements d'etat) dans les 2 secondes (à définir dans le timer). Les actions sont à dénifir dans la règle, par exemple allumer toute les lumières, envoyer un mail ou déclencher l'alarme, etc. Afin de d'analyser si le script a bien fonctionné, on peut analyser les logs de Calaos-Server : root@raspberrypi:~# journalctl -lu calaos-server --since 00:16 --no-pager | grep PANIC Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: Start Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: timer = true Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: counter = 0 Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: Max time exceeded, restart timer and counter. Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: Set timer = start Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: Set counter = 1 Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: End Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: Start Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: timer = false Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: counter = 1 Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: Max time not exceeded, count up. Set counter = 2 Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: Counts not reached, wait for the next count. Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: end Nov 11 00:16:46 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: Start ... __A créer dans CALAOS INSTALLER :__ - Un timer (type InputTimer) : Pour mesurer le temps depuis la première pression sur le poussoir. Le timer est à régler sur quelques secondes. - Un compteur (type InternalInt) : Pour compter le nombre de fois qu'un poussoir est actionné. __Script :__ -- Detect a panic situation in which switches are pressed several times within a short time. -- The script shall be called each time the state of a switch changes. --if true then return false end -- Uncomment to disable the script -- Script start local script_name = "SCRIPT_CONDITIONS_PANIC" print(script_name .. ": Start") -- IOs id local timer_id = "input_58" -- InputTimer type local counter_id = "intern_0" -- InternalInt type -- Other variables to define local panic_count = 12 -- Number of times the state of a switch must change within a specific time (typically 2 seconds) to trigger a panic situation -- Read IOs local timer = calaos:getInputValue(timer_id) -- ATTENTION: getInputValue(InputTimer type) returns a string type. Timer is false when timer starts or stops, true when timer is done. local counter = calaos:getInputValue(counter_id) print(script_name .. ": timer = " .. tostring(timer)) print(script_name .. ": counter = " .. tostring(counter)) -- Max time exceeded or first count if timer == "true" or counter == 0 then print(script_name .. ": Max time exceeded, restart timer and counter.") -- Restart timer timer = "start" calaos:setOutputValue(timer_id, timer) print(script_name .. ": Set timer = " .. tostring(timer)) -- First count counter = 1; calaos:setOutputValue(counter_id, counter) print(script_name .. ": Set counter = " .. tostring(counter)) print(script_name .. ": End") return false -- Max time not exceeded else -- Count up counter = counter + 1 calaos:setOutputValue(counter_id, counter) print(script_name .. ": Max time not exceeded, count up. Set counter = " .. tostring(counter)) -- Counts not reached, wait for the next count. if counter < panic_count then print(script_name .. ": Counts not reached, wait for the next count.") print(script_name .. ": end") return false -- Counts reached, panic mode. else print(script_name .. ": Counts reached, panic mode.") -- Stop timer timer = "stop" calaos:setOutputValue(timer_id, timer) print(script_name .. ": Set timer = " .. tostring(timer)) -- Reset counter counter = 0; calaos:setOutputValue(counter_id, counter) print(script_name .. ": Set counter = " .. tostring(counter)) print(script_name .. ": end") return true end end print(script_name .. ": Oups, should never be there...") return false \\ === 7- Simulateur de présence === //By mifrey// **But :** Script permettant de simuler une présence en allumant/éteignant des lampes dans une certaine séquence (par exemple on sort du salon, on va dans la salle de bain puis dans la chambre) avec des durées aléatoires. **Règle :** Le script doit être mis dans les conditions d'une règle et être déclenché par le changement des IOs "Enable", "Evening" et "Timer". Rien dans les actions. **Précisions :** Le script correspond à la machine d'état suivante {{:fr:state_machine.pdf|}} {{:fr:state_machine.odp|}}. C'est une machine d'état un peu particulière car il n'y a un qu'une seule séquence d'états possible (l'état n+1 suivra toujours l'état n). Pour adapter le script à votre situation, il faut modifier les variables définissant les adresses de vos lampes puis modifier la machine d'état. Pour tester la séquence sans devoir attendre le couché du soleil, il faut mettre la variable %%"script_test_enable"%% à true puis activer le simulateur avec l'IO "Enable". La séquence se déroulera avec un changement d'état toutes les 5 secondes. Afin de d'analyser si le script a bien fonctionné, on peut analyser les logs de Calaos-Server : root@raspberrypi:~# journalctl -lu calaos-server --since 12:00 --no-pager | grep PRESENCE_SIMULATOR Oct 14 12:01:59 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: Start Oct 14 12:01:59 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: State=0 Oct 14 12:01:59 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: End Oct 14 12:02:02 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: Start Oct 14 12:02:02 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: State=1 Oct 14 12:02:02 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: Set timer = 0:0:16:803 Oct 14 12:02:02 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: End Oct 14 12:02:07 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: Start Oct 14 12:02:07 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: State=2 Oct 14 12:02:07 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: Set timer = 0:12:37:894 Oct 14 12:02:07 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: End Oct 14 12:02:12 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: Start Oct 14 12:02:12 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: State=3 Oct 14 12:02:12 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: Set timer = 0:19:10:782 Oct 14 12:02:12 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: End ... __A créer dans CALAOS INSTALLER :__ - Une variable interne booléenne (type InternalBool) "Enable" : Pour activer/désactiver la fonction simulateur de présence depuis Calaos Home. - Une plage horaire (type InPlageHoraire) "Evening" : Qui passe à true vers l'heure du couché du soleil qui passe à false vers l'heure d'aller au lit. L'idéal est d'avoir des heures légèrement différentes pour chaque jour de façon à ce que le simulateur ne démarre pas à la même heure chaque jour. - Un timer (type InputTimer) "Timer" : Pour passer d'un état à l'autre. - Une variable interne entière (type InternalInt) "State" : Pour retenir l'état dans lequel se trouve la machine d'état. __Script :__ -- Presence simulator -- -- Rule conditions: -- This script -- -- Script triggers: -- enable_id -- evening_time_range_id -- timer_id -- -- Rule actions: -- None --if true then return false end -- Uncomment to disable the script -- Script start local script_name = "SCRIPT_CONDITIONS_PRESENCE_SIMULATOR" print(script_name .. ": Start") -- IOs defined in Calaos Installer local enable_id = "intern_3" -- InternalBool type, to be set/clear from Calaos Home or Mobile to enable/disable the presence simulator local evening_time_range_id = "input_91" -- InPlageHoraire type, to be configured in Calaso Installer to get true around sunset and get false around bedtime local timer_id = "input_90" -- InputTimer type local state_id = "intern_2" -- InternalInt type local HJ_BAL_id = "output_9" -- WODigital type, staircase lamps local SAL_L1_id = "output_7" -- WODigital type, living room lamp local HJ_L1L2_id = "output_8" -- WODigital type, 1sth floor all lamps local HN_L1_id = "output_19" -- WODigital type, 2nd floor hall lamps local SDB_L1_id = "output_28" -- WODigital type, bathroom lamp local DR_L1_id = "output_25" -- WODigital type, dressing room lamp local CH1_L1_id = "output_21" -- WODigital type, parents bedroom lamp -- Other variables local script_test_enable = false -- Set to true to test the script with a short time between each state change, e.g., 5 seconds local script_test_timer_duration = "00:00:05:000" -------------------------------------------------------------------------------- -- Functions -------------------------------------------------------------------------------- -- Split a string (from http://lua-users.org/wiki/SplitJoin) function string:split(sSeparator, nMax, bRegexp) assert(sSeparator ~= '') assert(nMax == nil or nMax >= 1) local aRecord = {} if self:len() > 0 then local bPlain = not bRegexp nMax = nMax or -1 local nField, nStart = 1, 1 local nFirst,nLast = self:find(sSeparator, nStart, bPlain) while nFirst and nMax ~= 0 do aRecord[nField] = self:sub(nStart, nFirst-1) nField = nField+1 nStart = nLast+1 nFirst,nLast = self:find(sSeparator, nStart, bPlain) nMax = nMax-1 end aRecord[nField] = self:sub(nStart) end return aRecord end -- Convert a time (format "h:m:s:ms") to ms function time_to_ms (t) local ms = 0 for k,x in next, string.split(t, ":") do if k == 1 then ms = ms + x*60*60*1000 elseif k == 2 then ms = ms + x*60*1000 elseif k == 3 then ms = ms + x*1000 elseif k == 4 then ms = ms + x end end return ms end -- Convert ms to a time (format "h:m:s:ms") function ms_to_time (ms) local mi = math.floor(ms % 1000); local s = math.floor((ms/1000) % 60); local m = math.floor((ms/(1000*60)) % 60); local h = math.floor((ms/(1000*60*60)) % 24); return (h .. ":" .. m .. ":" .. s .. ":" .. mi) end -- Set the new state number. Used in every state change. function state_change (st) state = st calaos:setOutputValue(state_id, state) print(script_name .. ": State=" .. state) end -- Set the timer for the next state change. A random time is generated between low and high values (format "h:m:s:ms"). Used in every state change. function timer_set (low, high) -- Generate a random time local ms_low = time_to_ms(low) local ms_high = time_to_ms(high) math.randomseed(os.time()) local ms = math.random(ms_low, ms_high) local t = ms_to_time(ms) -- Set and start the timer for the next state print(script_name .. ": Set timer = " .. t) calaos:setOutputValue(timer_id, t) calaos:setOutputValue(timer_id, "start") -- Script test if script_test_enable == true then calaos:setOutputValue(timer_id, script_test_timer_duration) end print(script_name .. ": End") end -------------------------------------------------------------------------------- -- State machine -------------------------------------------------------------------------------- -- Get initial values local timer = calaos:getInputValue(timer_id) -- ATTENTION: getInputValue(InputTimer type) returns a string type local evening = calaos:getInputValue(evening_time_range_id) local enable = calaos:getInputValue(enable_id) local state = calaos:getInputValue(state_id) -- Overwrite values if testing the script if script_test_enable == true then if state < 4 then evening = true else evening = false end end -- Simulator reset if state == 14 or enable == false then state_change (0) -- Timer reset calaos:setOutputValue(timer_id, "stop") print(script_name .. ": End") return false end -- Staircase: switch on if state == 0 and evening == true then state_change (1) calaos:setOutputValue(HJ_BAL_id, true) timer_set("00:00:5:000", "00:00:40:000") return false end -- Living room: switch on if state == 1 and timer == "true" then state_change (2) calaos:setOutputValue(SAL_L1_id, true) timer_set("00:10:00:000", "00:30:00:000") return false end -- Hall 1: switch on if state == 2 and timer == "true" then state_change (3) calaos:setOutputValue(HJ_L1L2_id, true) timer_set("00:04:00:000", "00:40:00:000") return false end -- Hall 1: switch off if state == 3 and timer == "true" then state_change (4) calaos:setOutputValue(HJ_L1L2_id, false) timer_set("00:00:05:000", "00:00:05:000") return false end -- Living room: switch off if state == 4 and timer == "true" and evening == false then state_change (5) calaos:setOutputValue(SAL_L1_id, false) timer_set("00:00:05:000", "00:00:10:000") return false end -- Hall 2: switch on if state == 5 and timer == "true" then state_change (6) calaos:setOutputValue(HN_L1_id, true) timer_set("00:00:04:000", "00:00:08:000") return false end -- Straircase: switch off if state == 6 and timer == "true" then state_change (7) calaos:setOutputValue(HJ_BAL_id, false) timer_set("00:00:02:000", "00:00:08:000") return false end -- Bathroom: switch on if state == 7 and timer == "true" then state_change (8) calaos:setOutputValue(SDB_L1_id, true) timer_set("00:02:00:000", "00:09:00:000") return false end -- Bathroom: switch off if state == 8 and timer == "true" then state_change (9) calaos:setOutputValue(SDB_L1_id, false) timer_set("00:00:10:000", "00:00:20:000") return false end -- Hall 2: switch off if state == 9 and timer == "true" then state_change (10) calaos:setOutputValue(HN_L1_id, false) timer_set("00:00:02:000", "00:00:04:000") return false end -- Dressing: switch on if state == 10 and timer == "true" then state_change (11) calaos:setOutputValue(DR_L1_id, true) timer_set("00:00:03:000", "00:00:10:000") return false end -- Dressing: switch off if state == 11 and timer == "true" then state_change (12) calaos:setOutputValue(DR_L1_id, false) timer_set("00:00:02:000", "00:00:05:000") return false end -- Bedroom 1: switch on if state == 12 and timer == "true" then state_change (13) calaos:setOutputValue(CH1_L1_id, true) timer_set("00:02:00:000", "00:15:00:000") return false end -- Bedroom 1: switch off if state == 13 and timer == "true" then state_change (14) calaos:setOutputValue(CH1_L1_id, false) timer_set("00:00:05:000", "00:00:05:000") return false end print(script_name .. ": Did nothing") return false