Author Topic: Probably a simple question about night or day  (Read 465 times)

Offline clausnilsson

  • Newbie
  • *
  • Posts: 15
  • Karma: +0/-0
Probably a simple question about night or day
« on: February 08, 2017, 04:46:55 pm »
Hi,

I wish to have a scene that switches off all lights in the living room. But a few lights need to stay on for a minute until we are out of the room. One light in a dark corner by the door needs to stay on for a minute any time of day and two others only at night. This works:

luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },11)
luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },12)
luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = "20"},14)
luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },5)
luup.call_delay( 'switch_off', 60) -- Call the switch off function after a delay of 60 seconds
function switch_off()
   luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },11)
   luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },12)
   luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },14)
end

But I have tried using:

if ( luup.is_night() ) then
  .....
else
  .....
end

in several variations so that two of the lights only remain on for an extra minute at night, and I can't figure it out. How would this be done correctly?

thanks,

Online jswim788

  • Hero Member
  • *****
  • Posts: 537
  • Karma: +29/-2
Re: Probably a simple question about night or day
« Reply #1 on: February 08, 2017, 06:46:40 pm »
I'm sure you'll get several responses saying "use PLEG" which is probably a good option.  And you'll get some comments that using a delay of more than a second or two isn't good practice because the Vera might restart in that time period.

But let's see if we can do it in lua:

Code: [Select]
-- always turn off device 5:
luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },5)

-- function to turn off desired lights:
function switch_off()
   luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },11)
   luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },12)
   luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },14)
end

-- night check:
if luup.is_night() then
  -- turn on or keep on some lights temporarily:
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },11)
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },12)
  luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = "20"},14)
  -- call function to turn them off after 60 seconds:
  luup.call_delay( 'switch_off', 60) -- Call the switch off function after a delay of 60 seconds
else
  -- turn off lights immediately
  switch_off()
end

return true
I'm no expert, but perhaps this might help or others will chime in with better solutions...

Offline Mike Yeager

  • Sr. Member
  • ****
  • Posts: 455
  • Karma: +6/-12
Re: Probably a simple question about night or day
« Reply #2 on: February 09, 2017, 12:05:12 pm »
I agree with using either PLEG or Rules Engine. Either way is a bit more intuitive and easier to manage...