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
Last revision Both sides next revision
fr:liste_script [2016/07/28 14:14]
mifrey Script 6 + coloration syntaxique
fr:liste_script [2016/10/14 12:32]
mifrey
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 295: Line 295:
  
  
 +
 +\\ === 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 --no-pager | grep SCRIPT_CONDITIONS_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>