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

User avatar
jvdz
Posts: 1423
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/Netherlands
Contact:

Re: email notification when a user disarmed the alarm

Post by jvdz » Wednesday 13 June 2018 16:35

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

EricT
Posts: 35
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

Re: email notification when a user disarmed the alarm

Post by EricT » Wednesday 13 June 2018 21:30

Aah, ok.

Thanks Jos I will have another go at it tomorrow!

Eric
if it was easy everybody would do it

EricT
Posts: 35
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

Re: email notification when a user disarmed the alarm

Post by EricT » Thursday 14 June 2018 9:48

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
Last edited by EricT on Thursday 14 June 2018 23:31, edited 1 time in total.
if it was easy everybody would do it

User avatar
jvdz
Posts: 1423
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/Netherlands
Contact:

Re: email notification when a user disarmed the alarm

Post by jvdz » Thursday 14 June 2018 12:41

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

EricT
Posts: 35
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

Re: email notification when a user disarmed the alarm

Post by EricT » Thursday 14 June 2018 12:47

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.#youremaill@hotmail.com'
   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.#youremaill@hotmail.com'
   end
end

return commandArray

Last edited by EricT on Friday 15 June 2018 8:30, edited 2 times in total.
if it was easy everybody would do it

User avatar
waaren
Posts: 1075
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post by waaren » Thursday 14 June 2018 22:49

@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 !
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

EricT
Posts: 35
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

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

Post by EricT » 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)
if it was easy everybody would do it

User avatar
jvdz
Posts: 1423
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/Netherlands
Contact:

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

Post by jvdz » Thursday 14 June 2018 23:13

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

User avatar
waaren
Posts: 1075
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post by waaren » Thursday 14 June 2018 23:17

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
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

User avatar
jvdz
Posts: 1423
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/Netherlands
Contact:

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

Post by jvdz » Thursday 14 June 2018 23:20

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

EricT
Posts: 35
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

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

Post by EricT » Thursday 14 June 2018 23:38

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
if it was easy everybody would do it

User avatar
jvdz
Posts: 1423
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/Netherlands
Contact:

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

Post 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:

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

EricT
Posts: 35
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

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

Post by EricT » Friday 15 June 2018 15:25

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
if it was easy everybody would do it

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest