Author Topic: Last sunday of March check  (Read 877 times)

Offline tony-park

  • Jr. Member
  • **
  • Posts: 75
  • Karma: +1/-0
Last sunday of March check
« on: February 26, 2018, 02:47:00 am »
Hi,

please can someone check my coding for the above- its attached to a scene that runs on Sunday at 1am:

Code: [Select]
  local v_time = os.date("*t")
  local v_wday = (v_time.wday)
  local v_day  = (v_time.day)
  local v_month = (v_time.month)
  local v_hour = (v_time.hour)
  local v_minute = (v_time.min)
 
  if v_month == 3 THEN -- Check if its March
   if v_day > 24 and v_day <=31 THEN  -- last sunday of month   
      return true
     end
   else
     return false
 end

The scene is set to change a virtual device, which is used as a flag to show whether we're in GMT or not.  However the scene seems to be changing the flag anyway.


Thanks
Tony

Offline akbooer

  • Master Member
  • *******
  • Posts: 5963
  • Karma: +258/-69
  • "Less is more"
Re: Last sunday of March check
« Reply #1 on: February 26, 2018, 03:26:15 am »
Not quite sure what you're trying to do here, but picking up on your last comment the os.date() table has a flag called isdst which indicates DST or not (assuming that's what you're meaning by GMT?)
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline tony-park

  • Jr. Member
  • **
  • Posts: 75
  • Karma: +1/-0
Re: Last sunday of March check
« Reply #2 on: February 26, 2018, 03:36:08 am »
Hi,

yep, trying to check for the last Sunday in March. Not sure that DST and GMT always align, so looking to try and code it independently. So, it depends on whether DST on a vera based in the UK, is actually GMT/BST or the US date. For example, this year,  GMT changes on 25th March, but US DST comes into play on 11th March.

If this could be confirmed, that would be great.


Thanks
Tony


Offline akbooer

  • Master Member
  • *******
  • Posts: 5963
  • Karma: +258/-69
  • "Less is more"
Re: Last sunday of March check
« Reply #3 on: February 26, 2018, 03:45:05 am »
It's based on the locale of the underlying OS.  If you have that set right, then isdst indicates the expected GMT/BST for a UK-based machine.   Similarly in the US, or anywhere else.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline akbooer

  • Master Member
  • *******
  • Posts: 5963
  • Karma: +258/-69
  • "Less is more"
Re: Last sunday of March check
« Reply #4 on: February 26, 2018, 03:53:30 am »
Easy to check...

Code: [Select]
local t1 = os.date ("*t", os.time {year=2018, month=3, day=24})
print (t1.isdst)
local t2 = os.date ("*t", os.time {year=2018, month=3, day=25})
print (t2.isdst)

gives...

Code: [Select]
false
true
« Last Edit: February 26, 2018, 05:04:26 am by akbooer »
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline tony-park

  • Jr. Member
  • **
  • Posts: 75
  • Karma: +1/-0
Re: Last sunday of March check
« Reply #5 on: March 07, 2018, 03:43:23 am »
Hi,

thanks for your help so far...

currently my scene trigger has the following code:

local function kwikLog(message, clear)
  local socket = require("socket")
  local time = socket.gettime() or os.time()
  local tms = string.format(".%03d ",math.floor (1000 * (time % 1)))
  local stamp = os.date("%d %b %Y %T",math.floor(time)) .. tms
  local mode = "a+"
  if clear then mode = "w+" end
  local file = io.open("/www/kwikLog.txt", mode)
  file:write(stamp .. (message or "") .. "\n")
  file:close()
end

  local v_time = os.date ('*t')
  local v_dst = (v_time.isdst)
   kwikLog("GMT_Test")

 
if v_dst then
  kwikLog("return dst true")
  return true
else
  kwikLog("return dst false")
  return false
end



When I run the trigger, I'm not seeing that the kwikLog is outputing anything at all for this code. I can run the code in the test lua code, and it outputs, but as the trigger lua code, nada. Any ideas why this would be?

The trigger is picking up a virtual device being turned on, and I can see that the scene is being executed, as its actually performing the actions defined - however currently it shouldn't be actioning them.

Any thoughts on why this code isn't actually being called, gratefully accepted!

Thanks
Tony

Offline akbooer

  • Master Member
  • *******
  • Posts: 5963
  • Karma: +258/-69
  • "Less is more"
Re: Last sunday of March check
« Reply #6 on: March 07, 2018, 07:55:50 am »
Have you tried putting this in the scene Lua rather than a trigger?

It's a timer running the scene, not a trigger.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline tony-park

  • Jr. Member
  • **
  • Posts: 75
  • Karma: +1/-0
Re: Last sunday of March check
« Reply #7 on: March 07, 2018, 08:45:12 am »
Hi,

do you mean in step 3 - "also, execute the following Luup code:"?

Originally I had a 2am timer on Sunday morning to kick off the check, however in order to use the LUA condition, I created a virtual device, which is then changed from off to on at 2am on Sunday, this then allows me to have a device based trigger with conditional code attached.

Thanks again!
Tony
« Last Edit: March 07, 2018, 08:49:09 am by tony-park »

Offline akbooer

  • Master Member
  • *******
  • Posts: 5963
  • Karma: +258/-69
  • "Less is more"
Re: Last sunday of March check
« Reply #8 on: March 07, 2018, 08:59:49 am »
The conventional approach would be

timer (as you had originally) -> Lua scene code (yes, step 3) -> scene actions.

It seems you are making life difficult for yourself by involving a virtual switch?
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline tony-park

  • Jr. Member
  • **
  • Posts: 75
  • Karma: +1/-0
Re: Last sunday of March check
« Reply #9 on: March 07, 2018, 09:14:26 am »
Hi,

I'm happy to go with any approach, so long as it works! Yep, the virtual switch wasn't the route I wanted to go, but with the lua code not being fired, and the references seem to be implying that code should be attached in a trigger.

Seriously, I'm happy to go with a route, so long as that route works.  I'm just seeing currently, that the code isn't actually being used, either when attached to the trigger, or to the luup code. I've run the script via the test script, and can see that the kwikLog actually reports details.


Thanks
Tony

Offline tony-park

  • Jr. Member
  • **
  • Posts: 75
  • Karma: +1/-0
Re: Last sunday of March check
« Reply #10 on: March 13, 2018, 06:36:57 pm »
Hi,

so the latest attempt sees me trying to create an xml file with the lua code in:

Code: [Select]
function isBST()

local v_time = os.date ("*t", os.time {year=2018, month=3, day=10})
        local v_dst = (v_time.isdst)

if v_dst then
return 1
else
return 0
end
end

This compiles ok, and can be called from the test luup code:

Code: [Select]
function kwikLog(message, clear)
  local socket = require("socket")
  local time = socket.gettime() or os.time()
  local tms = string.format(".%03d ",math.floor (1000 * (time % 1)))
  local stamp = os.date("%d %b %Y %T",math.floor(time)) .. tms
  local mode = "a+"
  if clear then mode = "w+" end
  local file = io.open("/www/kwikLog.txt", mode)
  file:write(stamp .. (message or "") .. "\n")
  file:close()
end

local t1 = os.date ("*t", os.time {year=2018, month=3, day=10})
local t2 = os.date ("*t", os.time {year=2018, month=3, day=25})

local bst=isBST

if bst==0 then
  kwikLog(" bst value is false")
--  return 0
else
  kwikLog(" bst value is true")
--  return 1
end

if (t1.isdst) then
   kwikLog (" t1 dst true")
else
   kwikLog (" t1 dst false")
end

if t2.isdst then
kwikLog ("t2.isdst true")
else
kwikLog ("t2.isdst false")
end


So, I've got the function isBST hardcoded, to 10th March 2018 (which is definitely in GMT, not BST), and local variables t1 and t2 hard coded within the test script.

Now, when the function is called, it doesn't appear to matter what date is hard coded, it always returns true. However when the test code is run t1 returns false, and t2 returns true (as expected). Why does the function handle this differently?

13 Mar 2018 22:30:44.036  bst value is true
13 Mar 2018 22:30:44.040  t1 dst false
13 Mar 2018 22:30:44.044 t2.isdst true


bst value should match with t1 :-(

Thanks
Tony

Offline akbooer

  • Master Member
  • *******
  • Posts: 5963
  • Karma: +258/-69
  • "Less is more"
Re: Last sunday of March check
« Reply #11 on: March 13, 2018, 06:49:06 pm »
I believe that

Code: [Select]
local bst=isBST

should be

Code: [Select]
local bst=isBST ()

No?
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline tony-park

  • Jr. Member
  • **
  • Posts: 75
  • Karma: +1/-0
Re: Last sunday of March check
« Reply #12 on: March 13, 2018, 06:58:45 pm »
Unfortunately, when I try calling the function with the () after, the code fails:

Failed to test code, please try again


Without the brackets, it completes.

Thanks!

Offline rigpapa

  • Sr. Member
  • ****
  • Posts: 462
  • Karma: +66/-0
Re: Last sunday of March check
« Reply #13 on: March 13, 2018, 07:09:41 pm »
I was able to run your code successfully with the parens as @akbooer suggested, but stubbing out your fancy log function to use just plain luup.log. Here's the output:

08   03/13/18 19:07:33.463   JobHandler_LuaUPnP::HandleActionRequest device: 0 service: urn:micasaverde-com:serviceId:HomeAutomationGateway1 action: RunLua <0x6f2ad520>
08   03/13/18 19:07:33.464   JobHandler_LuaUPnP::HandleActionRequest argument id=lu_action <0x6f2ad520>
08   03/13/18 19:07:33.464   JobHandler_LuaUPnP::HandleActionRequest argument serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1 <0x6f2ad520>
08   03/13/18 19:07:33.464   JobHandler_LuaUPnP::HandleActionRequest argument action=RunLua <0x6f2ad520>
08   03/13/18 19:07:33.464   JobHandler_LuaUPnP::HandleActionRequest argument Code=function isBST()
   
        local v_time = os.date ("*t", os.time {year=2018, month=3, day=10})
        local v_dst = (v_time.isdst)

        if v_dst then
            return 1
        else
            return 0
        end
    end
   
function kwikLog(message, clear)
  luup.log(message)
end

t1 = os.date ("*t", os.time {year=2018, month=3, day=10})
t2 = os.date ("*t", os.time {year=2018, month=3, day=25})

bst=isBST()

if bst==0 then
  kwikLog(" bst value is false")
--  return 0
else
  kwikLog(" bst value is true")
--  return 1
end

if (t1.isdst) then
   kwikLog (" t1 dst true")
else
   kwikLog (" t1 dst false")
end

if t2.isdst then
kwikLog ("t2.isdst true")
else
kwikLog ("t2.isdst false")
end <0x6f2ad520>
50   03/13/18 19:07:33.466   luup_log:0:  bst value is false <0x6f2ad520>
50   03/13/18 19:07:33.466   luup_log:0:  t1 dst false <0x6f2ad520>
50   03/13/18 19:07:33.466   luup_log:0: t2.isdst true <0x6f2ad520>
Author of Rachio, Deus Ex Machina II, Intesis WMP Gateway, SiteSensor, DelayLight, Auto Virtual Thermostat and VirtualSensor plugins. Vera Plus w/100+ Z-wave devices. Vera3 sandbox.

Offline tony-park

  • Jr. Member
  • **
  • Posts: 75
  • Karma: +1/-0
Re: Last sunday of March check
« Reply #14 on: March 13, 2018, 07:31:33 pm »
Hi,

thanks for the input, mine is still behaving oddly!

08   03/13/18 23:29:14.984   JobHandler_LuaUPnP::HandleActionRequest argument Code=function kwikLog(message, clear)
  luup.log(message)
end

t1 = os.date ("*t", os.time {year=2018, month=3, day=10})
t2 = os.date ("*t", os.time {year=2018, month=3, day=25})

bst=isBST

if bst==0 then
  kwikLog(" bst value is false")
--  return 0
else
  kwikLog(" bst value is true")
--  return 1
end

if (t1.isdst) then
   kwikLog (" t1 dst true")
else
   kwikLog (" t1 dst false")
end

if t2.isdst then
kwikLog ("t2.isdst true")
else
kwikLog ("t2.isdst false")
end <0x6b528520>
50   03/13/18 23:29:14.986   luup_log:0:  bst value is true <0x6b528520>
50   03/13/18 23:29:14.986   luup_log:0:  t1 dst false <0x6b528520>
50   03/13/18 23:29:14.986   luup_log:0: t2.isdst true <0x6b528520>

I've got my location details set to UK, England, Fenwick.

Thanks
Tony