Help with Scene - only run lua code when it is night

I am (once again!) struggling to get a scene working - not least due to my limited programming skills. I have had the following code working for a year which turns my bathroom light on (triggered by a movement sensor), the light level depending upon the time of day:

local t = os.date(‘*t’)
local current_second = t.hour * 3600 + t.min * 60 + t.sec – number of seconds since midnight
local min_time_in_seconds = 22 * 3600 + 30 * 60 – 22:30
local max_time_in_seconds = 5 * 3600 + 0 * 60 – 5:00

if (current_second > max_time_in_seconds) and (current_second < min_time_in_seconds) then
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “50”}, 127) – turn bathroom light to dim level
else
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “25”}, 127) – turn bathroom light to dim level
end

I want the code only to run when it is night. I have tried a number of different code examples from this forum, one example is the following which produces a lua scene error:

local t = os.date(‘*t’)
local current_second = t.hour * 3600 + t.min * 60 + t.sec – number of seconds since midnight
local min_time_in_seconds = 22 * 3600 + 30 * 60 – 22:30
local max_time_in_seconds = 5 * 3600 + 0 * 60 – 5:00

dID = 67
local allow = true – true runs scene during daytime, false runs it at night
local status = luup.variable_get(“urn:rts-services-com:serviceId:DayTime”,“Status”,dID)
return ((status == “0”) == allow)

if (current_second > max_time_in_seconds) and (current_second < min_time_in_seconds) then
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “50”}, 127) – turn bathroom light to dim level
else
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “25”}, 127) – turn bathroom light to dim level
end

Can any one help me correct the above code to only run the if, then else when it is night?

I’m by no means an expert in Lua, but something like this may work. it will only run your if

  local t = os.date('*t')
  local current_second = t.hour * 3600 + t.min * 60 + t.sec   -- number of seconds since midnight
  local min_time_in_seconds = 22 * 3600 +  30 * 60             -- 22:30
  local max_time_in_seconds =  5 * 3600 +  0 * 60             -- 5:00

if luup.is_night() then
    if  (current_second > max_time_in_seconds) and (current_second < min_time_in_seconds) then
      luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = "50"}, 127) -- turn bathroom light to dim level
    else
      luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = "25"}, 127) -- turn bathroom light to dim level
    end
end

Hi,

Have you looked at the time range you can set in the UI7 Scene editor? Saves from writing code.

Cheers Rene