Simu_presence ska vara en global variabel med On och Off fördefinierat, overideSimuSunset ska vara vanlig variabel
zip filen innehåller den virtuell enhet som behövs för att aktivera scenen.
Code: Select all
--[[
%% autostart
%% properties
%% globals
Simu_presence
overideSimuSunset
--]]
-- LUA - Presence Simulator at Home
-- Modified by Jonny Larsson (jompa68)
-- 2014-06-23
-- New features (sunset can be overrided), rewritten som lua code
-- and moved code from virtual mainloop to scene
-- 2014-01-24
-- Changed from fixed START TIME to sunsetHour and to STOP Hour and Minute.
--
--
-- Simulate a presence when you're on vacation.
-- A part of code is reused, it can found here. Thanx to Richo: http://forum.fibaro.com/viewtopic.php?t=1892&postdays=0&postorder=asc&highlight=presence&start=15
--
--------------------- USER SETTINGS --------------------------------
local stop_hour = "23"; -- Hour when you want simulation to stop
local stop_minute = "00"; -- Minute of the hour you want simulation to stop
local rndmaxtime = 20 -- random time of light change in minutes --> here each device is on max 20min
local ID_devices_lights = {133,148} -- IDs of lights to use in simulation
local activatePush = true; -- activate push when simulation starts and stops
local ID_Smartphone = 141; -- ID of your smartphone
-- 2 variable needed, Simu_presence(predefined ("On" and "Off") and overrideSimuSunset ("0" and "1")
-- Future release
-- Check if variable exist, if not then create them
--------------------- USER SETTINGS END ----------------------------
----------------------ADVANCED SETTINGS-----------------------------
local startSource = fibaro:getSourceTrigger();
local showStandardDebugInfo = true; -- Debug shown in white
local showExtraDebugInfo = true; -- Debug shown in orange
local numbers_lights = #ID_devices_lights --numbers of light devices listed above
local simu = fibaro:getGlobal("Simu_presence"); --value of the global value: simulation is on or off
local manualOveride = fibaro:getGlobal("overideSimuSunset");
--------------------------------------------------------------------
-----------------------------------
----- Do not change code below ----
-----------------------------------
local simu = fibaro:getGlobal("Simu_presence"); --value of the global value: simulation is on or off
local start_simu = fibaro:getValue(1, "sunsetHour"); --Start simulation when sunset
local minute = 60000 --in milliseconds
local start = os.date("%H:%M")
local time = os.time()
local date = os.date("*t", time)
local year = date.year
local month = date.month
local day = date.day
local endtime = os.time{year=year, month=month, day=day, hour=stop_hour, min=stop_minute, sec=sec}
version = "1.0.4"
SimulatorPresenceEngine = {};
-- debug function
Debug = function ( color, message )
fibaro:debug(string.format('<%s style="color:%s;">%s</%s>', "span", color, message, "span"));
end
ExtraDebug = function (debugMessage)
if ( showExtraDebugInfo ) then
Debug( "orange", debugMessage);
end
end
StandardDebug = function (debugMessage)
if ( showStandardDebugInfo ) then
Debug( "white", debugMessage);
end
end
-- function push message to mobile
pushMessage = function (sendPush)
if (activatePush) then
ExtraDebug("Push info sent to mobile")
fibaro:call(ID_Smartphone, 'sendPush', sendPush);
end
end
-- function to switch off devices in the list
function SimulatorPresenceEngine:TurnOff(group)
Debug("red","TurnOff lights!")
local name, id;
local ID_devices_group = group;
for i=1, #ID_devices_group do
id = tonumber(ID_devices_group[i]);
fibaro:call(id, "turnOff");
name = fibaro:getName(id);
if (name == nil or name == string.char(0)) then
name = "Unknown"
end
StandardDebug("Device:" .. name .. " Off ");
end
end
-- function to simulate a presence
function SimulatorPresenceEngine:Launch()
ExtraDebug( "Simulation will stop: "..stop_hour..":"..stop_minute );
if (os.time() >= endtime) or (simu == "Off") or (manualOveride == "0") then
ExtraDebug("Simulation stopped")
SimulatorPresenceEngine:TurnOff(ID_devices_lights)
end
pushMessage("Lights simulation started")
while ((os.time() <= endtime) and (simu == "On")) or ((os.time() <= endtime) and (simu == "On") and (manualOveride == "1")) do
if time == endtime then StandardDebug("same value") end
local random_light = tonumber(ID_devices_lights[math.random(numbers_lights)]) --choose a random light in the list
local lightstatus = fibaro:getValue(random_light, 'value') --get the value of the random light in the list
-- turn on the light if off or turn off if on
if tonumber(lightstatus) == 0 then fibaro:call(random_light, 'turnOn') else fibaro:call(random_light, 'turnOff') end
fibaro:sleep(1000) ; --necessary to get back the new status, because HC2 is too fast :-)
lightstatus = fibaro:getValue(random_light, 'value') --get the value of the random light after his update
StandardDebug('light ID:'..random_light..' status:'..lightstatus)
local sleeptime = math.random(rndmaxtime*minute) --random sleep
fibaro:sleep(sleeptime)
local sleeptimemin = math.abs(sleeptime/60000)
StandardDebug('sleeptime:'..sleeptimemin)
simu = fibaro:getGlobal("Simu_presence"); --verify the global value, if the virtual device is deactivated, the scene stops.
manualOveride = fibaro:getGlobalValue("overideSimuSunset")
end
--turn Off all lights
SimulatorPresenceEngine:TurnOff(ID_devices_lights);
sunset = 0
end
-- Condition to start simulation
if (os.date("%H:%M") >= start_simu) then
sunset = 1 else sunset = 0
end
if (simu == "On") then
Debug("green", "Simulate Precense at Home | v" .. version );
Debug( "green", "--------------------------------------------------");
ExtraDebug("Todays sunset: "..fibaro:getValue(1, "sunsetHour"))
end
if (simu == "On") then Debug("grey", "Simulate Precense will normally start:"..start_simu) end
if ((simu == "On") and os.time() <= endtime and sunset == 1 ) or ((simu == "On") and os.time() <= endtime and manualOveride == "1" ) then
SimulatorPresenceEngine:Launch(); --launch the simulation when virtual device is on, and the current time is triggered.
if manualOveride == "1" and sunset == 0 then Debug("grey", "Manual override activated")
elseif sunset == 1 then Debug("grey", "It's sunset time!")
end
if sunset == 0 and manualOveride == "0" then
Debug("grey", "Not manual overided so Simulate Precense will not be activated before sunset");
elseif os.time() >= endtime then
Debug("grey", "Time is now after:"..stop_hour..":"..stop_minute );
end
end
if (simu == "Off") then
SimulatorPresenceEngine:TurnOff(ID_devices_lights);
Debug("red","Simulation is deactivated")
pushMessage("Lights simulation stopped")
end