Author Topic: Event Watcher with PLEG - writing to the Log  (Read 314 times)

Offline kartcon

  • Full Member
  • ***
  • Posts: 156
  • Karma: +7/-0
Event Watcher with PLEG - writing to the Log
« on: December 03, 2017, 10:40:09 am »
akbooer,

I was hoping you might be able to help me solve a problem I am having. Event Watcher is installed and working well, writing its logs to the /dataMine/EventWatcher/ folder as described in your documentation. Where I am struggling is with writing events to the log. Your documentation uses this bit of code:
Code: [Select]
local function myEvent (text)
  local url = require "socket.url"
  luup.inet.wget ("http://127.0.0.1:3480/data_request?id=lr_EventWatcher&event=" ..
url.escape(text))
end
which I believe may be a little outdated. I am on UI7 with a Vera Secure. (1.7.3233) The current code to access the data is on my machine (192.168.0.11) is:
Code: [Select]
http://192.168.0.11/port_3480/data_request?id=lr_EventWatcher&report=log
This works perfectly, but notice the difference in how the port is defined. All following code uses this basic approach.

What I want to do is to be able to write to the log from within PLEG, using the LUA code in the Logic Action. I can successfully write one line of data using this code:
Code: [Select]
luup.inet.wget ("http://192.168.0.11/port_3480/data_request?id=lr_EventWatcher&event=Something_Happened")
but would like to expand on this with a function that includes URL encoding. I have placed the following code in the LUA startup section of the PLEG instance:
Code: [Select]
local function EWLogIt (text)
  local url = require "socket.url"
  local text = "HVAC PLEG" .. url.escape(text)
  luup.inet.wget ("http://127.0.0.1/port_3480/data_request?id=lr_EventWatcher&event=" .. text)
end
and am calling the function in the LUA code of the associated action within that PLEG instance using:
Code: [Select]
EWLogIt "Something happened"

The ultimate goal is to add the start-up code to each of my 4 instances of PLEG, changing the prefixed text to reflect each PLEG, and log specific data to the Event Watcher log. Having a log written to USB allows all logged data to survive a restart, which is a huge advantage in my opinion. Using a function reduces code overhead, simplifies coding changes and makes the call to the function universal. As I understand it, each PLEG uses its own startup functions so calls to the function are local to that instance.

Unfortunately, this does not work. In my mind there are several possibilities; 1) The function is not being called correctly; 2) The function is not defined correctly (Global vs Local?); 3) There is a syntax error in the code or 4) What I am trying to do is simply not possible in this situation. I believe that the code is correct and that this should be possible so I am at a loss for why it does not work.

I thank you for all the work you have done with your plug-ins and hope that there is a simple solution to my problem


Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6127
  • Karma: +274/-69
  • "Less is more"
Re: Event Watcher with PLEG - writing to the Log
« Reply #1 on: December 03, 2017, 12:49:44 pm »
It sounds as though your issue is more to do with PLEG than EventWatcher? 

Have you been able to test the code outside of the PLEG context (ie. in Lua Test or Startup?)

I?m not in the least familiar with PLEG, so I don?t know how it arranges its code, but if it is the same as usual then it's quite possible that the function needs to be defined as a global (so simply remove the local from its definition.)

Perhaps RTS will be along to sort this out.  If not, you may wish to repost of the PLEG sub-forum.  However, my advice is certainly to check the code without PLEG before trying to embed it in the same.
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 kartcon

  • Full Member
  • ***
  • Posts: 156
  • Karma: +7/-0
Re: Event Watcher with PLEG - writing to the Log
« Reply #2 on: December 03, 2017, 01:15:08 pm »
akbooer,

Thanks for the quick response. I will check the forum for help calling a function within PLEG and may reach out to RTS if I still can't get it figured out. I will also try testing the code outside PLEG but not sure if I can call a function using the Test Lua. Does not seem like it could hurt to try. Thanks again!!

kartcon

Offline kartcon

  • Full Member
  • ***
  • Posts: 156
  • Karma: +7/-0
Re: Event Watcher with PLEG - writing to the Log - RESOLVED
« Reply #3 on: December 03, 2017, 03:10:17 pm »
OK, so if you want to log system actions to Event Watcher from PLEG and have the ability to retain data beyond system reboots this is my final working code. Place this part in the Start Up Lua section of PLEG.

Code: [Select]
function EWLogIt (var_txt)
  var_url = require "socket.url"
  var_txt = "System PLEG: " .. var_txt
  var_txt = var_url.escape(var_txt)
  luup.inet.wget ("http://127.0.0.1:3480/data_request?id=lr_EventWatcher&event=" .. var_txt)
end

and this line goes in the LUA code section of the Action being logged:

Code: [Select]
EWLogIt "Text to get logged"

Each PLEG instance gets the startup code, and you can edit the prefix part to reflect the nature of that PLEG instance. In my system, with 4 instances running, the prefixes are Alarm PLEG, System PLEG, HVAC PLEG and Weather PLEG. You won't need to add anything to Event Watcher, as the logged entries are displayed as category 'E' which is on by default. The function will URL encode the text, so there is no need to add underscores or manually encode it. Type the text you want logged and let it run. Do note that in order to retain the data beyond reboots, you must log the data to USB or other storage. The Event Watcher documentation is very clear on how to do this, especially if you are already using DataMine. Thank you to akbooer and RichardTSchaefer for their hard work on these great plug-ins. Without either my Vera would not be nearly as useful.

Below is a very small excerpt of the log files during testing:

2017-12-03 14:31:19.712, S,  81, Kit Motion Sensor, Tripped, 1,
2017-12-03 14:31:19.726, E,   0, , User, System PLEG: Kitchen Motion,