Code for not activating a scene more than once within for example 1 minute.

Hello,

When I drive home I activate a scene; opening my gate, garagedoor, activating some lights etc. Activating this scene is done with a cellphone, but the reception is quite poor.

It happends quite a lot that I get an “empty response” from the authomation programm, I simply press the button another time and it normally works.

BUT, it happends also quite often that there is just a delay in the network and the scene triggers than 2 times… this is not good , since it closes the gate again.

Is it possible to insert something in a scene so this scene can only be activated once in a given period, let’s say 1 minute, if the button is pressed to activate the scene for another time within this minute , nothing should happen.

Many thanks,
Cor

Add the following to the LUUP for the trigger for your scene.

local result = true
local now = os.time()
if (LastTime) then
    result = now - LastTime > 60
end
LastTime = now
return result

Thanks Richard !!!

I just wanted to insert this code, but noticed I allready had a code in there (to acticate 2 scenes, one only for night time).

The code I allready have is this:

luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = "6"}, 0) luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = "7"}, 0)

Can I just add your code below this with a empty line in between, like this:

[code]luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”, “RunScene”, {SceneNum = “6”}, 0)
luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”, “RunScene”, {SceneNum = “7”}, 0)

local result = true
local now = os.time()
if (LastTime) then
result = now - LastTime > 60
end
LastTime = now
return result
[/code]

Thanks again,

Cor

This will run those other 2 scenes even if the event happens in less than 60 seconds.
If that’s what you want then you can do it.

If you do not want them to run if they are within 60 seconds than you can do:

local result = true
local now = os.time()
if (LastTime) then
    result = now - LastTime > 60
end
LastTime = now
if (result) then
  luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = "6"}, 0)
  luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = "7"}, 0)
end
return result

Empty lines are fine!

@ Richard; Yeah that makes sense, you first check if you want the scene to run or not :slight_smile:

Many thanks , I appreciate your help,
Cor

Looking at my scenes, I found another scene where this “not run within a timeframe comes in handy”

Also here allready a code ( home and away button via a virtual switch)
The idea is to, first check , if the virtual switch is on , if yes, than activate the scene , but not to activate more than once within 2 minutes.

The code I allready had:

[code]autoscene = luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”,“Status”,32)

if(autoscene==“1”)then

return true
else
return false
end[/code]

your code:

local result = true
local now = os.time()
if (LastTime) then
    result = now - LastTime > 60
end
LastTime = now
return result

Thinking about it , I did this:

autoscene = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",32)

if(autoscene=="1")then

local result = true
local now = os.time()
if (LastTime) then
    result = now - LastTime > 120
end
LastTime = now
return result

else
return false
end

but to be honoust… I have no clue if it makes sense what i did… :-\

Will this work ? :slight_smile:

Cor

That will work but is not very good form for programming.
Good form will reduce the possibility of programming errors.
I would use:

local autoscene = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",32)
local result = true
local now = os.time()
if (LastTime) then
    result = now - LastTime > 120
end
LastTime = now
return result and (autoscene == "1")

GREAT!!! :smiley:

Thanks for your time.

Cor

@ Richard,

Again I need your help:-)
I was editing some more scenes and my alarm needs some changing, i tried to edit the code myself , but it doesn’t work, even if the virtual switch (ID32) is selected “off” the 2 scenes i want to run still activate… where is the error?

local autoscene = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",32) local result = true local now = os.time() if (LastTime) then result = now - LastTime > 70 end LastTime = now if (result) then luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = "29"}, 0) luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = "32"}, 0) end return result

*check if virtual switch id 32 is on
*if yes, than check if the scene has not been activated the last 70 seconds
*if that is not the case than execute scene 29 and 32

many thanks,
Cor

Since I do not use Virtual Switches, not do I know the sense of it’s logic … I can only guess that the change is as follows:

local autoscene = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status",32)
local result = (autoscene == "1")
local now = os.time()
if (LastTime) then
    result =  result and (now - LastTime > 70)
end
LastTime = now
if (result) then
  luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = "29"}, 0)
  luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = "32"}, 0)
end
return result

perfect! Works exactly now how I want it to work :slight_smile:

Many thanks… The idea for the virtual switch is like an “away and home” switch , to switch the alarm function on

Cor

I have a code here, and i need help to figure out how to write it so that it will do:

After it have been trigged i want it to wait an hour to be trigged again

[code]local startTime = “14:00”
local endTime = “18:00”

local hour = tonumber( startTime:sub( startTime:find(“%d+”) ) )
local minute = tonumber(startTime:sub(-2))

if hour and minute then
startTime = hour * 100 + minute
else
luup.log(“ERROR: invalid start time”)
return false
end

hour = tonumber( endTime:sub( endTime:find(“%d+”) ) )
minute = tonumber(endTime:sub(-2))

if hour and minute then
endTime = hour * 100 + minute
else
luup.log(“ERROR: invalid end time”)
return false
end

local currentTime = os.date(“*t”)
currentTime = currentTime.hour * 100 + currentTime.min

luup.log("startTime = " … startTime … "; currentTime = " … currentTime … "; endTime = " … endTime)

if startTime <= endTime then
– Both the start time and the end time are in the same day:
– if the current time is in the given interval, run the scene.
if startTime <= currentTime and currentTime <= endTime then
return true
end
else
– The start time is before midnight, and the end time is after midnight:
– if the current time is not outside the given interval, run the scene.
if not (endTime < currentTime and currentTime < startTime) then
return true
end
end

return false
[/code]

There are only 60 minutes in an hour not 100. (Three occurrences)

But it should still work.

Yes but the trigged repeatedly

At the beginning:

if (not lasttime) then
  lasttime = 0
end

replace:

return true

with:

if (os.time() - lasttime > 60) then
   lasttime = os.time()
   return true
else
   return false
end

Thanks