os.difftime not working?

I was looking into an issue with my AltUI scenes today and determined that the lua os.difftime() call doesn?t appear to be working properly in openLuup. I?m not on the latest/greatest version so this may have been addressed already, my apologies if so but thought I?d pass it along. Oddly enough I didn?t really even need this function as you can simply cache values of os.time() and do simple subtraction (maybe I was misunderstanding what this function should do?)

Thanks again for all the hard work Ak, love openLuup!

This is not something that openLuup has anything to do with. It’s just part of the basic Lua system.

Definition here: https://www.lua.org/manual/5.1/manual.html#pdf-os.difftime

For most systems, it IS the same as subtracting two epochs. Perhaps you thought it did something else?

it works but if ur using a motion sensor tripp time i found that just using “tripped” state works petter that usinf the os.difftime() calculation with last tripped, what i was doing was motion detection on lights

[code]function checkLastTrip()
local lastTripmotion = luup.variable_get (SS_SID, “LastTrip”, KitchenMotion) --or os.time()
local Tripstate = luup.variable_get (SS_SID, “Tripped”, KitchenMotion)
local Ostime = os.time()
local osdifftime = os.difftime (tonumber (os.time()), tonumber (lastTripmotion))
luup.log("Os Time = " … Ostime … ", LastTrip = " … lastTripmotion … ", Diff = " … osdifftime … ", Trip state = " … Tripstate … “.”)
if (Tripstate == “0”) then
–(tonumber (os.difftime (tonumber (os.time()), tonumber (lastTripmotion))) >= tonumber (period))

	luup.log("run Scene to dimm lights")
	luup.call_action(PH_SID, "RunHueScene", {hueSceneID= Kitchendimall }, BurnsedPl) -- Brighten Scene
	luup.log("Re-dimm the light Scene")
    luup.call_action(SD_SID, "SetLoadLevelTarget", {newLoadlevelTarget= tonumber (Dimmlvl)}, HueKitchen1) -- Re-dimm the light.
	luup.log("Re-dimm the light.")
    luup.call_action(SD_SID, "SetLoadLevelTarget", {newLoadlevelTarget= tonumber (Dimmlvl)}, HueKitchen1) -- Re-dimm the light.
	luup.log("Re-dimm the light.")
	luup.call_action(SD_SID, "SetLoadLevelTarget", {newLoadlevelTarget= tonumber (Dimmlvl)}, HueKitchen2) -- Re-dimm the light.	
	luup.log("Re-dimm the light.")
    luup.call_action(SD_SID, "SetLoadLevelTarget", {newLoadlevelTarget= tonumber (Dimmlvl)}, HueKitchen3) -- Re-dimm the light.
	luup.log("Re-dimm the light.")
	luup.call_action(SD_SID, "SetLoadLevelTarget", {newLoadlevelTarget= tonumber (Dimmlvl)}, HueKitchen4) -- Re-dimm the light.				
else
    luup.call_delay ("checkLastTrip", period) -- Check when the sensor was last tripped every <period> seconds.
end

end

luup.call_delay (“checkLastTrip”, period)

return true
[/code]

i isued that instead and changed my motion sensor timeout to 1 min

Thanks all. The manual calculation is the way I eventually worked around it as well.

Glad about that, but I’m still struggling to understand what is, apparently, not working?

If it’s my fault, and it’s broken, then I need to fix it.

Sorry Ak, os.difftime() wasn?t working as I expected with my cached os.time() values, but as you said, that comes from Lua, not openLuup, so nothing for you thankfully. Simply my misunderstanding of what openLuup provides.

The pseudo code logic I was implementing was something like:

luup.variable_set(SES_SID, “LastTrip”, os.time(), sensorID)

… and then sometime later …

local lastTripped = luup.variable_get(SES_SID, “LastTrip”, lug_motionSensorID) if os.difftime(os.time(), lastTripped) > 10 then

That os.difftime() would yield true even when the calculation of os.time() - lastTripped should have been 0. Switching to the simple subtraction worked perfectly. Maybe I?m misunderstanding os.difftime()

This surprises me, and is not in line with my experience.

os.difftime() simply produces a numeric result, which may be positive, zero, or negative.

[quote=“bwillette, post:6, topic:199547”]The pseudo code logic I was implementing was something like:

luup.variable_set(SES_SID, “LastTrip”, os.time(), sensorID)

… and then sometime later …

local lastTripped = luup.variable_get(SES_SID, “LastTrip”, lug_motionSensorID) if os.difftime(os.time(), lastTripped) > 1…[/quote]
I think I see.
luup.variable_get() returns LastTrip as a string. Os.difftime sems to give odd results in this case, perhaps attempting to parse it (badly) as a date/time string, not a Unix timestamp.

The above was determined by testing with Lua command line on my Plus.

Interesting. I had thought of that and tested it on openLuup (RPi), and it didn’t seem to mind strings. I didn’t try it on Vera!

I have been using the os.difftime function in my scene and some of my modified plugins for openLuup. Have not have any problems but I have had to make sure that the arguments were always numbers indeed.