I’m trying to write an if statement for a scene that will have two separate conditions.
If the current hour is between 9AM and 9PM & a the light levels are below 15%
If the conditions for 1 are not met I want the scene to also check to see if it is night time, thus still activating the scene even though the light levels are above 15%.
I’ve gotten condition 1 to work without fail but when I added in the second if statement I keep getting “Error in lua for scenes and events”
[code]local from_hour = 09
local to_hour = 21
local cur_level = luup.variable_get(“urn:micasaverde-com:serviceId:LightSensor1”,“CurrentLevel”,35) or 0
local now = os.date (“*t”)
if ((now.hour >= from_hour) and (now.hour <= to_hour) and (tonumber(cur_level) <= 15)) then
return true
if (luup.is_night()) then
return true
else
return false
else
return false
end[/code]
if (x<=1) then
luup.log(“AAA”)
if (y==1) then
luup.log(“CCC”)
end
else
luup.log(“BBB”)
end
But I think you have the wrong apprach, if I got that right a scene is run as soon as it receives a return true, therfore you can’t have two “return true”. In your case you can use “or”:
x = 1
y = 0
if ((x==1) or (y==2)) then
luup.log(“AAA”)
else
luup.log(“BBB”)
end
so in your case:
local from_hour = 09
local to_hour = 21
local cur_level = luup.variable_get("urn:micasaverde-com:serviceId:LightSensor1","CurrentLevel",35) or 0
local now = os.date (“*t”)
if ((now.hour >= from_hour) and (now.hour <= to_hour) and (tonumber(cur_level) <= 15) or (luup.is_night())) then
return true
else
return false
end
Just a hint: You can test luup code in UI5 when you go to “Apps => Develop Apps => Test Luup Code”. Then you can watch the log with the following Link to see what happens: http://YOUR_VERA_IPADRESS/cgi-bin/cmh/log.sh?Device=LuaUPnP
I actually tried using this before but the only issue is that I have another scene that is essentially doing the same thing as my previous scene but instead of being from 9AM to 9PM it is 6AM to 8AM and it runs a different set of commands and lights. So when using the OR statement I found that it triggered both scenes together which turned on all the lights.
[quote=“chixxi, post:2, topic:170324”]local from_hour = 09
local to_hour = 21
local cur_level = luup.variable_get(“urn:micasaverde-com:serviceId:LightSensor1”,“CurrentLevel”,35) or 0
local now = os.date (“*t”)
if ((now.hour >= from_hour) and (now.hour <= to_hour) and (tonumber(cur_level) <= 15) or (luup.is_night())) then
return true
else
return false
end[/quote]
Seeing as there are more than one scene at stake here is there a way to write a nested if or addition function to solve this?
Hm, I am not sure what you want exactly, maybe I just don’t understand ???
If you have two scenes and both have “luup.is_night()” it is logic that both scenes run when it is night, independent of time. And that is what you want:
RUN scene IF it is time AND light level is below OR if it is night.
Is it? That’s what you describe in your first post?
I apologize if this is a little confusing, I will do my best to elaborate.
[quote=“chixxi, post:4, topic:170324”]If you have two scenes and both have “luup.is_night()” it is logic that both scenes run when it is night, independent of time. And that is what you want:
RUN scene IF it is time AND light level is below OR if it is night.
Is it? That’s what you describe in your first post?[/quote]
Scene 1 (Previous Post) triggered by motion sensor HSM-100 the scene is only supposed to run between the specific hours (9AM and 9PM) and only if the light levels are below 15%. The issue is with this last condition, some times right after dusk the light that is next to the motion/light sensor is on. When the sensor is tripped and the scene tries to run the light level will be greater than 15% which will not run the scene. What I’m trying to do is add in an additional condition that only runs between the hours specified but adds in the catch all functionality that it is night (luup.is_night()) just in case the light by the sensor happens to be on at night.
Scene 2 is triggered by the motion sensor HSM-100 but it is meant to only run between 6AM and 8AM and only if the light levels are below 15%.
This is where the problem with the OR statement comes in. For example, I walk down the hall at 7AM and the motion sensor is tripped. I only want Scene 2 to be run, but because of the OR statement in Scene 1 it disregards the time constraints (9AM-9PM) and jumps to the luup.is_night() piece which at this time of the year will be true since it is still dark outside.
Scene 1:
[quote=“chixxi, post:2, topic:170324”]local from_hour = 09
local to_hour = 21
local cur_level = luup.variable_get(“urn:micasaverde-com:serviceId:LightSensor1”,“CurrentLevel”,35) or 0
local now = os.date (“*t”)
if ((now.hour >= from_hour) and (now.hour <= to_hour) and (tonumber(cur_level) <= 15) or (luup.is_night())) then
return true
else
return false
end[/quote]
local from_hour = 09
local to_hour = 21
local cur_level = luup.variable_get("urn:micasaverde-com:serviceId:LightSensor1","CurrentLevel",35) or 0
local now = os.date (“*t”)
if ((now.hour >= from_hour) and (now.hour <= to_hour)) then
if ((tonumber(cur_level) <= 15) or (luup.is_night())) then
return true
end
end
return false
This first checks the time, if it is not time to run the scene, it is stopped right away. Only if it is time it will then check it light level is low OR if it is night.