We have moved at community.getvera.com

Author Topic: Lua Time variable  (Read 475 times)

Offline Tlex

  • Sr. Newbie
  • *
  • Posts: 49
  • Karma: +1/-0
Lua Time variable
« on: November 27, 2018, 11:06:11 am »
Good day,

I'm trying to display the current time via pushover and it works but I have issues when the minutes only have one digits. Let say it's 14h02, the system will return 14h2. Is their a better way I could do that ?

hours = os.date('*t',os.time())["hour"]
minutes   = os.date('*t',os.time())["min"]
pushover ("Door open at ",hours .. ":" .. minutes, "1", " ", " ", "Gamelan")

Thanks for your help !

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +290/-70
  • "Less is more"
Re: Lua Time variable
« Reply #1 on: November 27, 2018, 11:12:21 am »
Yes, absolutely.  The system function os.date() can do all you need:

Code: [Select]
local t = os.date "%Hh%M"
print(t)

will produce (just now, on my system):

Code: [Select]
16h08

So I imagine that this should work:

Code: [Select]
local t = os.date "%Hh%M"
pushover ("Door open at ",t, "1", " ", " ", "Gamelan")
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 Tlex

  • Sr. Newbie
  • *
  • Posts: 49
  • Karma: +1/-0
Re: Lua Time variable
« Reply #2 on: November 27, 2018, 11:33:14 am »
Forgot to mention I trying that within pleg....
I tried local t = os.date "%Hh%M" but it fail...

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +290/-70
  • "Less is more"
Re: Lua Time variable
« Reply #3 on: November 27, 2018, 11:43:06 am »
Perhaps the pushover call is incorrect somehow?
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 Tlex

  • Sr. Newbie
  • *
  • Posts: 49
  • Karma: +1/-0
Re: Lua Time variable
« Reply #4 on: November 27, 2018, 07:10:27 pm »
Nope, its working with pushover with the code I showed but not with "local t = os.date "%Hh%M" :(
maybe thats something with pleg...

Offline rafale77

  • Community Beta
  • Hero Member
  • ******
  • Posts: 1749
  • Karma: +101/-27
  • HA ≠ IoT as a blue sky is cloudless.
Re: Lua Time variable
« Reply #5 on: December 03, 2018, 01:03:40 pm »
I think it is. I do not use PLEG and am pushing all my notifications through pushover and for me it is working.
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.

Offline ZW-Tom

  • Jr. Member
  • **
  • Posts: 76
  • Karma: +8/-2
Re: Lua Time variable
« Reply #6 on: January 04, 2019, 11:17:32 pm »
Good day,

I'm trying to display the current time via pushover and it works but I have issues when the minutes only have one digits. Let say it's 14h02, the system will return 14h2. Is their a better way I could do that ?

hours = os.date('*t',os.time())["hour"]
minutes   = os.date('*t',os.time())["min"]
pushover ("Door open at ",hours .. ":" .. minutes, "1", " ", " ", "Gamelan")

Thanks for your help !

Try this:

 local minNum = tonumber(t.min)
 if minNum <= 9 then
  minStr = tostring(minNum)
  minStr = "0" .. minStr
 else
  minStr = t.min
 end

Hope that helps.
Tom