Luup help

Still learning the syntax…

If I wanted to only run a scene if between sunset and 11pm how do I do that? And is there a way to see if a scene is active? i.e. if a scene is active (it turned the lights on to 50%) and will turn them off at 11. I want another scene to trigger on an event then if this other scene is active return the lights to their previous settings.

EDIT
I found this snippit… I think will do what I want. The only thing missing is how to reference the internal sunset variable.

Scene that runs only in a user set time interval

Add this code in the Luup Code section:

local startTime = "22:30"
local endTime   = "05:30"
 
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

This code allows the scene to run only between 22:30 and 5:30.

Unfortunately there is no sunset variable. I added a Mantis #2147.

@mcvflorin

Thanks!

Could you use the onset of sunset to trip a virtual switch and use the switch state?

Yes, Good idea… Not as clean but would work.

… or install DAD.

Heliotrope has an “Altitude” variable that you can fetch with a luup.variable_get(). By definition, if the altitude is less than zero, the sun has set (and not yet risen).

Edit: or, I could read what you really asked for, and not just give a more-complicated equivalent of luup.is_night()…

[quote=“futzle, post:7, topic:170289”]Heliotrope has an “Altitude” variable that you can fetch with a luup.variable_get(). By definition, if the altitude is less than zero, the sun has set (and not yet risen).

Edit: or, I could read what you really asked for, and not just give a more-complicated equivalent of luup.is_night()…[/quote]

I know this is an old thread but it still seems a relevant place to post the question.

I have tried the following luup code with the heliotrope but when I test it in the “Develop Apps”, “Luup Test Code” area, it comes back with “Code Failed”.

[code]local sunLevel = luup.variable_get(“urn:futzle-com:serviceId:AstronomicalPosition_Sun”, “Altitude”, 35)

sunLevel = tonumber(sunLevel)

if (sunLevel <= 16) then
return true
else
return false
end if[/code]

Can anyone see if there is something wrong with this code? ???

Thanks

Gizmo

Use “end” not “end if”.

::slight_smile:

Thanks futzle, I couldn’t see the wood for the trees.