Page 2 of 2

Re: email notification when a user disarmed the alarm

Posted: Wednesday 13 June 2018 16:35
by jvdz
That means you are missing some LUA libraries. :)
I have them installed as described in the DTGBOT wiki: https://www.domoticz.com/wiki/Remote_Co ... _Libraries

Jos

Re: email notification when a user disarmed the alarm

Posted: Wednesday 13 June 2018 21:30
by EricT
Aah, ok.

Thanks Jos I will have another go at it tomorrow!

Eric

Re: email notification when a user disarmed the alarm

Posted: Thursday 14 June 2018 9:48
by EricT
Hi Jos,

I have created the Lua libraries as described here: https://www.domoticz.com/wiki/Remote_Co ... _Libraries and it WORKS! :D :D :D

I only received the error Error: EventSystem: in name test: [string "..."]:36: attempt to index global 'devicechanged' (a nil value) but changing the Lua type from All to Device solved the error.

Next step is using it in my email notification.

Thanks Jos! Thumbs Up!

Eric

Re: email notification when a user disarmed the alarm

Posted: Thursday 14 June 2018 12:41
by jvdz
You're welcome. :D
I've played a little more with the code and it is working pretty fast so no issues using it in the Event system thus far
I did set the domoticzurl to the below assuming it needs to stay on the same server:

Code: Select all

DOMO_url = "http:///127.0.0.1:8080"
Jos

Re: email notification when a user disarmed the alarm

Posted: Thursday 14 June 2018 12:47
by EricT
I have added the email notification and it is working like a charm! :D

Tanks again Jos!

BTW, I have used the local Raspberry Pi IP address and it issues an email immidiately when the switch is turned On/Off. It is working and I do not know the difference when using http:///127.0.0.1:8080 :(

Eric

For those who are interested, here is my completed script updated with loglevel2 for latest beta's:

Code: Select all

-- This script requires the installation of Lua 5.2 and the libraries
-- https://www.domoticz.com/wiki/Remote_Control_of_Domoticz_by_Telegram_Bot#Installing_Lua_Libraries
-- JQ dependencie is not requiered

--- This script sends an email when a user has turned the Test Switch On/Off
--- !!! Make sure this Lua is set as "Device", NOT as "All".

http = require "socket.http"; 
socket = require "socket";
https = require "ssl.https";
json = require "JSON";
DOMO_url = "http://192.168.x.xx:8080" -- Raspberry Pi local IP address

commandArray = {}

-- Get information recent log entries for switch action
function getswitchedbyinfo(device)
   local response, returncode = http.request(DOMO_url..'/json.htm?type=command&param=getlog&loglevel=2')  ---&loglevel=2 is required with the latest beta's.
   local suser=""
   local stime=""
   local sidx=""
   local sstatus=""
   local input = json:decode(response)
   local result_table = input['result']
   local tc = #result_table
   for i = 1, tc do
            for ltime,luser,lidx,ldevice,lstatus in string.gmatch(input.result[i].message, '(.-)%sUser:%s(.-)%sinitiated a switch command.-%((.-)/(.-)/(.-)%)')
            do
               if ldevice==device then
                  suser=luser
                  stime=ltime
                  sidx=lidx
                  sstatus=lstatus
               end
            end
   end
   return suser,stime,sidx,sstatus
end

device = "Test Switch"  -- define the switch you want to use

if devicechanged[device] == 'On' then
   user,timeinfo,idx,status = getswitchedbyinfo(device)
   if user ~= "" then
      commandArray['SendEmail'] =  'The Test Switch is turned On by '..user..', blabla # The Test Switch was turned On by  '..user..' , blabla.#[email protected]'
   end
end

if devicechanged[device] == 'Off' then
   user,timeinfo,idx,status = getswitchedbyinfo(device)
   if user ~= "" then
       commandArray['SendEmail'] =  'The Test Switch is turned Off by '..user..', blabla # The Test Switch was turned Off by  '..user..' , blabla.#[email protected]'
   end
end

return commandArray


Re: [another one solved] email notification when a user disarmed the alarm

Posted: Thursday 14 June 2018 22:49
by waaren
@EricT @jvdz,

could you please share the logline with the username that you get using the getlog json ?
For some reason I only get the username on a "Status:" logline and these lines are not returned by the json command (on my system)
(I do see them in my os logfile so I can go ahead and create a dzVents version of this method but I like my initial idea more)

Thanks in advance !

Re: [another one solved] email notification when a user disarmed the alarm

Posted: Thursday 14 June 2018 23:11
by EricT
Here it is:

Code: Select all

2018-06-14 23:09:29.822 User: Eric initiated a switch command (52/Activity Main Switch/Off)

Re: [another one solved] email notification when a user disarmed the alarm

Posted: Thursday 14 June 2018 23:13
by jvdz
I am still running on version V3.8793 on the systems I worked with. That is returneing both Level 1 & 2 messages with the getlog JSON request.
Checked now also an other domoticz setup for development which is running on V3.9625 and there it defaults to level 1 only. You can change this to get level 2 by adding this to the URL: /json.htm?type=command&param=getlog&loglevel=2

Jos

Re: [another one solved] email notification when a user disarmed the alarm

Posted: Thursday 14 June 2018 23:17
by waaren
EricT wrote: Thursday 14 June 2018 23:11 Here it is:

Code: Select all

2018-06-14 23:09:29.822 User: Eric initiated a switch command (52/Activity Main Switch/Off)
Many thanks Eric,

please be aware that if you move on to a later version the way this is logged will change and therefore you then will need to add &loglevel=2 to the getlog json to get this type of message. Not sure with what version is starts but I know for sure >= V3.9580

Re: [another one solved] email notification when a user disarmed the alarm

Posted: Thursday 14 June 2018 23:20
by jvdz
I have added it to the earlier posted code as the older domoticz version simply ignores it and it's required for the latest versions.

Jos

Re: [another one solved] email notification when a user disarmed the alarm

Posted: Thursday 14 June 2018 23:38
by EricT
please be aware that if you move on to a later version the way this is logged will change and therefore you then will need to add &loglevel=2 to the getlog json to get this type of message. Not sure with what version is starts but I know for sure >= V3.9580
I have added it to the earlier posted code as the older domoticz version simply ignores it and it's required for the latest versions.
Thanks guys!

I do not have plans to update to a newer Domoticz version any time soon (last time I did I had to roll back due to issues) but just in case I have updated my script.

Just a little advice to whom it may concern; make sure you have an image of your Raspberry Pi when updating to a newer version. I only had an old image thus I had some work to get it back where I left before the update. :cry:

Eric

Re: [another one solved] email notification when a user disarmed the alarm

Posted: Friday 15 June 2018 14:54
by jvdz
This is now the function i have in my general lua library file. It also limits the retrieval period of the log records to the last 15 seconds:

Code: Select all

http = require "socket.http";
socket = require "socket";
https = require "ssl.https";
json = require "JSON";

-- Get information from the last 15 seconds of log entries for the switch action details, returning User&Time and IDX&Status of the device
function getswitchedbyinfo(device)
   local LastXXsecs = os.time()-15
   local response, returncode = http.request('http://127.0.0.1:8080/json.htm?type=command&param=getlog&loglevel=2&lastlogtime='..tostring(LastXXsecs))
   local suser=""
   local stime=""
   local sidx=""
   local sstatus=""
   local input = json:decode(response)
   local result_table = input['result']
   local tc = #result_table
   for i = 1, tc
   do
      for ltime,luser,lidx,ldevice,lstatus in string.gmatch(input.result[i].message, '(.-)%sUser:%s(.-)%sinitiated a switch command.-%((.-)/(.-)/(.-)%)')
      do
         if ldevice==device then
            suser=luser
            stime=ltime
            sidx=lidx
            sstatus=lstatus
         end
      end
   end
   return suser,stime,sidx,sstatus
end
Jos

Re: [another one solved] email notification when a user disarmed the alarm

Posted: Friday 15 June 2018 15:25
by EricT
by jvdz » Friday 15 June 2018 14:54

This is now the function i have in my general lua library file. It also limits the retrieval period of the log records to the last 15 seconds:

Thanks Jos!

Eric