Welcome to Smart Home Forum by FIBARO
Dear Guest,
as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.
As a member you can:
- Start new topics and reply to others
- Follow topics and users to get email updates
- Get your own profile page and make new friends
- Send personal messages
- ... and learn a lot about our system!
Regards,
Smart Home Forum by FIBARO Team

Question
Guest berga_s
Hello I would like to create a scene or script such as:
- if the door is open for more than YY seconds then push a notification and send email
I've tried to build by graphic blocks but may be is not possible, so I've tried to find some LUA code, but it's first time to me with LUA...
This is the state of the art of the script I've assembled (taking also some part from other script in the forum) but I have some doubt:
1) is this script triggered every time that the door is open? this should be done by string " 25 value" in row 4 at the beginning, right?
2) the string in row 20 "if (fibaro:countScenes(sceneID) == 1) then" check if there are more than 1 scene (with ID11) running? --> Update, yes...
3) the string in row 26 "fibaro:call(2, "sendEmail", pushMsg);" still not send the mail, what's wrong? the ID 2 should be right becasue it's aslo used in other scene that I've created by graphic blocks... --> Update, now is working
4) What is the command "fibaro:getSourceTrigger()"? --> Update, Gets information about the trigger that caused the current scene to run. This function can be also used to determine which of the triggers was the direct cause of the script execution.
5) What's happen if between the interval MaxOpenTimeInSeconds the door is closed and opened again? Is the scene restart again or it continue to count since the first opening? --> Update, new script below
The second step will be play a notification in to the Sonos Playbar, but for these I've already see some other LUA examples.
The script (OLD):
****************************************************************
--[[
%%autostart
%% properties
25 value
%% globals
--]]
local sensorID = 25; -- ID door to be checked
local sceneID=11; -- ID of the present scene
local MaxOpenTimeInSeconds = 2; -- max sec before send notification
local debug = true;
local pushMsg = "The door appears to have been open for too long"; -- mesage to be sent
if (debug) then
fibaro:debug ("Sensor "..sensorID .. " value is " .. fibaro:getValue(sensorID, "value"));
fibaro:debug("Running Scenes: ".. fibaro:countScenes(sceneID));
fibaro:debug(" getSourceTriggerType: "..fibaro:getSourceTriggerType());
end
if (fibaro:countScenes(sceneID) == 1) then
if (fibaro:getValue(sensorID, "value") == "1") then -- if the door is open then
if (debug) then fibaro:debug("The door is Open"); end
fibaro:sleep(MaxOpenTimeInSeconds*1000); -- wait for x seconds
if (fibaro:getValue(sensorID, "value") == "1") then -- if the door is still open then
if (debug) then fibaro:debug("The door is still open, pushing notification!"); end
fibaro:call(2, "sendEmail", pushMsg); -- user to be sent the mail
fibaro:call(37, "sendPush", pushMsg); -- phone to be sent the notification
end
end
else
if (debug) then fibaro:debug("Scene is already running. No action taken."); end
end
****************************************************************
[ Added: 2014-09-16, 21:12 ]
I've made some modification to the script and in the meanwhile answered to some doubts...
Now the script is parametric and can send a max number of notification and also in case there is a change status in the meanwhile is running it's exit from the loop...
Not yet tested for real change state in the in the meanwhile is running, but should be working.
Is there a way to keep formatted the code?
The NEW script:
*******************************************************
--[[
%%autostart
%% properties
25 value
%% globals
--]]
local sensorID = 25; -- ID door to be checked
local sceneID=11; -- ID of the present scene
local MaxOpenTimeInSeconds = 5; -- max sec before send notification
local debug = true;
local MaxNotificNum = 5 -- max number of notification message to be sent
if (debug) then
fibaro:debug ("Sensor "..sensorID .. " value is " .. fibaro:getValue(sensorID, "value"));
fibaro:debug("Numero di scene in esecuzione: ".. fibaro:countScenes(sceneID));
fibaro:debug(" getSourceTriggerType: "..fibaro:getSourceTriggerType());
end
if (fibaro:countScenes(sceneID) == 1) then
if (fibaro:getValue(sensorID, "value") == "1") then -- if the door is open then
if (debug) then fibaro:debug("La porta è aperta"); end
for i = 0, MaxNotificNum-1 do
fibaro:sleep(MaxOpenTimeInSeconds*1000); -- wait for x seconds
if (fibaro:getValue(sensorID, "value") == "1") then -- if the door is still open
TimeSec = os.time() - fibaro:getModificationTime(sensorID, "value");
if TimeSec >= MaxOpenTimeInSeconds then -- if there is not a change on value state of door since begining
if (debug) then
fibaro:debug("Ultimo cambio di stato: "..TimeSec.." secondi ovvero > di "..MaxOpenTimeInSeconds.." secondi")
fibaro:debug("Porta ancora aperta, invio notifica numero "..i+1);
end
TimeElapsed = (MaxOpenTimeInSeconds*(i+1))
pushMsg = "Porta ingresso aperta da più di "..TimeElapsed.." secondi" -- message formatting
fibaro:call(2, "sendEmail", pushMsg, pushMsg);
fibaro:call(37, "sendPush", pushMsg);
else i=MaxNotificNum-1
end
end
end
end
else
if (debug) then fibaro:debug("La scena è già in esecuzione. Nessuna azione intrapresa."); end
end
*******************************************************
6 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.