We have moved at community.getvera.com

Author Topic: openLuup: Tips & Tricks  (Read 6729 times)

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
openLuup: Tips & Tricks
« on: October 27, 2015, 11:30:22 am »
openLuup saves performance data as attributes in the user_data.json file, which is updated every 6 minutes.  You can configure ALTUI to run a command to give you a glimpse of this under the Misc > OS Command page.  Save this command line with the label "Stats" or such like:

Code: [Select]
cat user_data.json | grep Stats_

Running it gives output like this:

Code: [Select]
  "Stats_CpuLoad":"4.15%",
  "Stats_Memory":"9.2Mb",
  "Stats_Uptime":"3.96 days",

The same data is accessible with the luup.attr_get call.
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 akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: openLuup: Tips & Tricks
« Reply #1 on: October 28, 2015, 11:10:15 am »
The Misc > Lua Startup Code page of ALTUI shows, unsurprisingly, the startup code executed by the system on each reload (this is not to be confused with the startup.lua file used for initialising an new system.)

Sadly, though, under openLuup, editing this window does not save any changes made (it's another port 49451 problem.)

Happily enough, you shouldn't really have to do that...  if you define it just once to have this code...

Code: [Select]
require "my_restart_file"

... then you can simply use an external editor to put whatever you like into the contents of my_restart_file.lua and it will be executed on every restart.

Alternatively, you can use code like this executed in the Lua Test window to change the startup code:
Code: [Select]
luup.attr_set ("StartupCode",  [[

-- just write any Lua code here which will be put into the new Startup code
-- the good thing here is that you don't have to quote quotation marks - any sort will do...

luup.log ("This message is made from " .. 'two differently quoted strings')

]])
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 akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: openLuup: Tips & Tricks
« Reply #2 on: November 02, 2015, 02:47:22 pm »
Whilst you can (now, in Release 5.5) happily create a local device interactively on openLuup using either a bit of test code with luup.create_device() or using the +Create button on the AltUI interface, I'd also recommend that you maintain a startup.lua script which does the same for all your local devices and plugins.

That way if, for whatever reason, you can't reload your system configuration from the user_data.json file, you can always recreate most of it on a fresh system by running the startup.lua file.

Equally, since the user_data.json file is not encoded but it is pretty-printed, it's actually really easy to edit this, for example, to remove an unwanted variable from a device.
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 akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: openLuup: Tips & Tricks
« Reply #3 on: November 04, 2015, 03:24:18 am »
If, for some reason, you want to revert to any other version of AltUI, you can use this HTTP request:

Code: [Select]
http://<VeraIP>:3480/data_request?id=action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=CreatePlugin&PluginNum=8246&TracRev=859

...replacing the '859' with whatever build version you like (note that this is NOT the same as the MiOS App store version number, but it is the last part of the version number which AltUI reports at the bottom of the page.)

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 akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: openLuup: Tips & Tricks
« Reply #4 on: December 14, 2015, 11:53:30 am »
I don't want [remote scenes] to appear amongst the locally created scenes and it seems you cannot hide scenes like you can with devices.

Remote scenes are assigned to the new room created specifically for the remote Vera, so simply selecting 'No Room' rather than 'All' will remove them from the display.
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 vosmont

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 687
  • Karma: +60/-8
Re: openLuup: Tips & Tricks
« Reply #5 on: February 26, 2016, 08:57:07 am »
You can add a favicon in /etc/cmh-ludl by copying (for example) the file https://vera-eu-oem-relay12.mios.com/www/1.7.1707-en/favicon.ico

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: openLuup: Tips & Tricks
« Reply #6 on: March 20, 2016, 05:58:05 pm »
Since openLuup now searches the directory /etc/cmh-lu/ in addition to /etc/cmh-ludl/ for device and program files, you can define a symbolic link in /etc/ called cmh-lu which points to the /etc/cmh-ludl/files/ directory where the getfiles utility has downloaded them from the remote Vera.  This means you don't have to move any files manually, and it leaves the /etc/cmh-ludl/ directory relatively clear for logfiles and any development files you might be working on.

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 CudaNet

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1401
  • Karma: +42/-11
  • Chimichanga !
Re: openLuup: Tips & Tricks
« Reply #7 on: March 21, 2016, 12:33:02 pm »
I'll add this to the images and guide moving forward.

Since openLuup now searches the directory /etc/cmh-lu/ in addition to /etc/cmh-ludl/ for device and program files, you can define a symbolic link in /etc/ called cmh-lu which points to the /etc/cmh-ludl/files/ directory where the getfiles utility has downloaded them from the remote Vera.  This means you don't have to move any files manually, and it leaves the /etc/cmh-ludl/ directory relatively clear for logfiles and any development files you might be working on.
openLuup, AltUI, Zway and HomeWave, enough said...

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: openLuup: Tips & Tricks
« Reply #8 on: April 20, 2016, 03:36:51 pm »
An easy way to access the whole of the current log file is simply to make this HTTP request in your browser:

Code: [Select]
http://VeraIP:3480/LuaUPnP.log

or, of course, you can use the AltUI OS Command

Code: [Select]
tail -n 50 /var/log/cmh/LuaUPnP.log

...although this only shows device variable changes and scene executions.

For the tail of the complete current log, define a new OS command like this:

Code: [Select]
tail -n 100 LuaUPnP.log

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 amg0

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3174
  • Karma: +210/-8
Re: openLuup: Tips & Tricks
« Reply #9 on: April 20, 2016, 04:13:34 pm »
An easy way to access the whole of the current log file is simply to make this HTTP request in your browser:

Code: [Select]
http://VeraIP:3480/LuaUPnP.log

or, of course, you can use the AltUI OS Command

Code: [Select]
tail -n 50 /var/log/cmh/LuaUPnP.log

...although this only shows device variable changes and scene executions.

For the tail of the complete current log, define a new OS command like this:

Code: [Select]
tail -n 100 LuaUPnP.log

just in case, you can edit OS commands of ALTUI and add your prefered ones. click on the pencil

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: openLuup: Tips & Tricks
« Reply #10 on: May 01, 2016, 11:54:54 am »
Using AltUI to back up your user_data.json file

The latest build of AltUI (1608) adds a 'backup' button to the Controllers page.  On Vera, this backs up your configuration to a file on your browsing machine by running a CGI file.  Under openLuup, you can make this do anything you want by writing the appropriate WSAPI CGI Lua script.

The script file should go into /etc/cmh-ludl/cgi-bin/cmh/backup.sh and here's an example which simply backs up to a file in /etc/cmh-ludl/backups/.  The filename has the format backup.openLuup-00000000-2016-05-01, so only unique once a day.  The "00000000" is replaced by whatever PK_AccessPoint you've given your machine.

Code: [Select]
#!/usr/bin/env wsapi.cgi

module(..., package.seeall)

ABOUT = {
  NAME          = "backup.sh",
  VERSION       = "2016.04.30",
  DESCRIPTION   = "user_data backup script /etc/cmh-ludl/cgi-bin/cmh/backup.sh",
  AUTHOR        = "@akbooer",
  DOCUMENTATION = "https://github.com/akbooer/openLuup/tree/master/Documentation",
}

local DIRECTORY     = "backup"      -- change this if you want to backup elsewhere

-- WSAPI Lua implementation of backup.sh
-- backup written to ./backups/backup.openLuup-00000000-2015-01-21

local userdata = require "openLuup.userdata"
local lfs = require "lfs"

local _log    -- defined from WSAPI environment as wsapi.error:write(...) in run() method.


-- global entry point called by WSAPI connector

--[[

The environment is a Lua table containing the CGI metavariables (at minimum the RFC3875 ones) plus any
server-specific metainformation. It also contains an input field, a stream for the request's data,
and an error field, a stream for the server's error log.

The input field answers to the read([n]) method, where n is the number
of bytes you want to read (or nil if you want the whole input).

The error field answers to the write(...) method.

return values: the HTTP status code, a table with headers, and the output iterator.

--]]

function run (wsapi_env)
  _log = function (...) wsapi_env.error:write(...) end      -- set up the log output, note colon syntax -- 2016.02.26
 
  lfs.mkdir (DIRECTORY)
   
  local PK = userdata.attributes.PK_AccessPoint or "XXXXXXXX"
  local DATE = os.date "%Y-%m-%d" or "0000-00-00"
  local fmt = "%s/backup.openLuup-%s-%s"
  local fname = fmt: format (DIRECTORY, PK, DATE) 
  _log ("Backing up user_data to " .. fname)
 
  local ok, msg = userdata.save (nil, fname)   -- save current luup environment
 
  local status, return_content
  if ok then
    status, return_content  = 200, "backup completed"
  else
    status, return_content  = 500, "backup failed: " .. msg
  end
 
  local headers = {["Content-Type"] = "text/plain"}
 
  local function iterator ()     -- one-shot iterator, returns content, then nil
    local x = return_content
    return_content = nil
    return x
  end

  return status, headers, iterator
end

-----

You'll also need the latest commit from the openLuup GitHub development branch to make this work.
« Last Edit: May 01, 2016, 12:33:20 pm by akbooer »
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 konradwalsh

  • Hero Member
  • *****
  • Posts: 566
  • Karma: +19/-6
Re: openLuup: Tips & Tricks
« Reply #11 on: May 04, 2016, 10:02:37 am »
Using AltUI to back up your user_data.json file

The latest build of AltUI (1608) adds a 'backup' button to the Controllers page.  On Vera, this backs up your configuration to a file on your browsing machine by running a CGI file.  Under openLuup, you can make this do anything you want by writing the appropriate WSAPI CGI Lua script.

The script file should go into /etc/cmh-ludl/cgi-bin/cmh/backup.sh and here's an example which simply backs up to a file in /etc/cmh-ludl/backups/.  The filename has the format backup.openLuup-00000000-2016-05-01, so only unique once a day.  The "00000000" is replaced by whatever PK_AccessPoint you've given your machine.

Code: [Select]
#!/usr/bin/env wsapi.cgi

module(..., package.seeall)

ABOUT = {
  NAME          = "backup.sh",
  VERSION       = "2016.04.30",
  DESCRIPTION   = "user_data backup script /etc/cmh-ludl/cgi-bin/cmh/backup.sh",
  AUTHOR        = "@akbooer",
  DOCUMENTATION = "https://github.com/akbooer/openLuup/tree/master/Documentation",
}

local DIRECTORY     = "backup"      -- change this if you want to backup elsewhere

-- WSAPI Lua implementation of backup.sh
-- backup written to ./backups/backup.openLuup-00000000-2015-01-21

local userdata = require "openLuup.userdata"
local lfs = require "lfs"

local _log    -- defined from WSAPI environment as wsapi.error:write(...) in run() method.


-- global entry point called by WSAPI connector

--[[

The environment is a Lua table containing the CGI metavariables (at minimum the RFC3875 ones) plus any
server-specific metainformation. It also contains an input field, a stream for the request's data,
and an error field, a stream for the server's error log.

The input field answers to the read([n]) method, where n is the number
of bytes you want to read (or nil if you want the whole input).

The error field answers to the write(...) method.

return values: the HTTP status code, a table with headers, and the output iterator.

--]]

function run (wsapi_env)
  _log = function (...) wsapi_env.error:write(...) end      -- set up the log output, note colon syntax -- 2016.02.26
 
  lfs.mkdir (DIRECTORY)
   
  local PK = userdata.attributes.PK_AccessPoint or "XXXXXXXX"
  local DATE = os.date "%Y-%m-%d" or "0000-00-00"
  local fmt = "%s/backup.openLuup-%s-%s"
  local fname = fmt: format (DIRECTORY, PK, DATE) 
  _log ("Backing up user_data to " .. fname)
 
  local ok, msg = userdata.save (nil, fname)   -- save current luup environment
 
  local status, return_content
  if ok then
    status, return_content  = 200, "backup completed"
  else
    status, return_content  = 500, "backup failed: " .. msg
  end
 
  local headers = {["Content-Type"] = "text/plain"}
 
  local function iterator ()     -- one-shot iterator, returns content, then nil
    local x = return_content
    return_content = nil
    return x
  end

  return status, headers, iterator
end

-----

You'll also need the latest commit from the openLuup GitHub development branch to make this work.


What needs to be done make this run daily?

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: openLuup: Tips & Tricks
« Reply #12 on: May 04, 2016, 11:36:42 am »
To run a backup, you just need to issue the HTTP request:

Code: [Select]
http://openLuupIP:3480/cgi-bin/cmh/backup.sh

So if you schedule a daily scene with the following Luup code:

Code: [Select]
luup.inet.wget "http://127.0.0.1:3480/cgi-bin/cmh/backup.sh"

...that should do the trick.
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 konradwalsh

  • Hero Member
  • *****
  • Posts: 566
  • Karma: +19/-6
Re: openLuup: Tips & Tricks
« Reply #13 on: May 04, 2016, 11:49:58 am »
thanks!

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: openLuup: Tips & Tricks
« Reply #14 on: August 03, 2017, 01:00:44 pm »
You can always add useful global variable or function definitions to your Lua Startup code, and then they're available for use within you Lua Test code or Scene Lua without further ado.

For example, I include in my Startup Lua:

Code: [Select]
json = require "openLuup.json"

and then in Test Lua or scenes I can simply use it...

Code: [Select]
local x = json.decode (y)
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.