Page 1 of 2

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

Posted: Wednesday 16 May 2018 10:55
by EricT
Hi all,

Is it possible to send an email notification if one of the users has armed or disarmed the alarm?

E.g if user X has logged in and disarmed the alarm an email notification is send to the other users like:

user X has disarmed the alarm

Thanks for your help.

Eric

Re: email notification when a user disarmed the alarm

Posted: Wednesday 16 May 2018 12:13
by ben53252642
Yes this is easily possible but I don't see the need to explain it specifically for your use case in this post.

The alarm is simply a device of which the typical Domoticz home usually has dozens if not hundreds.

You need to read the Wiki and perhaps some threads in this forum to find how to send a notification when a device state changes.

Eg: Bed light turned off = send notification to x user

It's the same thing as: Device alarm turned off (disarmed or whatever) send notification to x user or users.

Re: email notification when a user disarmed the alarm

Posted: Wednesday 16 May 2018 12:35
by EricT
I am sorry but maybe I was not clear enough.

I can send an email when something is switched on or off, no problem with that. What I am looking for that it mentioned in the email which user has done it, to be more specific name of the user that has logged in and made the change.

This email has to be send to the other users to notify them that it was user X that has done it.

I can see in the log which user activated an event but I do not know how to get that user name in an email notification.

I hope this explains what I am looking for.

Eric

Re: email notification when a user disarmed the alarm

Posted: Wednesday 16 May 2018 12:58
by ben53252642
How exactly will Domoticz know which user disarmed the alarm?

What is the interface with Domoticz?

Re: email notification when a user disarmed the alarm

Posted: Wednesday 16 May 2018 13:02
by DewGew
You can add several users to domoticz. In my head it should work some how. :?
When you switch a device it says in the log:
2018-05-16 13:04:30.438 User: Dewgew initiated a switch command (130/Lamp outside/On)

Re: email notification when a user disarmed the alarm

Posted: Wednesday 16 May 2018 13:08
by EricT
That is exactly my question.

In the log it is visable who made a change thus somehow Domoticz knows who made the change (see attachment). So the question is how to get that name in an email to other users.

Re: email notification when a user disarmed the alarm

Posted: Wednesday 16 May 2018 13:17
by ben53252642
Now I understand, the logic you are looking for:

If x user controls x device and state = x then perform x (send an email in your case).

One solution I can think of is this:

Create a dummy device for each user and in security lock it down so only they and the domoticz admin user can see their dummy device.

User can turn dummy device on to arm or off to disarm etc... (you will need to use a Lua script, Dzvents or maybe blockly can do it)

Then you can use blockly or a script in events eg: If x device (the dummy device for that user) becomes x state then send an email to x.

Re: email notification when a user disarmed the alarm

Posted: Wednesday 16 May 2018 13:37
by EricT
Thank you very much for your advice but I only want to make the dashboard available to the users.

I have the alarm arm/disarm switch on the Domoticz dashboard available for all users. If one of the users arms or disarms the alarm I want to notify the other users who done it.

I currently use a Lua script to send an email to all users that the alarm is armed or disarmed. I want to be more specific to avoid confusion among the users, therefore I want to know how to pull that name of the user into my Lua.

Re: email notification when a user disarmed the alarm

Posted: Tuesday 12 June 2018 10:55
by EricT
So nobody knows how to pull the name of the user that has logged in into a Lua? :shock:

Re: email notification when a user disarmed the alarm

Posted: Tuesday 12 June 2018 12:33
by waaren
EricT wrote: Tuesday 12 June 2018 10:55 So nobody knows how to pull the name of the user that has logged in into a Lua? :shock:
if the name is in the log directly after a user disarmed the device (and therewith activated an event that can be picked up by dzVents), dzVents could be used to get the last loglines, - analyze these for that specific event and extract the name from it. It then can be used to create a message including the name obtained from the logfile.

using
openURL( http://domoticzIP:domoticzPORT/json.htm?type=command&param=getlog)

Re: email notification when a user disarmed the alarm

Posted: Tuesday 12 June 2018 14:50
by jvdz
This is a nice idea to use the JSON call for retrieving the information. This is an example script that would do this in LUA:

Code: Select all

http = require "socket.http";
socket = require "socket";
https = require "ssl.https";
json = require "JSON";
DOMO_url = "http://192.168.xx.xx:8080"

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 = "Switch Name Here"  -- define the switch you want to test for
if devicechanged[device] then
   user,timeinfo,idx,status = getswitchedbyinfo(device)
   if user ~= "" then
      print("==> device "..device.." with IDX"..idx.." switched by ".. user.." at "..timeinfo .. " status:"..status)
   else
      print("==> Can't find log information for device ".. device)
   end
end
return commandArray
Jos

Re: email notification when a user disarmed the alarm

Posted: Tuesday 12 June 2018 15:19
by ben53252642
Very interesting! I had no idea it was possible to pull the log via JSON.

Initially looking at it though doesn't seem like it delivers much previous history, not sure if Lua polling every minute would be sufficient but a device trigger that immediately checks the log would likely be able to get the needed information.

Re: email notification when a user disarmed the alarm

Posted: Tuesday 12 June 2018 15:23
by jvdz
ben53252642 wrote: Tuesday 12 June 2018 15:19 .. but a device trigger that immediately checks the log would likely be able to get the needed information.
That is more or less what the posted example LUA script does. ;)

Jos

Re: email notification when a user disarmed the alarm

Posted: Tuesday 12 June 2018 16:35
by waaren
jvdz wrote: Tuesday 12 June 2018 15:23
ben53252642 wrote: Tuesday 12 June 2018 15:19 .. but a device trigger that immediately checks the log would likely be able to get the needed information.
That is more or less what the posted example LUA script does. ;)

Jos
This indeed the way I taught of to get to the required result. Still have a couple a questions / remarks.

If the username ends up in the log it must be known to domoticz in the first place. I red a discussion on GIT about exposing this username. That would make a more natural method to get this information possible. But in the mean time it would be nice to see if we can turn this idea in something workable.

Please do not read the next lines as an attack on the use of Lua as opposed to dzVents (also Lua :D ) . I only want to know if my observations / assumptions are correct.

This Lua script is triggered by every device change on the system. In dzVents it would only be triggered when the specific device will trigger it.
This Lua script will stay alive until the system returned the JSON to script. dzVents use async communication to prevent that. It is my understanding that there was a discussion between the developers of dzVents and @gizmocuz on the use of this but cannot remember the exact worries that led to the use of async http calls.

Re: email notification when a user disarmed the alarm

Posted: Tuesday 12 June 2018 19:50
by jvdz
waaren wrote: Tuesday 12 June 2018 16:35 This Lua script is triggered by every device change on the system. In dzVents it would only be triggered when the specific device will trigger it.
This Lua script will stay alive until the system returned the JSON to script. dzVents use async communication to prevent that. It is my understanding that there was a discussion between the developers of dzVents and @gizmocuz on the use of this but cannot remember the exact worries that led to the use of async http calls.
I don't know the internals of dzVent but the event system is indeed single threaded in domoticz, so correct that it will have to wait until it is finished.
The LUA script also first checks for the changed device to only perform the logic when the appropriate device is changed.
I am honestly not worried in this case as the JSON call is likely done to 127.0.0.1 so the should be a very low delay.
The posted script is a simply example of how to retrieve the information based on your thoughts and anything is possible when you put your mind to it. :)

Jos

Re: email notification when a user disarmed the alarm

Posted: Tuesday 12 June 2018 21:29
by waaren
ben53252642 wrote: Tuesday 12 June 2018 15:19 Very interesting! I had no idea it was possible to pull the log via JSON.

Initially looking at it though doesn't seem like it delivers much previous history, not sure if Lua polling every minute would be sufficient but a device trigger that immediately checks the log would likely be able to get the needed information.
@ben53252642. In the source the max number of lines in the (memory) buffer for loglines is hardcoded to 100. This should be sufficient in most cases but if not, it is easy to lift by changing the line #define MAX_LOG_LINE_BUFFER 100 in the file logger.cpp to a higher number before compiling.
For test purposes I changed it to 10000, compiled it and it executes without problems and without a noticeable raise in memory usage after 1 hour. (2000 loglines)

Re: email notification when a user disarmed the alarm

Posted: Tuesday 12 June 2018 21:52
by ben53252642
@warren it should probably default to 100 and be possible to specify a higher value in the JSON url.

A mild enhancement (if it ever gets added) 8-) .

Re: email notification when a user disarmed the alarm

Posted: Tuesday 12 June 2018 22:41
by waaren
ben53252642 wrote: Tuesday 12 June 2018 21:52 @warren it should probably default to 100 and be possible to specify a higher value in the JSON url.

A mild enhancement (if it ever gets added) 8-) .
Not sure if I understand you but because of the limit set in logger.cpp to 100 the buffer cannot have more than 100 lines. Before line 101 is added to this buffer line 1 is removed. The json can only get lines available in that buffer.
If the developers decide to increase this buffersize (and I strongly doubt they will as it just as easy to get more loglines via the OS) the API could be changed in such a way that you can ask "give me the last nnn loglines"

but indeed probably only a theoretical contemplation :D

Re: email notification when a user disarmed the alarm

Posted: Tuesday 12 June 2018 23:38
by EricT
Wow guys, I am truly impressed.

I did not have the time to read it all, I will have a closer look at it later.

Eric

Re: email notification when a user disarmed the alarm

Posted: Wednesday 13 June 2018 14:10
by EricT
Today I have tried the Lua proposed by Jos but I get the below error:

Code: Select all

 Error: EventSystem: in name test: [string "..."]:3: module 'socket.http' not found:
Am I missing some dependencies?