We have moved at community.getvera.com

Author Topic: Vera Sysmon : Looking for Lua code to do a few things  (Read 2242 times)

Offline vincenthimpe

  • Sr. Newbie
  • *
  • Posts: 28
  • Karma: +0/-1
Vera Sysmon : Looking for Lua code to do a few things
« on: July 18, 2011, 12:00:03 pm »
I built a hardware extender for Vera that solves a couple of problems i have with the thing. ( Realtime clock , watchdog , UPS , status display , IP address etc ) ( see my post on 'SysMon' in 'general' for more info
The hardware and firmware are running. now onto the Lua code.

i am looking for a few simple things. Instead of reinventing the wheel and spending day reading all the documentation i will 'crowdsource'.

Qustion1 : Clock related
How can i read the current date and timefrom a LUA script and store this in a variable ?
How can i SET the current date and time from within a LUA script?

My hardware extender has a true battery backed RTC with perpetual calender. If Vera reboots and has no NNTP access i want to have a script that can read the RTC and set vera's time/date. I will make a simple script : retrieve system timedate , retrieve RTC timedate. see who has the most recent timestamp -> commit to either vera or NNTP. This script will fire once every 5 minutes or so. that way the RTC is kept up to date with NNTP ( if there is NNTP ) or vera will keep up to date with the RTC.

question2: Network related
How can i read Vera's IP address. Sysmon has a status line. One of the things i want to do is show vera's Ip address.

Any idea's are welcome.

Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: Vera Sysmon : Looking for Lua code to do a few things
« Reply #1 on: July 18, 2011, 12:35:25 pm »
How can i read the current date and timefrom a LUA script and store this in a variable ?

os.time() will get the current time in Unix timestamp format (seconds since epoch).

os.date() will get the current time and date in human readable format.

How can i SET the current date and time from within a LUA script?

You can set the date and time with os.execute ('date -s "2 OCT 2006 18:00:00"').

How can i read Vera's IP address. Sysmon has a status line. One of the things i want to do is show vera's Ip address.

cat /var/state/network will show you network info for Vera. Here is the function to execute a command and return the output:

Code: [Select]
-------------------------------------------------------------------------------------
-- Execute the command and return the stdout.
-------------------------------------------------------------------------------------
local function execute (command)

    local file = io.popen (command)
    local data = file:read ("*a")
    file:close()

    -- We want to return nil instead of the empty string.
    if data == "" then
        data = nil
    end

    return data
end

If you want only the IP address you can get it with:

Code: [Select]
local ipAddr = execute ("cat /var/state/network | grep network.wan.ipaddr")
ipAddr = ipAddr:match (".*=(.*)")