Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
fr:liste_script [2016/07/28 14:10] mifrey |
fr:liste_script [2016/11/11 00:25] (current) mifrey Correction script panique |
||
---|---|---|---|
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 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 | ||
- | < | + | < |
-- Title: Set RGB light output depending on Temperature | -- Title: Set RGB light output depending on Temperature | ||
-- | -- | ||
Line 60: | Line 60: | ||
- | < | + | < |
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. | ||
- | < | + | < |
print(" | print(" | ||
local temp_panneaux = calaos: | local temp_panneaux = calaos: | ||
Line 172: | Line 172: | ||
__Script :__ | __Script :__ | ||
- | < | + | < |
local RGBplafond = calaos: | local RGBplafond = calaos: | ||
local Wplafond = calaos: | local Wplafond = calaos: | ||
Line 199: | Line 199: | ||
**But :** Script permettant de donner une valeur True ou False à une variable lors de l' | **But :** Script permettant de donner une valeur True ou False à une variable lors de l' | ||
- | < | + | < |
local Variable_Activation_Inter = calaos: | local Variable_Activation_Inter = calaos: | ||
Line 225: | Line 225: | ||
**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' | **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' | ||
+ | Afin de d' | ||
+ | < | ||
+ | root@raspberrypi: | ||
+ | Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: | ||
+ | Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: | ||
+ | Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: | ||
+ | Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: | ||
+ | Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: | ||
+ | Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: | ||
+ | Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: | ||
+ | Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: | ||
+ | Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: | ||
+ | Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: | ||
+ | Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: | ||
+ | Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: | ||
+ | Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: | ||
+ | Nov 11 00:16:46 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: | ||
+ | |||
+ | ... | ||
+ | </ | ||
__A créer dans CALAOS INSTALLER :__ | __A créer dans CALAOS INSTALLER :__ | ||
Line 235: | Line 255: | ||
<code lua> | <code lua> | ||
-- Detect a panic situation in which switches are pressed several times within a short time. | -- Detect a panic situation in which switches are pressed several times within a short time. | ||
- | -- The script | + | -- The script |
+ | |||
--if true then return false end -- Uncomment to disable the script | --if true then return false end -- Uncomment to disable the script | ||
+ | |||
-- Script start | -- Script start | ||
local script_name = " | local script_name = " | ||
- | print(script_name .. ": | + | print(script_name .. ": |
- | + | ||
- | -- IO id | + | -- IOs id |
local timer_id = " | local timer_id = " | ||
local counter_id = " | local counter_id = " | ||
+ | |||
-- Other variables to define | -- 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 | 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: | ||
+ | local counter = calaos: | ||
- | -- Print timer and counter value at script start | + | print(script_name .. ": timer = " .. tostring(timer)) |
- | print(script_name .. ": timer = " .. tostring(calaos: | + | print(script_name .. ": counter = " .. tostring(counter)) |
- | print(script_name .. ": counter = " .. tostring(calaos: | + | |
- | -- Get timer status: Timer is false when timer starts | + | |
- | local timer = calaos: | + | -- Max time exceeded |
- | + | if timer == " | |
- | -- Timer is done, reset timer and counter. | + | print(script_name .. ": |
- | if timer == " | + | -- Restart timer |
- | print(script_name .. ": | + | timer = " |
- | | + | calaos: |
- | calaos: | + | print(script_name .. ": |
- | print(script_name .. ": timer = " .. tostring(calaos: | + | -- First count |
- | print(script_name .. ": counter = " .. tostring(calaos: | + | counter = 1; |
- | print(script_name .. ": | + | |
+ | print(script_name .. ": | ||
+ | print(script_name .. ": | ||
return false | return false | ||
- | end | + | -- Max time not exceeded |
- | + | ||
- | -- Increment the counter | + | |
- | local counter = calaos: | + | |
- | calaos: | + | |
- | + | ||
- | -- Timer is not done and counts not reached, continues to count. | + | |
- | if counter < panic_count then | + | |
- | print(script_name .. ": Timer is not done and counts not reached, continue." | + | |
- | print(script_name .. ": timer = " .. tostring(calaos: | + | |
- | print(script_name .. ": counter = " .. tostring(calaos: | + | |
- | print(script_name .. ": end" | + | |
- | return false | + | |
- | -- Timer is not done and counts reached, panic mode. | + | |
else | else | ||
- | | + | -- Count up |
- | print(script_name .. ": timer = " | + | counter = counter + 1 |
- | print(script_name .. ": | + | calaos: |
- | calaos: | + | print(script_name .. ": |
- | | + | |
- | print(script_name .. ": end" | + | -- Counts |
- | return true | + | 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 .. ": | ||
+ | -- Stop timer | ||
+ | timer = "stop" | ||
+ | calaos:setOutputValue(timer_id, timer) | ||
+ | print(script_name .. ": | ||
+ | -- Reset counter | ||
+ | counter = 0; | ||
+ | calaos: | ||
+ | print(script_name .. ": Set counter = " .. tostring(counter)) | ||
+ | print(script_name .. ": end" | ||
+ | return true | ||
+ | end | ||
end | end | ||
+ | |||
print(script_name .. ": Oups, should never be there..." | print(script_name .. ": Oups, should never be there..." | ||
Line 295: | Line 325: | ||
+ | |||
+ | \\ === 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 | ||
+ | </ | ||