Simulate Presence (Semesterläge) v1.0.4

Hjälp varandra att vara kreativa för att göra hemmet mer bekvämt.
Post Reply
jompa68
Proffsmedlem
Posts: 735
Joined: 12 Aug 2012, 08:53
12
Location: Hofors

Ny version av Simulate Presence (Semesterläge)
Skärmavbild 2014-06-23 kl. 17.41.42.png
Skärmavbild 2014-06-23 kl. 17.41.42.png (38.61 KiB) Viewed 75857 times
2st variabler behövs Simu_presence och overideSimuSunset
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.
Simulate_presence.vfib.zip
(1.33 KiB) Downloaded 1201 times

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

djesko
Medlem
Posts: 91
Joined: 10 Feb 2014, 22:50
10
Location: Trelleborg

funkar kanon

Tack

Ser att push meddelandet på on/off är samma "Lights simulation stopped" så du vet.

Niklas
jompa68
Proffsmedlem
Posts: 735
Joined: 12 Aug 2012, 08:53
12
Location: Hofors

Justerat koden nu :)
nohed
Medlem
Posts: 88
Joined: 20 Apr 2013, 14:10
11

får detta i debuggen

[ERROR] 08:27:09: line 45: attempt to call method 'getSourceTrigger' (a nil value)
jompa68
Proffsmedlem
Posts: 735
Joined: 12 Aug 2012, 08:53
12
Location: Hofors

Ser ut som du missat att skapa dom 2 variabler som behövs


Skickat från min iPhone med Tapatalk
nohed
Medlem
Posts: 88
Joined: 20 Apr 2013, 14:10
11

har dessa variabler och de ändras när man trycker på knapparna
jens
Medlem
Posts: 329
Joined: 22 Apr 2013, 17:09
11

nohed wrote:har dessa variabler och de ändras när man trycker på knapparna
Har samma felmeddelande som nohed.. :?
jompa68
Proffsmedlem
Posts: 735
Joined: 12 Aug 2012, 08:53
12
Location: Hofors

Det är flyttat till scen nu istället för main loop
jens
Medlem
Posts: 329
Joined: 22 Apr 2013, 17:09
11

jompa68 wrote:Det är flyttat till scen nu istället för main loop
Ja tänk vad det kan bli bra ibland... Nu funkar det :P
nohed
Medlem
Posts: 88
Joined: 20 Apr 2013, 14:10
11

var det så ja nu funkar det tackar
KristianO
Medlem
Posts: 461
Joined: 10 Mar 2014, 09:50
10

Gillar denna simulering men har en del frågor.
- Kan jag enbart styra den manuellt, eller allra helst. När larmet är påslaget på spec. enhet, typ dörrsensor så startar simuleringen oavsett tid på dygnet.
jompa68
Proffsmedlem
Posts: 735
Joined: 12 Aug 2012, 08:53
12
Location: Hofors

Finns en förbättrad version här: http://forum.fibaro.com/index.php?/topi ... l=presence
KristianO
Medlem
Posts: 461
Joined: 10 Mar 2014, 09:50
10

Tack! Men tror ändå inte scriptet går att köra helt manuellt? Verkar ändå vara tidsstyrt.


Sent from my iPhone using Tapatalk
jompa68
Proffsmedlem
Posts: 735
Joined: 12 Aug 2012, 08:53
12
Location: Hofors

Går alltid att få till ;)
nohed
Medlem
Posts: 88
Joined: 20 Apr 2013, 14:10
11

Här är en lite enklare simulator den kan man starta när man vill

--[[
%% properties

%% globals
--]]

fibaro:debug('start')


fibaro:setGlobal("Simulator", "På");
local minute = 60000 --in miliseconds

local rndmaxtime = 20 --random time of light change in minutes
local runtime = 1000 --how long to run simulation in minutes

local lights = {4,10,13,16,17,225,44,73,55} --IDs of lights to use in simulation
local nrlights = #lights --nr of light devices listed above

local start = os.time()
local endtime = start + runtime*minute/1000 -- after how many minutes exit simulation

while os.time() < endtime do

local rndlight = tonumber(lights[math.random(nrlights)])
local rnd = math.random(nrlights) --make it more random
local lightstatus = fibaro:getValue(rndlight, 'value')
fibaro:debug('light ID:'..rndlight..' status:'..lightstatus)
-- turn on the light if off or turn off if on
if tonumber(lightstatus) == 0 then fibaro:call(rndlight, 'turnOn') else fibaro:call(rndlight, 'turnOff') end

local sleeptime = math.random(rndmaxtime*minute)
fibaro:sleep(sleeptime)

local sleeptimemin = math.abs(sleeptime/60000)
fibaro:debug('sleeptime:'..sleeptimemin)

end

--turn Off all lights
for i = 1, nrlights do
rndlight = tonumber(lights)
fibaro:call(rndlight, 'turnOff');
fibaro:setGlobal("Simulator", "Av")
end


fibaro:debug('END')
TeknikPatrick
Ny medlem
Posts: 1
Joined: 15 Mar 2013, 18:42
11

Denna scen fungerar riktigt bra!
Det enda som jag inte får att lira är att den kör inte när solen har gått ner.
Är det inte meningen att den ska göra det eller har jag missat något?
Post Reply