I just write a set of scripts to manage devices from a calendar.
The problem : My wife has 5 types of working time (plus week-end worked or not). I wanted to automate the heating according to those hours.
I defined type of days in a file : actions.txt
# cat actions.txt
Code: Select all
M5W;05:00;Zone jour;Set Level:50
M5;05:00;Zone jour;Set Level:50
M5;07:00;Zone jour;Set Level:20
M5;14:00;Zone jour;Set Level:50
S7W;08:00;Zone jour;Set Level:50
S7;06:30;Zone jour;Set Level:50
S7;13:30;Zone jour;Set Level:20
S7;18:30;Zone jour;Set Level:50
S25W;08:00;Zone jour;Set Level:50
S25;06:00;Zone jour;Set Level:50
S25;11:30;Zone jour;Set Level:20
S25;18:30;Zone jour;Set Level:50
CONGES;07:30;Zone jour;Set Level:50
ABSENTS;01:00;Zone jour;Set Level:20
J0;06:30;Zone jour;Set Level:50
INCONNU;06:30;Zone jour;Set Level:50
*;22:30;Zone jour;Set Level:20
*;22:30;Zone nuit;Set Level:20
Name of day : the name you want to give to this type of day. It's used in the other file below
time of action : if the form hh:mm
Name of device to action
Action to perform
The name of day can be *, which mean every days (in my case, the heating is put in eco mode every days at 22:30)
INCONNU is the default is nothing is defined in horaires.txt
# cat horaires.txt
Code: Select all
<...>
20180525 M5
20180526 M5
20180527 M5
20180528 CONGES
20180529 CONGES
20180530 S7
20180531 S7
date (YYYYMMDD)
type of day (as defined in actions.txt)
Then a write a lua/time script LectureTypedeJour which setup a user variable TypeJour
Code: Select all
commandArray = {}
-- Lecture de l'heure (HH:MM)
h = os.date("%R");
-- Lecture du type de jour dans le fichier, si on ne trouve pas, INCONNU
v='INCONNU'
-- On ne le fait qu'une fois par jour
if (h == "00:06") then
file="/home/horaires.txt";
for line in io.lines(file) do
j = string.sub(line, 1, 8);
if (j == os.date("%Y%m%d")) then
v = string.sub(line, 10);
break;
end
end
commandArray['Variable:TypeJour'] = v
commandArray[46] = { ['UpdateDevice'] = "46" ..'|0|'.. "Jour " .. v }
end
return commandArray

I write a lua/time script Chauffage to actually turn device on or off, depending of time
Code: Select all
commandArray = {}
-- A function i found in a lua forum
function split(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
-- Lecture de l'heure (HH:MM)
h = os.date("%R");
-- Hors période de chauffage, on ne fait rien
-- I set up a dummy switch device, in summer, no need of all this stuff
if (otherdevices['État du chauffage'] == "Off") then
return commandArray;
end
-- The variable set in the first script
TypedeJour = uservariables['TypeJour'];
-- Par convention, on ajoute un W à la fin le weekend
-- Lazy stuff to avoid things like M5W in horaires.txt. I don't know wich days are week ends
-- when I type them in the file
if ( os.date("%u") == "6" or os.date("%u") == "7") then
TypedeJour = TypedeJour .. "W";
end
-- We read the file, and build the commands in commandArray
-- Several devices can be activated at the same hour
file="/home/actions.txt";
JourOk = 0;
for line in io.lines(file) do
for i, word in ipairs (split(line, ";")) do
if (i == 1 and (word == TypedeJour or word == "*")) then
JourOk = 1;
end
if (i == 2 and JourOk == 1) then
HeureDebut = word;
if (HeureDebut == h) then
JourOk = 2;
end
end
if (i == 3 and JourOk == 2) then
NomZone = word;
end
if (i == 4 and JourOk == 2) then
Action = word;
end
end
if (JourOk == 2) then
print (NomZone, HeureDebut, Niveau);
commandArray[NomZone] = Action;
JourOk = 0;
end
end
return commandArray
Code: Select all
commandArray = {}
-- Mise à jour du texte du bouton 'Type de jour' pour info sur dashboard
for variableName,variableValue in pairs(uservariablechanged) do
print ("Variable based event fired on '"..variableName.."', value '"..tostring(variableValue).."'");
if (variableName=='TypeJour') then
commandArray[46] = { ['UpdateDevice'] = "46" ..'|0|'.. "Jour " .. variableValue }
end
end
return commandArray
Many things to improve :
- My english

- A screen to help input of actions.txt and horaires.txt
- may be some bugs
Today, to populate horaires.txt, i do (for may by example)
# seq 20180501 20180531 >> horaites.txt
then change the lines with the type of day in vi !
Feel free to use, rewrite, improve, ...
Pascal