Author Topic: Time of day exception  (Read 361 times)

Offline pulpman52

  • Sr. Newbie
  • *
  • Posts: 38
  • Karma: +1/-0
Time of day exception
« on: May 16, 2017, 07:52:44 am »
Prior to the latest update I had a scene which worked as expected but now it has stopped working correctly. Since I noticed the issue I have done a lot of troubleshooting and I don't know if the script I have now is verbatim to what I had working before. Below is the current script:

Code: [Select]
local function day()
local dID = 45             -- Device ID of your DayTime plugin
local allow = false       -- 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 == "1") == allow)
end
if(luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", 33)=="0") then
     luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue ="1"}, 33) -- Recessed
end

There are 2 sections to this scrip, when local function Day() is the only section it works as expected in this and other scenes. When the "Recessed" portion is the only section it also works. When both sections are present only the second section (--Recessed) executes. I have tried to rewrite this several different ways and nothing seems to get both sections to work. Like I said I am fairly confident that this script, as written above, worked before the latest Firmware update.

Can anyone help me rewrite this so that it works as intended?

Thank you for your help.

Offline jswim788

  • Sr. Member
  • ****
  • Posts: 427
  • Karma: +15/-2
Re: Time of day exception
« Reply #1 on: May 16, 2017, 10:05:14 am »
You want the 'if' statement to run if the day() function is true?  Then this:
Code: [Select]
local function day()
  local dID = 45             -- Device ID of your DayTime plugin
  local allow = false       -- 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 == "1") == allow)
end

if day() then
  if(luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", 33)=="0") then -- need this check?
       luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue ="1"}, 33) -- Recessed
  end
end
return true -- do you need the return for the scene to be conditional based on something above?
By the way, it is easier to use the luup.is_night(), but the DayTime plugin allows offsets which you might be using.

Also, I don't know if you rely on the return from this code for the scene.  Do you want it true only if it is night?  Or if the switch was turned on?

Lastly, do you need to check that device 33 is off before turning it on?  Usually there's no harm in turning on something even if it is already on.  But maybe your device 33 is special?
« Last Edit: May 16, 2017, 11:44:44 am by jswim788 »

Offline pulpman52

  • Sr. Newbie
  • *
  • Posts: 38
  • Karma: +1/-0
Re: Time of day exception
« Reply #2 on: May 16, 2017, 02:52:18 pm »
This code is placed in the main Luup code section not in the device triggers section. What I expect to happen is the device triggers the scene, the scene first executes this Luup code. In doing so it would first run the local function day(). If the DayTime plugin reports that it is day time then it should exit the Luup code and exit the scene (performing no actions). If the DayTime plugin reports that it is night time then presumably it would then execute the 'if 33 is on' statement and then execute the remainder of the scene, which includes a few other devices set through the GUI.

I will try your suggestion tonight and see if it produces the results I expect... Thank you.

I do use the offsets that DayTime allows for, it also provides a visual indicator for what period Vera thinks it is (day or night).

I only rely on the return to allow further execution of the scene. I check if device 33 is on because if a user has set the dimmer to some value not 0 then I don't want to override it with this scene; the light should only turn on if it was previously off and it should remain at x% if it was previously on.

Offline jswim788

  • Sr. Member
  • ****
  • Posts: 427
  • Karma: +15/-2
Re: Time of day exception
« Reply #3 on: May 16, 2017, 04:31:06 pm »
if you want the scene execution based on the day() function as well, then the return needs to be modified as shown below:
Code: [Select]
local function day()
  local dID = 45             -- Device ID of your DayTime plugin
  local allow = false       -- 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 == "1") == allow)
end

if day() then
  if(luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", 33)=="0") then
       luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue ="1"}, 33)
  end
  return true -- day function returned true - should it be called night instead?
else
  return false  -- day function returned false
end

Offline pulpman52

  • Sr. Newbie
  • *
  • Posts: 38
  • Karma: +1/-0
Re: Time of day exception
« Reply #4 on: May 16, 2017, 09:25:24 pm »
So the theory is that if day() returns false then the scene exits both Luup and GUI execution. So there shouldn't be any need to put the second (device 33) snippet in a if day() statement. I got the code from here (http://forum.micasaverde.com/index.php/topic,18679.msg141954.html#msg141954)

in this scene the device 33 snippet runs as expected in both day and night conditions but the GUI devices execute in both states. I use this day() function in several scenes and they all work correctly but the others only have GUI switches. This is the only scene that uses a day() function and has additional Luup code. in this scene the GUI portion that isn't working correctly, it always executes. Also i tried your suggested version and the results were the same; Set the Daytime plugin to day and triggered the scene. Device 33 didn't respond because it was day time but the GUI switches turned on in both day and night conditions.

GUI switches are devices set to change by the GUI interface of the scene editor.

Offline jswim788

  • Sr. Member
  • ****
  • Posts: 427
  • Karma: +15/-2
Re: Time of day exception
« Reply #5 on: May 16, 2017, 11:18:16 pm »
Try adding some log lines and take a look in your log file.

Code: [Select]
local function day()
  local dID = 45             -- Device ID of your DayTime plugin
  local allow = false       -- true runs scene during daytime, false runs it at night
  local status = luup.variable_get("urn:rts-services-com:serviceId:DayTime","Status",dID)
  luup.log("day function status is: " .. status)
  return ((status == "1") == allow)
end

if day() then
  if(luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", 33)=="0") then
       luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue ="1"}, 33)
  end
  luup.log("day() returned true, returning TRUE to scene")
  return true -- day function returned true - should it be called night instead?
else
  luup.log("day() returned false, returning FALSE to scene")
  return false  -- day function returned false
end