Last sunday of March check

Hi,

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

[code] 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 [/code]

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

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?)

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

It’s based on the locale of the underlying OS. If you have that set right, then [tt]isdst[/tt] indicates the expected GMT/BST for a UK-based machine. Similarly in the US, or anywhere else.

Easy to check…

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…

false
true

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

Have you tried putting this in the scene Lua rather than a trigger?

It’s a timer running the scene, not a trigger.

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

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?

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

Hi,

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

[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[/code]

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

[code]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[/code]

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 :frowning:

Thanks
Tony

I believe that

local bst=isBST

should be

local bst=isBST ()

No?

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!

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:

[tt]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>[/tt]

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

You appear still to be missing the vital parentheses.

Hi AK,

when I put in the ()s, the code fails to run. Without them, it at least attempts to run. That is the reason.

Thanks
Tony

I don’t see a definition of the isBST function in your latest version.

That being the case, then

bst=isBST

if bst==0 then ...

will result in bst being nil and the if statement test being false.

Or

bst=isBST ()

results in an error, because you are trying to call a nil function.

When testing code, simplify, simplify, simplify. There’s just too many, rather baroque, constructs in there at the moment to see the wood for the trees.

Hi,

unfortunately the code is as simple as it can possibly be!

I have function written in an i_…xml file, uploaded.

I have a script which calls the function, and then tests 2 commands which we can see work fine.

I’ve even tried adjusting the function, to actually return 0 only, and its still showing as true. I’ve also tried using the commands you provided where the return returns the .isdst variable directly, and still the same.

I don’t think what I’m asking is too much, but maybe, something this simple, is just too simple for a home automation system?

Thanks
Tony

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