Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next 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:24] mifrey |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== 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/ | ||
+ | |||
+ | **Règle :** Le script doit être mis dans les conditions d'une règle et être déclenché par le changement des IOs " | ||
+ | |||
+ | **Précisions :** Le script correspond à la machine d' | ||
+ | |||
+ | Pour adapter le script à votre situation, il faut modifier les variables définissant les adresses de vos lampes puis modifier la machine d' | ||
+ | |||
+ | Pour tester la séquence sans devoir attendre le couché du soleil, il faut mettre la variable %%" | ||
+ | |||
+ | Afin de d' | ||
+ | < | ||
+ | root@raspberrypi: | ||
+ | Oct 14 12:01:59 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: | ||
+ | Oct 14 12:01:59 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: | ||
+ | Oct 14 12:01:59 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: | ||
+ | Oct 14 12:02:02 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: | ||
+ | Oct 14 12:02:02 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: | ||
+ | Oct 14 12:02:02 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: | ||
+ | Oct 14 12:02:02 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: | ||
+ | Oct 14 12:02:07 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: | ||
+ | Oct 14 12:02:07 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: | ||
+ | Oct 14 12:02:07 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: | ||
+ | Oct 14 12:02:07 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: | ||
+ | Oct 14 12:02:12 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: | ||
+ | Oct 14 12:02:12 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: | ||
+ | Oct 14 12:02:12 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: | ||
+ | Oct 14 12:02:12 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: | ||
+ | ... | ||
+ | </ | ||
+ | |||
+ | |||
+ | __A créer dans CALAOS INSTALLER :__ | ||
+ | < | ||
+ | - Une variable interne booléenne (type InternalBool) " | ||
+ | - Une plage horaire (type InPlageHoraire) " | ||
+ | - Un timer (type InputTimer) " | ||
+ | - Une variable interne entière (type InternalInt) " | ||
+ | </ | ||
+ | |||
+ | __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 = " | ||
+ | print(script_name .. ": Start" | ||
+ | |||
+ | -- IOs defined in Calaos Installer | ||
+ | local enable_id = " | ||
+ | local evening_time_range_id = " | ||
+ | local timer_id = " | ||
+ | local state_id = " | ||
+ | |||
+ | local HJ_BAL_id = " | ||
+ | local SAL_L1_id = " | ||
+ | local HJ_L1L2_id = " | ||
+ | local HN_L1_id = " | ||
+ | local SDB_L1_id = " | ||
+ | local DR_L1_id = " | ||
+ | local CH1_L1_id = " | ||
+ | |||
+ | -- 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 = " | ||
+ | |||
+ | |||
+ | -------------------------------------------------------------------------------- | ||
+ | -- Functions | ||
+ | -------------------------------------------------------------------------------- | ||
+ | |||
+ | -- Split a string (from http:// | ||
+ | function string: | ||
+ | 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, | ||
+ | while nFirst and nMax ~= 0 do | ||
+ | aRecord[nField] = self: | ||
+ | nField = nField+1 | ||
+ | nStart = nLast+1 | ||
+ | nFirst, | ||
+ | nMax = nMax-1 | ||
+ | end | ||
+ | aRecord[nField] = self: | ||
+ | end | ||
+ | |||
+ | return aRecord | ||
+ | end | ||
+ | |||
+ | -- Convert a time (format " | ||
+ | function time_to_ms (t) | ||
+ | local ms = 0 | ||
+ | for k,x in next, string.split(t, | ||
+ | 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 " | ||
+ | function ms_to_time (ms) | ||
+ | local mi = math.floor(ms % 1000); | ||
+ | local s = math.floor((ms/ | ||
+ | local m = math.floor((ms/ | ||
+ | local h = math.floor((ms/ | ||
+ | return (h .. ":" | ||
+ | end | ||
+ | |||
+ | -- Set the new state number. | ||
+ | function state_change (st) | ||
+ | state = st | ||
+ | calaos: | ||
+ | print(script_name .. ": State=" | ||
+ | end | ||
+ | |||
+ | -- Set the timer for the next 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, | ||
+ | local t = ms_to_time(ms) | ||
+ | -- Set and start the timer for the next state | ||
+ | print(script_name .. ": Set timer = " .. t) | ||
+ | calaos: | ||
+ | calaos: | ||
+ | -- Script test | ||
+ | if script_test_enable == true then | ||
+ | calaos: | ||
+ | end | ||
+ | print(script_name .. ": End") | ||
+ | end | ||
+ | |||
+ | |||
+ | -------------------------------------------------------------------------------- | ||
+ | -- State machine | ||
+ | -------------------------------------------------------------------------------- | ||
+ | |||
+ | -- Get initial values | ||
+ | local timer = calaos: | ||
+ | local evening = calaos: | ||
+ | local enable = calaos: | ||
+ | local state = calaos: | ||
+ | |||
+ | -- 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: | ||
+ | print(script_name .. ": End") | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | -- Staircase: switch on | ||
+ | if state == 0 and evening == true then | ||
+ | state_change (1) | ||
+ | calaos: | ||
+ | timer_set(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | -- Living room: switch on | ||
+ | if state == 1 and timer == " | ||
+ | state_change (2) | ||
+ | calaos: | ||
+ | timer_set(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | -- Hall 1: switch on | ||
+ | if state == 2 and timer == " | ||
+ | state_change (3) | ||
+ | calaos: | ||
+ | timer_set(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | -- Hall 1: switch off | ||
+ | if state == 3 and timer == " | ||
+ | state_change (4) | ||
+ | calaos: | ||
+ | timer_set(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | -- Living room: switch off | ||
+ | if state == 4 and timer == " | ||
+ | state_change (5) | ||
+ | calaos: | ||
+ | timer_set(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | -- Hall 2: switch on | ||
+ | if state == 5 and timer == " | ||
+ | state_change (6) | ||
+ | calaos: | ||
+ | timer_set(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | -- Straircase: switch off | ||
+ | if state == 6 and timer == " | ||
+ | state_change (7) | ||
+ | calaos: | ||
+ | timer_set(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | -- Bathroom: switch on | ||
+ | if state == 7 and timer == " | ||
+ | state_change (8) | ||
+ | calaos: | ||
+ | timer_set(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | -- Bathroom: switch off | ||
+ | if state == 8 and timer == " | ||
+ | state_change (9) | ||
+ | calaos: | ||
+ | timer_set(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | -- Hall 2: switch off | ||
+ | if state == 9 and timer == " | ||
+ | state_change (10) | ||
+ | calaos: | ||
+ | timer_set(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | -- Dressing: switch on | ||
+ | if state == 10 and timer == " | ||
+ | state_change (11) | ||
+ | calaos: | ||
+ | timer_set(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | -- Dressing: switch off | ||
+ | if state == 11 and timer == " | ||
+ | state_change (12) | ||
+ | calaos: | ||
+ | timer_set(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | -- Bedroom 1: switch on | ||
+ | if state == 12 and timer == " | ||
+ | state_change (13) | ||
+ | calaos: | ||
+ | timer_set(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | -- Bedroom 1: switch off | ||
+ | if state == 13 and timer == " | ||
+ | state_change (14) | ||
+ | calaos: | ||
+ | timer_set(" | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | |||
+ | print(script_name .. ": Did nothing" | ||
+ | return false | ||
+ | </ | ||