Page 1 of 1

[Solved] Stepp down light, before turning off ?

Posted: Sunday 12 August 2018 16:47
by Droll
By searching this forum, using som bits and pices, a bit cut/paste :) I usually ends up with what I want, or close ( AND learning as a bonus )...

This time it's close, just want's a little bit more... found the bases for this script here on the forum, just made some small adjustments ...

BUT ...
Any easy way to replace the "OFF" command by something like :

commandArray['Bad']='Set Level 5%' for 30 sek
THEN
commandArray['Bad']='OFF '

Just to get a warning ;-) before it gets dark ( Light triggered by movement )

Arne Kjetil



Code: Select all

function timedifference(s)
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)
   t1 = os.time()
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   difference = os.difftime (t1, t2)
   return difference
end
 
commandArray = {}

-- Set length of time light should be on for in seconds
timeon = 140
-- Calculate time since time PIR was last activated
difference = timedifference(otherdevices_lastupdate['Bevegelse_Bad'])
-- If the time since last activation was within 1 minute of time for light to stay on
if (difference > timeon and difference < (timeon + 61)) then
   tempdiff = tostring(difference)
   tempmessage = "Switch1 Light Off - after at least " .. (timeon+1) .. "secs up - actually - " .. tempdiff .. "seconds"
   print(tempmessage)
   -- Switch off Switch1
   commandArray['Bad'] = 'Off'

print('testing domoticz log')
end
 
return commandArray

Re: Stepp down light, before turning off ?

Posted: Thursday 16 August 2018 17:48
by Droll
Solution ....

Found what I was looking for, found in a post from alanlsmith :)
Spoiler: show
function timedifference(s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = os.difftime (t1, t2)
return difference

end

commandArray = {} --Round to a set number of decimal places
function round(what, precision)
if (what) then
return math.floor(what*math.pow(10,precision)+0.5) / math.pow(10,precision)
else
return math.floor(val+0.5)
end
end

local light = 'Bad' --For fading
local lightstepsfull = 10 --100% in 5% steps --These need to be adjusted top suit the--
local lightstep = 5 --number of steps to suit your light/dimmer--
local timestep = 10 -- Time in seconds between each light level step--
local delay = 0 --Delay, (if any) before start of dimming/fading up--

lysbaddag = tonumber(uservariables['lysbaddag'])
lysbadnatt = tonumber(uservariables['lysbadnatt'])

if (timeofday['Daytime']) then lightlevel = lysbaddag
elseif (timeofday['Nighttime']) then lightlevel = lysbadnatt

end
local lightstepsnow = round( lightlevel / lightstep , 0 )

local lightsteps = lightstepsfull - lightstepsnow

timeon = 61 -- Set length of time light should be on for in seconds

diff1 = timedifference(otherdevices_lastupdate['Bevegelse_Bad'])
diff2 = timedifference(otherdevices_lastupdate['DorBad'])

if (diff1 < diff2) then --Using 2 devices to trigger the light, door switch and movmen sensor
difference = diff1 else difference = diff2 --Just need to know witch one triggered last...
end

-- If the time since last activation was within 1 minute of time for light to stay on
if (difference > timeon and difference < (timeon + 61)) then
tempdiff = tostring(difference)
tempmessage = "Bad av, etter minst " .. (timeon+1) .. "secs up - actually - " .. tempdiff .. "seconds"
print(tempmessage)

for i = 1,lightstepsnow do -- Dimming down the light in steps, need a warning before the lights goes dark ...
lightlevel = lightlevel - lightstep
delay = delay + timestep
commandArray[#commandArray+1] = {[light] = 'Set Level ' ..lightlevel.. ' AFTER ' ..delay}
end


Print('Slar av lys bad')

end

return commandArray