We have moved at community.getvera.com

Author Topic: os.difftime not working?  (Read 492 times)

Offline bwillette

  • Sr. Newbie
  • *
  • Posts: 47
  • Karma: +3/-0
os.difftime not working?
« on: July 28, 2018, 01:46:32 pm »
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!

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: os.difftime not working?
« Reply #1 on: July 28, 2018, 02:09:40 pm »
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?
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 RichieB

  • Full Member
  • ***
  • Posts: 131
  • Karma: +2/-0
Re: os.difftime not working?
« Reply #2 on: July 28, 2018, 03:06:49 pm »
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: [Select]
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

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

Offline bwillette

  • Sr. Newbie
  • *
  • Posts: 47
  • Karma: +3/-0
Re: os.difftime not working?
« Reply #3 on: July 28, 2018, 09:56:27 pm »
Thanks all.  The manual calculation is the way I eventually worked around it as well.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: os.difftime not working?
« Reply #4 on: July 29, 2018, 02:22:26 am »
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.

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 bwillette

  • Sr. Newbie
  • *
  • Posts: 47
  • Karma: +3/-0
Re: os.difftime not working?
« Reply #5 on: July 29, 2018, 08:09:35 am »
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()

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: os.difftime not working?
« Reply #6 on: July 29, 2018, 09:45:57 am »
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.
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 rigpapa

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1121
  • Karma: +187/-3
Re: os.difftime not working?
« Reply #7 on: July 29, 2018, 10:02:51 am »
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...
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.
Author of Reactor, DelayLight, SiteSensor, Rachio, Deus Ex Machina II, Intesis WMP Gateway, Auto Virtual Thermostat and VirtualSensor plugins. Vera Plus w/100+ Z-wave devices. Vera3, Lite. Hassio, Slapdash.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: os.difftime not working?
« Reply #8 on: July 29, 2018, 11:20:04 am »
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!
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 rafale77

  • Community Beta
  • Hero Member
  • ******
  • Posts: 1749
  • Karma: +101/-27
  • HA ≠ IoT as a blue sky is cloudless.
Re: os.difftime not working?
« Reply #9 on: July 30, 2018, 10:12:05 pm »
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.
openLuup (79 devices, 141 scenes, 19 apps) master to VeraPlus (142 zwave nodes, 8 Zigbee nodes, 221 devices,  20 scenes , 2 apps) +  Hubitat (15 Zigbee nodes) + Home-Assistant (API Integrations). Bridged to Siri and Alexa. Homewave. VeraPlus ExtRooted and mios server independent.