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

Moderator: leecollings

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

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

Post 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
Last edited by EricT on Thursday 14 June 2018 13:00, edited 1 time in total.
if it was easy everybody would do it
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: email notification when a user disarmed the alarm

Post 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.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
EricT
Posts: 30
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

Re: email notification when a user disarmed the alarm

Post 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
if it was easy everybody would do it
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: email notification when a user disarmed the alarm

Post by ben53252642 »

How exactly will Domoticz know which user disarmed the alarm?

What is the interface with Domoticz?
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
User avatar
DewGew
Posts: 579
Joined: Thursday 21 April 2016 12:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10618
Location: Sweden
Contact:

Re: email notification when a user disarmed the alarm

Post 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)
Raspberry Pi 3 | domoticz | Aeon Labs Z-Stick GEN5 | RFlink gateway
NanoPi NEO-air | REGO6XX interface | Machinon theme | Homebridge | Domoticz Google Assistant | ideAlarm
EricT
Posts: 30
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

Re: email notification when a user disarmed the alarm

Post 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.
Attachments
user log.png
user log.png (3.36 KiB) Viewed 3592 times
if it was easy everybody would do it
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: email notification when a user disarmed the alarm

Post 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.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
EricT
Posts: 30
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

Re: email notification when a user disarmed the alarm

Post 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.
if it was easy everybody would do it
EricT
Posts: 30
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

Re: email notification when a user disarmed the alarm

Post by EricT »

So nobody knows how to pull the name of the user that has logged in into a Lua? :shock:
if it was easy everybody would do it
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: email notification when a user disarmed the alarm

Post 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)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: email notification when a user disarmed the alarm

Post 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
Last edited by jvdz on Thursday 14 June 2018 23:19, edited 1 time in total.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: email notification when a user disarmed the alarm

Post 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.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: email notification when a user disarmed the alarm

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: email notification when a user disarmed the alarm

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: email notification when a user disarmed the alarm

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: email notification when a user disarmed the alarm

Post 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)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: email notification when a user disarmed the alarm

Post 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-) .
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: email notification when a user disarmed the alarm

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
EricT
Posts: 30
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

Re: email notification when a user disarmed the alarm

Post 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
if it was easy everybody would do it
EricT
Posts: 30
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

Re: email notification when a user disarmed the alarm

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

Who is online

Users browsing this forum: No registered users and 1 guest