Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
fr:liste_script [2015/11/19 09:50]
eric64
fr:liste_script [2016/11/11 00:25] (current)
mifrey Correction script panique
Line 1: Line 1:
-====== 3- Liste des scripts LUA ======+====== Liste des scripts LUA ======
  
 \\ === 1- Modifier la couleur d’une lumière Led en fonction de la température de la pièce === \\ === 1- Modifier la couleur d’une lumière Led en fonction de la température de la pièce ===
Line 6: Line 6:
 **But :** Script permettant de modifier la couleur d’une led RGB en fonction de la température de la pièce **But :** Script permettant de modifier la couleur d’une led RGB en fonction de la température de la pièce
  
-<code>+<code lua>
 -- Title: Set RGB light output depending on Temperature -- Title: Set RGB light output depending on Temperature
 --  -- 
Line 60: Line 60:
  
  
-<code>+<code lua>
 function urlencode(str) function urlencode(str)
    if (str) then    if (str) then
Line 88: Line 88:
 **But :** Script permettant de gérer le chauffage en fonction de la température des panneaux solaires du ballon EC et du plancher. **But :** Script permettant de gérer le chauffage en fonction de la température des panneaux solaires du ballon EC et du plancher.
  
-<code>+<code lua>
 print("script chauffage start") print("script chauffage start")
  local temp_panneaux = calaos:getInputValue("input_temp_3")  local temp_panneaux = calaos:getInputValue("input_temp_3")
Line 153: Line 153:
  
  
-__A créer dans CALAOS_INSTALLER :__+__A créer dans CALAOS INSTALLER :__ 
 +<code>
 - IO inter_booleen. "active_bloc" : Elle va nous permettre de signaler un changement d'état des leds - 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  - lumière : cette lumière est "fictive". Elle sert 
 +</code>
  
-__Règles à créer pour chaque lumières :__+__Règles à créer pour chaque lumière :__
 <code> <code>
 SI RGBplafond == changed SI RGBplafond == changed
Line 169: Line 171:
 </code> </code>
  
-__script__ +__Script :__ 
-<code>+<code lua>
  local RGBplafond = calaos:getOutputValue("output_55" -- lumière RGB du plafond  local RGBplafond = calaos:getOutputValue("output_55" -- lumière RGB du plafond
  local Wplafond = calaos:getOutputValue("output_72" -- lumière W du plafond  local Wplafond = calaos:getOutputValue("output_72" -- lumière W du plafond
Line 188: Line 190:
  end  end
  return true  return true
 +</code>
 +
 +
 +
 +\\ === 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.
 +
 +<code lua>
 +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
 +</code>
 +
 +\\ === 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 :
 +<code>
 +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
 +
 +...
 +</code>
 +
 +__A créer dans CALAOS INSTALLER :__
 +<code>
 +- 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é.
 +</code>
 +
 +__Script :__
 +<code lua>
 +-- 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
 +</code>
 +
 +
 +
 +\\ === 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 :
 +<code>
 +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
 +...
 +</code>
 +
 +
 +__A créer dans CALAOS INSTALLER :__
 +<code>
 +- 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.
 +</code>
 +
 +__Script :__
 +<code lua>
 +-- 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
 </code> </code>