We have moved at community.getvera.com

Author Topic: Help with Scene - only run lua code when it is night  (Read 401 times)

Offline Talisker

  • Sr. Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
Help with Scene - only run lua code when it is night
« on: November 11, 2018, 06:49:52 am »
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?


Offline sebby

  • Sr. Member
  • ****
  • Posts: 306
  • Karma: +15/-3
Re: Help with Scene - only run lua code when it is night
« Reply #1 on: November 11, 2018, 08:11:17 am »
I'm by no means an expert in Lua, but something like this may work.  it will only run your if

Code: [Select]
  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

Offline reneboer

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1574
  • Karma: +110/-31
Re: Help with Scene - only run lua code when it is night
« Reply #2 on: November 11, 2018, 09:36:57 am »
Hi,

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

Cheers Rene
2xVeraLite, VeraEdge, openLuup, ALTUI, 20 switches, 10 dimmers, 20 sensors, 10 scene controllers, 1 Harmony Hub, many plug-ins. Not enough time.